You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
983 B

2 years ago
#include <stdio.h>
#include <stdlib.h>
2 years ago
typedef struct LinkStack {
2 years ago
char data;
struct LinkStack *next;
} LinkStack;
2 years ago
LinkStack *push(LinkStack *top, char a) {
LinkStack *line = (LinkStack *) malloc(sizeof(LinkStack));
2 years ago
line->data = a;
line->next = top;
top = line;
return top;
}
2 years ago
LinkStack *pop(LinkStack *top) {
if (top) {
2 years ago
LinkStack *p = top;
top = top->next;
2 years ago
printf("old top = %c; ", p->data);
if (top) {
printf("new top = %c\n", top->data);
} else {
2 years ago
printf("empty\n");
}
free(p);
2 years ago
} else {
2 years ago
printf("Stack Empty\n");
return top;
}
return top;
}
2 years ago
int stackEmpty(LinkStack *top) {
if (top == NULL) {
return 1; //链栈为空
} else {
return 0; // 链栈不为空
}
}
char getTop(LinkStack* top) {
if (stackEmpty(top)) {
printf("stack empty");
exit(0);
} else {
return top->data;
}
}