栈的顺序存储(利用数组来完成出入栈,将top赋值为0时)
#include <iostream.h>
#define MAX  100
struct stack
{
 int data[MAX];
    int top;
};
 
void main()
{
 struct stack st1; char ch;
//int stack_length;
//<<"input the reality stack_length:";
//cin>>stack_length;
st1.top=0; //置空栈
while(st1.top<MAX)//判断栈是否已满;
{
 
cout<<"would you like pop the data: y/n?";
cin>>ch;
if (ch=='y')
{ if (st1.top==0) { cout<<"the stack is empty!"; continue;}  
   st1.top--;
   cout<<st1.top<<"  :  "<<st1.data[st1.top]<<endl;
}  //是否出栈
 
 cout<<"input a data to  the stack:";
 cin>>st1.data[st1.top];
 st1.top++;
   
cout<<"would you like continue push the data: y/n?";
cin>>ch;
if (ch=='n') break;
}
//输出栈
cout<<"the stack's actual length is :"<<st1.top;
 cout<<'\n';
if(st1.top==0)  cout<<"the stack is empty!";
 while(st1.top>0)   
 {    st1.top--;
   cout<<st1.top<<"  :  "<<st1.data[st1.top]<<endl;
}
}