#include <iostream>
#include <vector>
using namespace std;
const int stack_size=30;
const int stack_increase= 10;
typedef struct
{
int* base;
int length;
int* top;
}SqStack;
SqStack Create_stack(SqStack &S)
{
S.base=(int*)malloc(stack_size * sizeof(int));
if(!S.base) exit (OVERFLOW);
S.top=S.base;
S.length=stack_size;
return S;
}
int GetTop(SqStack S,int &e)
{
if(S.top==S.base)
return 0;
e=*(S.top-1);
return e;
}
int Push(SqStack &S,int e)
{
if(S.top-S.base>=S.length)
{
S.base=(int *)realloc(S.base,(S.length+stack_increase)* sizeof(int));
if(!S.base) exit (OVERFLOW);
return 0;
S.top =S.base+S.length;
S.length+=stack_increase;
}
*S.top++=e;
}
int StackLength(SqStack S)
{
return S.top-S.base;
}
int Pop(SqStack &S,int &e)
{
if(S.top==S.base)
return 0;
e = *(--S.top);
return e;
}
int main()
{
SqStack test;
int b;
vector<int> vi;
cout<<"Please Some number(按Ctrl+Z结束): "<<endl;
while (cin>>b)
{
vi.push_back(b);
}
int e;
Create_stack(test);
for(vector<int>::iterator iter = vi.begin();iter != vi.end();++iter)
{
Push(test,*iter);
}
int num= StackLength(test);
GetTop(test,e);
cout<<"The num is:"<<num<<endl;
cout<<"The top is :"<<e<<endl;
for(int i=0;i<=num-1;++i)
{
cout<<Pop(test,e)<<" ";
}
}
栈的一些基本操作
最新推荐文章于 2021-12-15 21:49:55 发布