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

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

#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;
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值