数据结构:栈的实现

来源:互联网 发布:淘宝手机首页尺寸 编辑:程序博客网 时间:2024/05/22 02:31


using namespace std;
class Stack
{
private:
int maxSize;
int top;
int theArray[100];


public:
Stack(int capacity)
{
maxSize = capacity;
top = -1;
int x;
cin>>x;
int i = 0;
while(x != 0)
{
theArray[++top] = x;
cin>>x;
}
}


bool isEmpty()
{
return top == -1;
}


bool isFull()
{
return top == maxSize - 1;
}


void push(int x)
{
if(isFull())
cout<<"push failed,the stack is full"<<endl;
else
{
theArray[++top] = x;
}
}


void pop()
{
if(isEmpty())
cout<<"pop failed,the stack is empty"<<endl;
else
{
top--;
}
}


int topAndPop()
{
if(isEmpty())
cout<<"top and pop failed,the stack is empty"<<endl;
else
{
return theArray[top - 1];
top--;
}
}


int Top()
{
if(isEmpty())
cout<<"top failed,the stack is empty"<<endl;
else
return theArray[top];
}


void outputStack()
{
if(isEmpty())
cout<<"output failed,the stack is empty"<<endl;
else
{
for(int i = 0;i <= top;i ++)
{
cout<<theArray[i]<<" ";
}
}
}
};
原创粉丝点击