压入( !a)
{
放入非a资源;
返回!a;
}
弹出(a)
{
拿出a资源;
返回a;
}
开始( )
{
压入(x);
弹出(y);
输出("x"+x+",y"+y);
}
#include
using namespace std;
template
class Stack
{
public:
Stack(int msize=100);
~Stack();
bool IsEmpty();
bool IsFull();
bool Top(T& x);
bool Push(T x);
bool Pop();
void Clear();
void OutPut(ostream& out=cout);
private:
int top;
int maxtop;
T *s;
};
template
Stack
{
top=-1;
maxtop=msize-1;
s=new T[msize];
}
template
Stack
{
delete []s;
}
template
bool Stack
{
return top==-1;
}
template
bool Stack
{
return top==maxtop;
}
template
bool Stack
{
if(Isempty())
{
cout << "UnderFlow" << endl;
return false;
}
x=s[top];
return true;
}
template
bool Stack
{
if(IsFull())
{
cout << "OverFlow" << endl;
return false;
}
s[++top]=x;
return true;
}
template
bool Stack
{
if(IsEmpty())
{
cout << "UnderFlow" << endl;
return false;
}
--top;
return true;
}
template
void Stack
{
if(IsEmpty())
{
out << "Empty" << endl;
return;
}
for(int i=0;i
cout << s[i] << ' ';
}
out << endl;
return;
}
template
void Stack
{
top=-1;
}
顺序栈
#include
class SeqStack
{
private:
int *element;
int top;
int maxSize;
int stackment;
public:
SeqStack(int sz=10)
{
maxSize=sz;
element=new int[maxSize];
top=-1;
stackment=10;
}
~SeqStack();
void overflowProcess();
int Push(int &x);
int Pop(int &x);
bool IsEmpty();
bool IsFull();
};
void SeqStack::overflowProcess()
{
int *newArray=new int[maxSize+stackment];
if(newArray==NULL)
{
cout<<"存储分配失败"<
for(int i=0;i
maxSize=maxSize+stackment;
delete[]element;
}
int SeqStack::Push(int x[])
{
if(IsFull==true)
{
overflowProcess();
return true;
}
for(int i=0;i<10;i++)
{
cin>>x[i];
element[++top]=x[i];
cout<
return ;
}
int SeqStack::Pop(int x[])
{
if(IsEmpty()==true)
return false;
for(int i=10;i>0;i--)
{
x[i]=element[top--];
}
return ;
}
void main()
{
SeqStack a;
a.overflowProcess();
int x[10];
for(int i=0;i<10;i++)
cin>>x[i];
a.Push(x[10]);
a.Pop(x[10]);
}