Hrbust-1939(栈)

#include<stdio.h>
#include<string.h>
#include<stack>
#include<iostream>
using namespace std;

int main()
{
    int T;
    while(scanf("%d",&T)!=EOF)
    {
        getchar();
        stack<int>q;
        while(T--)
        {
            char s[20];
            scanf("%s",s);
            if(strcmp(s,"push")==0)
            {
                int e;
                scanf("%d",&e);
                q.push(e);
            }
            if(strcmp(s,"pop")==0)
            {
                if(q.empty())
                    printf("this is empty!\n");
                else
                {
                    cout << q.top() << endl;
                    q.pop();
                }
            }
            if(strcmp(s,"query")==0)
            {
                if(q.empty())
                    printf("this is empty!\n");
                else
                {
                    while(!q.empty())
                    {
                        cout << q.top() << endl;
                        q.pop();
                    }
                }
            }
        }
        printf("\n");
    }
}
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define maxsize 100

typedef struct stack
{
    int data[maxsize];
    int top;
} Stack;
Stack *q;

void Initstack(Stack *q)
{
    q->top=-1;
}

void pushstack(Stack *q,int e)
{
    q->top++;
    q->data[q->top]=e;
}

void popstack(Stack *q)
{
    if(q->top==-1)
        printf("this is empty!\n");
    else
    {
        int e=q->data[q->top];
        printf("%d\n",e);
        q->top--;
    }
}

void query(Stack *q)
{
    if(q->top==-1)
        printf("this is empty!\n");
    else
    {
        while(q->top>=0)
        {
            printf("%d\n",q->data[q->top]);
            q->top--;
        }
    }
}

int main()
{
    int T;
    while(scanf("%d",&T)!=EOF)
    {
        getchar();
        q=(Stack *)malloc(sizeof(Stack));
        Initstack(q);
        while(T--)
        {
            char s[20];
            scanf("%s",s);
            if(strcmp(s,"push")==0)
            {
                int e;
                scanf("%d",&e);
                pushstack(q,e);
            }
            if(strcmp(s,"pop")==0)
            {
                popstack(q);
            }
            if(strcmp(s,"query")==0)
            {
                query(q);
            }
        }
        free(q);
        printf("\n");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值