J.H


私信TA

用户名:dotcpp0649969

访问量:3228

签 名:

等  级
排  名 78
经  验 9375
参赛次数 1
文章发表 135
年  龄 0
在职情况 学生
学  校 桂林理工大学
专  业 计算机科学与技术

  自我简介:

TA的其他文章

解题思路:

注意事项:

参考代码:

#include <stdio.h>

#include <stdlib.h>

 

typedef struct linkedstack {

    int data;

    struct linkedstack* next;

} Linkedstack;

 

Linkedstack* create_linkedstack() {

    Linkedstack* top = (Linkedstack*)malloc(sizeof(Linkedstack));

    if (top == NULL)

        return NULL;

    top->next = NULL;

    return top;

}

 

int empty_linkedstack(Linkedstack* top) {

    if (top->next == NULL)

        return 1;

    else

        return 0;

}

 

int push_linkedstack(Linkedstack* top, int x) {

    Linkedstack* s = (Linkedstack*)malloc(sizeof(Linkedstack));

    if (s == NULL)

        return 0;

    s->data = x;

    s->next = top->next;

    top->next = s;

    return 1;

}

 

int pop_linkedstack(Linkedstack* top) {

    if (empty_linkedstack(top))

        return 0;

    Linkedstack* node = top->next;

    top->next = node->next;

    free(node);

    return 1;

}

 

int print_top_linkedstack(Linkedstack* top) {

    if (empty_linkedstack(top)) {

        printf("E\n");

        return 0;

    } else {

        int x = top->next->data;

        printf("%d\n", x);

        return 1;

    }

}

 

int main() {

    int n, x;

    char c;

    Linkedstack* top;

    while (scanf("%d", &n) != EOF) {

        getchar();

        top = create_linkedstack();

        while (n--) {

            scanf(" %c", &c); // Notice the space before %c to skip whitespace

            if (c == 'P') {

                scanf("%d", &x);

                push_linkedstack(top, x);

            } else if (c == 'O') {

                pop_linkedstack(top);

            } else if (c == 'A') {

                print_top_linkedstack(top);

            }

        }

        while (!empty_linkedstack(top)) {

            pop_linkedstack(top);

        }

        printf("\n");

        free(top);

    }

    return 0;

}


 

0.0分

0 人评分

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

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

代码解释器

代码纠错

SQL生成与解释

  评论区