数据结构栈1Word下载.docx
《数据结构栈1Word下载.docx》由会员分享,可在线阅读,更多相关《数据结构栈1Word下载.docx(13页珍藏版)》请在冰豆网上搜索。
template<
classT>
classStack
{
public:
Stack(){};
virtual~Stack();
virtualvoidPush(constT&
x)=0;
virtualboolPop(T&
virtualboolgetTop(T&
x)const=0;
virtualboolIsEmpty()const=0;
virtualboolIsFull()const=0;
virtualintgetSize()const=0;
};
2、顺序栈的类定义、实现与测试:
#include<
assert.h>
iostream.h>
#include"
stdlib.h"
classSeqStack{
SeqStack(intsz=50);
~SeqStack(){delete[]elements;
voidPush(constT&
x);
boolPop(T&
boolgetTop(T&
x)const;
boolIsEmpty()const{return(top==-1)?
true:
false;
}
boolIsFull()const{return(top==maxSize-1)?
intgetSize()const{returntop+1;
voidMakeEmpty(){top=-1;
boolInPut();
boolOutPut()const;
private:
T*elements;
inttop;
intmaxSize;
voidoverflowProcess();
SeqStack<
T>
:
SeqStack(intsz):
top(-1),maxSize(sz){
elements=newT[maxSize];
assert(elements!
=NULL);
template<
voidSeqStack<
overflowProcess(){
intstackIncreament=maxSize;
T*newArray=newT[maxSize+stackIncreament];
if(newArray==NULL){cerr<
<
"
存储分配失败!
endl;
exit
(1);
for(inti=0;
i<
=top;
i++)newArray[i]=elements[i];
maxSize=maxSize+stackIncreament;
delete[]elements;
elements=newArray;
boolSeqStack<
Pop(T&
x){
if(IsEmpty()==true)returnfalse;
x=elements[top--];
returntrue;
Push(constT&
if(IsFull()==true)overflowProcess();
elements[++top]=x;
getTop(T&
x)const{
x=elements[top];
InPut(){
Tval;
cin>
>
val;
Pop(val);
OutPut()const{
cout<
elements[top]<
测试:
#include<
SeqStack.h"
voidmain()
SeqStack<
int>
seq(10);
intm;
for(inti=0;
5;
i++)
请输入要入栈的一个整数:
;
m;
seq.Push(m);
//cout<
seq.getTop(m)<
}
intt=seq.getSize();
当前栈内共有元素:
t<
seq.Pop(m);
t=seq.getSize();
执行Pop()后栈内元素个数为:
endl<
//seq.Push(x);
请输入InPut()的val:
seq.InPut();
执行InPut()后栈内元素个数为:
执行OutPut()后栈顶元素为:
seq.OutPut();
3、链栈的类定义、实现与测试:
structLinkNode
Tdata;
LinkNode*link;
LinkNode(LinkNode*ptr=NULL){link=ptr;
LinkNode(Titem,LinkNode*ptr){data=item;
link=ptr;
classLinkedStack
LinkedStack():
top(NULL){}
virtual~LinkedStack(){makeEmpty();
boolIsEmpty()const{return(top==NULL)?
intgetSize();
voidmakeEmpty();
friendostream&
operator<
(ostream&
os,SeqStack<
&
s);
LinkNode<
*top;
voidLinkedStack<
makeEmpty(){
*p;
while(top!
=NULL)
{p=top;
top=top->
link;
deletep;
Push(constT&
x){
top=newLinkNode<
(x,top);
//assert(top!
boolLinkedStack<
Pop(T&
if(IsEmpty()==true)returnfalse;
structLinkNode<
*p=top;
top=top->
x=p->
data;
deletep;
getTop(T&
x)const{
x=top->
intLinkedStack<
getSize(){
*p=top;
intk=0;
{top=top->
k++;
returnk;
ostream&
s){
os<
栈中元素个数="
s.getSize()<
*p=S.top;
inti=0;
while(p!
{os<
++i<
p->
data<
p=p-link;
returnos;
LinkedStack.h"
LinkedStack<
lsk;
intx=2;
lsk.Push(x);
x=3;
intt=lsk.getSize();
cout<
把2和3压栈后链栈内的元素个数为:
lsk.makeEmpty();
t=lsk.getSize();
清空栈内元素后,栈内元素个数为:
把1,3,5压栈进入栈内"
x=1;
lsk.Push(x);
x=3;
x=5;
依次弹出栈内元素:
while(lsk.IsEmpty()==false){
lsk.Pop(x);
x<
"
4、实现括号匹配:
string.h>
classkuohao
voidPrintMatchedPairs(char*expression){
SeqStack<
char>
s(50);
charj;
intlength=strlen(expression);
for(inti=1;
=length;
i++){
if(expression[i-1]=='
('
||expression[i-1]=='
['
{'
)s.Push(expression[i-1]);
elseif(expression[i-1]=='
)'
]'
}'
){
if(s.Pop(j)==true)
{
if(expression[i-1]=='
j=='
)
{cout<
j<
与"
expression[i-1]<
匹配"
j='
'
elseif(expression[i-1]=='
}
elsecout<
没有与第"
i-1<
个右括号匹配的左括号!
}
}
while(s.IsEmpty()==false){
s.Pop(j);
cout<
没有与"
左括号相匹配的右括号!
//constintmaxLength=100;
#include"
kuohao.h"
voidmain(){
kuohaok;
表达式1测试“{[()](())}”:
char*e1="
{[()](())}"
k.PrintMatchedPairs(e1);
表达式2测试“{[()]]}”:
char*e2="
{[()]]}"
k.PrintMatchedPairs(e2);
六、附录
参考《数据结构(用面向对象方法与C++语言描述)》
附上SeqStack.h代码:
//voidPrintMatchedPairs(char*expression);