栈的基本操作

数据结构实验之栈八:栈的基本操作
Time Limit: 1000MS Memory limit: 65536K
题目描述
堆栈是一种基本的数据结构。堆栈具有两种基本操作方式,push 和 pop。push一个值会将其压入栈顶,而 pop 则会将栈顶的值弹出。现在我们就来验证一下堆栈的使用。

输入
首先输入整数t(1 <= t <= 10),代表测试的组数,以后是 t 组输入。
对于每组测试数据,第一行输入两个正整数 m(1 <= m <= 100)、n(1 <= n <= 1000),其中m代表当前栈的最大长度,n代表本组测试下面要输入的操作数。 而后的 n 行,每行的第一个字符可能是'P’或者'O’或者'A’;如果是'P’,后面还会跟着一个整数,表示把这个数据压入堆栈;如果是'O’,表示栈顶元素出栈;如果是'A',表示询问当前栈顶的值'。

输出
对于每组测试数据,根据其中的命令字符来处理堆栈;
(1)对所有的'P'操作,如果栈满输出'F',否则完成压栈操作;
(2)对所有的'A'操作,如果栈空,则输出'E',否则输出当时栈顶的值;
(3)对所有的'O'操作,如果栈空,则输出'E',否则输出栈顶元素的值,并让其出栈;
每个输出占据一行,每组测试数据(最后一组除外)完成后,输出一个空行。

示例输入
2
5 10
A
P 9
A
P 6
P 3
P 10
P 8
A
P 2
O
2 5
P 1
P 3
O
P 5
A
示例输出
E
9
8
F
8

3
5
提示
建议: 用串的方式(%s)读入操作字符。
# include <bits/stdc++.h>
//# include <stdio.h>
//# include <stdlib.h>
# define STACK_INIT_SIZE 100
using namespace std;
typedef int SelemType;
typedef struct{
	SelemType *base;
	SelemType *top;
	int stacksize;
} SqStack;
void InitStack(SqStack&S);
void Push(SqStack&S,SelemType e);
void Pop(SqStack&S,SelemType&e);
/*栈空返回ture*/
bool StackEmpty(SqStack&S);
void GetTop(SqStack&S,SelemType&e);
int main()
{
    SqStack S;
    int t,m,n,key,etop,epop;
    char op;
    //scanf("%d",&t);
    cin>>t;
    InitStack(S);
    while(t--)
    {
        S.top = S.base;
        //scanf("%d %d",&m,&n);
        cin>>m>>n;
        while(n--)
        {
            //scanf("%c",&op);
            cin>>op;
            if(op == 'P')
            {
               // scanf("%d",&key);
               cin>>key;
                if(S.top - S.base < m)
                    Push(S,key);
                else
                    printf("F\n");
            }
             if(op == 'A')
            {
                if(!StackEmpty(S))
                {
                    GetTop(S,etop);
                    printf("%d\n",etop);
                }
                else
                {
                    printf("E\n");
                }
            }
            if(op == 'O')
            {
                if(StackEmpty(S))
                    printf("E\n");
                else
                {
                    GetTop(S,etop);
                    printf("%d\n",etop);
                    Pop(S,epop);
                }
            }
        }
        if(t)
        printf("\n");
    }
	return 0;
}

void InitStack(SqStack&S)
{
	S.base = (SelemType*)malloc(STACK_INIT_SIZE*sizeof(SelemType));
	if(!S.base)
	{
		exit(0);
	}
	S.top = S.base;
	S.stacksize = STACK_INIT_SIZE;
}

void Push(SqStack&S,SelemType e)
{
	/*放入添加的元素*/
	*S.top = e;
	/*栈顶S.top上移*/
	S.top++;
}

void Pop(SqStack&S,SelemType&e)
{
	/*判断栈是否为空,若为空直接返回*/
	if(S.top == S.base)
		return;
	else
	{
		/*栈顶下移找到要Pop的元素*/
		S.top--;
		/*获取要出栈的元素*/
		e = *S.top;
	}
}
bool StackEmpty(SqStack&S)
{
	if(S.top == S.base)
		return true;
	else
		return false;
}
void GetTop(SqStack&S,SelemType&e)
{
	if(S.top == S.base)
        return;
    else
        e = *(S.top - 1);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值