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
50 lines
983 B
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
typedef struct LinkStack {
|
|
char data;
|
|
struct LinkStack *next;
|
|
} LinkStack;
|
|
|
|
LinkStack *push(LinkStack *top, char a) {
|
|
LinkStack *line = (LinkStack *) malloc(sizeof(LinkStack));
|
|
line->data = a;
|
|
line->next = top;
|
|
top = line;
|
|
return top;
|
|
}
|
|
|
|
LinkStack *pop(LinkStack *top) {
|
|
if (top) {
|
|
LinkStack *p = top;
|
|
top = top->next;
|
|
printf("old top = %c; ", p->data);
|
|
if (top) {
|
|
printf("new top = %c\n", top->data);
|
|
} else {
|
|
printf("empty\n");
|
|
}
|
|
free(p);
|
|
} else {
|
|
printf("Stack Empty\n");
|
|
return top;
|
|
}
|
|
return top;
|
|
}
|
|
|
|
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;
|
|
}
|
|
} |