顺序栈实验

来源:互联网 发布:1688一键传淘宝 编辑:程序博客网 时间:2024/05/17 02:06
#include <iostream>
using namespace std;
#define Stack_Size 200
typedef struct sqStack
{
       char *elem;
       int top;
       int stackSize;
}sqStack;
 
 
void initStack_Sq(sqStack &S)
{
       S.elem=new char[Stack_Size];
       S.top=-1;
       S.stackSize=Stack_Size;
}
 


void creatStack_Sq(sqStack &S,int n)
{
       initStack_Sq(S);
       for(int i=0;i<n;i++)
       {
              cin>>S.elem[i];
              S.top++;
       }
}
 


void destroyStack_Sq(sqStack S)
{
       delete []S.elem;
       S.top=-1;
       S.stackSize=0;
}
 


void creatStack_Sq(sqStack &S,int n)
{
       initStack_Sq(S);
       for(int i=0;i<n;i++)
       {
              cin>>S.elem[i];
              S.top++;
       }
}
 


void destroyStack_Sq(sqStack S)
{
       delete []S.elem;
       S.top=-1;
       S.stackSize=0;
}
 


void push(sqStack &S,char x)
{
       if(S.top==Stack_Size-1)
              cout<<"Stack Overflow!";
       S.elem[++S.top]=x;
}
 


char pop(sqStack &S)
{
       char x;
       if(S.top==-1)
              cout<<"Stack Empty!";
       x=S.elem[S.top--];
       return x;
}
 
void main()
{
       sqStack S;
       creatStack_Sq(S,5);
       push(S,'a');
       cout<<pop(S)<<endl;
       destroyStack_Sq(S);
}
0 0
原创粉丝点击