解题思路:
注意事项:
参考代码:
#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 人评分
回文数(一) (C语言代码)浏览:750 |
本人酷爱递归实现很多问题,这里也是浏览:556 |
WU-小九九 (C++代码)浏览:1684 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:660 |
1157题解浏览:715 |
蚂蚁感冒 (C语言代码)浏览:773 |
C语言程序设计教程(第三版)课后习题11.5 (C语言代码)浏览:1479 |
第三届阿里中间件性能挑战赛-总决赛亚军比赛攻略浏览:1145 |
WU-C语言程序设计教程(第三版)课后习题12.3 (C++代码)浏览:862 |
平方数问题,oj一直是wrong answer浏览:739 |