3335 数据结构实验之栈八:栈的基本操作

来源:互联网 发布:淘宝直通车无线端推广 编辑:程序博客网 时间:2024/06/06 19:41

数据结构实验之栈八:栈的基本操作

#include<cstdlib>#include<iostream>#include<cstdio>#include<stdio.h>using namespace std;typedef int ET;typedef struct{    int *top;    int *base;    int stacksize;}stack;int CreatStack(stack &s,int n){   s.base=(int*)malloc(n*sizeof(int));   if(!s.base)   exit(0);   s.top=s.base;   s.stacksize=n;   return 1;}int empty(stack &s){    if(s.base==s.top)        return 1;    else        return 0;}void push (stack &s,int e ){   /* if(s.top-s.base>s.stacksize)    {        s.base=(ET*)malloc((stackmax+stacknum)*sizeof(ET));        if(!s.base)            exit(0);        s.top=s.base+s.stacksize;        s.stacksize+=stacknum;    }  */    *s.top++=e;}void pop(stack &s){    s.top--;   cout<<*s.top<<endl;}void choose(stack &s,int n,char c) {     int x;         if(c=='A')            {              if(empty(s))                cout<<"E"<<endl;              else cout<<*(s.top-1)<<endl;            }         else if(c=='O')           {               if(empty(s))cout<<"E"<<endl;              else pop(s);           }         else if (c=='P')         {              cin>>x;              if(s.top-s.base>=n)                cout<<"F"<<endl;              else push(s,x);         } } int main() {     stack s;     int t,n,m;     char c;     cin>>t;     while(t--)     {         cin>>n>>m;         CreatStack(s,n);         while(m--)         {             cin>>c;          choose(s,n,c);         }         if(t)cout<<endl;     }        return 0; }
0 0
原创粉丝点击