当前位置:文档之家› 数据结构——顺序栈的基本操作

数据结构——顺序栈的基本操作

{
cout<<"* 1、构造一个空栈*"<<endl;
cout<<"* 2、输入栈的元素*"<<endl;
cout<<"* 3、输出栈的元素*"<<endl;
cout<<"* 4、求栈的长度*"<<endl;
cout<<"* 5、求栈顶元素*"<<endl;
cout<<"* 6、删除栈顶元素*"<<endl;
S.stacksize=STACK_INIT_SIZE;
cout<<"构造成功!!!"<<endl;
}
}
void Push(SqStack &S,int e)//插入元素e为栈顶元素
{
if(S.top-S.base>=S.stacksize)
{
S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int));
if(k==1) InitStack(S);
if(k==2)
{
int a;
cout<<"输入栈S的元素为: ";
cin>>a;
Push(S,a);
DisplayStack(S);
}
if(k==3) DisplayStack(S);
if(k==4) cout<<"栈的长度为: "<<StackLength(S)<<endl<<endl;
if(!S.base) cout<<"存储分配失败!!!"<<endl<<endl;
else
{
S.stacksize+=STACKINCREMENT;
S.top=S.base+S.stacksize;
}
}
*S.top++=e;
}
void DisplayStack(SqStack &S) //从栈底到栈顶逐次显示栈中的元素
}
void GetTop(SqStack S,int &e)//返回栈顶元素
{
if(S.top==S.base) cout<<"操作失败!!!"<<endl<<endl;
else
{
cout<<"栈顶元素为: ";
e=*(S.top-1);
cout<<e<<endl<<endl;
}
}
int main()
DisplayStack(S);
}
}
void ClearStack(SqStack &S)//清空
{
int b;
while(S.top!=S.base) b=*--S.top;
if(S.top==S.base) cout<<"顺序栈已清空!!!"<<endl<<endl;
}
void StackEmpty(SqStack S)//判空
if(k==5) {int c;GetTop(S,c);}
if(k==6) {int b;pop(S,b);}
if(k==7) ClearStack(S);
if(k==8) StackEmpty(S);
}
return 0;
}
{
int *p;
p=S.base;
if(S.base==S.top)cout<<"当前栈为空栈!!!"<<endl<<endl;
else
{
cout<<"当前栈内元素为: ";
while(p!=S.top)
{
cout<<*(p)<<" ";
p++;
}
cout<<endl;
}
}
int StackLength(SqStack S) //求长度
{
if(S.top==S.base) cout<<"顺序栈为空!!!"<<endl<<endl;
else cout<<"顺序栈不为空!!!"<<endl<<endl;
}
void DestroyStack(SqStack &S)
{
S.base=NULL;
cout<<"顺序栈已销毁!!!"<<endl<<endl;
#include<iostream>
using namespace std;
# define STACK_INIT_SIZE 100
# define STACKINCREMENT 10
typedef struct
{
int * base;
int * top;
int stacksize;//当前栈可使用的最大容量
} SqStack;
void InitStack(SqStack &S)//构造一个空栈
{Байду номын сангаас
S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S.base) {cout<<"存储分配失败!!!"<<endl<<endl;}
else
{
S.top=S.base;
{
int *p;
p=S.base;
int i=0;
while(p!=S.top) {p++;i++;}
return i;
}
void pop(SqStack &S,int &e) //出栈
{
if (S.top==S.base) cout<<"操作失败!!!"<<endl<<endl;
else
{
e=*--S.top;
cout<<"* 7、清空已存在的栈*"<<endl;
cout<<"* 8、判断栈是否为空*"<<endl;
cout<<"* 0、销毁栈*"<<endl;
int n,k;
SqStack S;
for(n=0;n<15;n++)
{
cout<<"请选择0-8: ";
cin>>k;
if(k==0) {DestroyStack(S);n=15;}
相关主题