解题思路:
注意事项:
参考代码:
#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 人评分
2004年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:716 |
Biggest Number (C++代码)回溯法浏览:1678 |
弟弟的作业 (C++代码)浏览:1342 |
printf基础练习2 (C语言代码)浏览:605 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:545 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:1000 |
WU-输入输出格式练习 (C++代码)浏览:1133 |
【金明的预算方案】 (C++代码)浏览:873 |
完数 (C语言代码)浏览:757 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:580 |