故我


私信TA

用户名:uq_37592642929

访问量:1439

签 名:

一起去啊,更远方的地方!!!!

等  级
排  名 728
经  验 3722
参赛次数 0
文章发表 53
年  龄 0
在职情况 学生
学  校 社会大学
专  业

  自我简介:

解题思路:

注意事项:

参考代码:

#include <stdio.h>

#include <stdlib.h>

#define len sizeof(Stack)

typedef struct s {

    int data[1000];

    int top;

}Stack;

void pushstack(Stack* h, int n) {

    h->top++;

    h->data[h->top] = n;

}

void popstack(Stack* h) {

    if (h->top == -1) {

        return NULL;

    }

    else {

        h->top--;

    }

}

void printstack(Stack* h) {

    if (h->top == -1) {

        printf("E\n");    //换行记得!!!

    }

    else {

        printf("%d\n", h->data[h->top]);

    }

}

int main() {

    Stack* h;

    h = (Stack*)malloc(len);

    h->top = -1;

    int n, t;

    char s;

    while (scanf("%d", &n)&&n){

        for (int i = 0; i < n; i++) {

            getchar();

            scanf("%c", &s);

            if (s == 'P') {

                scanf("%d", &t);

                pushstack(h, t);

            }

            else if (s == 'O') {

                popstack(h);

            }

            else if (s == 'A') {

                printstack(h);

            }

        }

        h->top=-1;     //每个测试样例过后,栈都清空

        printf ("\n");

    }

    return 0;

}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区