69,371
社区成员
发帖
与我相关
我的任务
分享
typedef int elemType;
typedef struct stack {
elemType item;
struct stack *next;
} stack;
stack *predecessor_stack(stack *s) {
if (NULL == s->next) {
return NULL;
} else if (NULL == s->next->next) {
return s;
} else {
return predecessor_stack(s->next);
}
}
stack *pop(stack *s) {
stack *temp = s->next;
stack *pre = predecessor_stack(s);
if (NULL == pre) {
return NULL;
} else {
while (NULL != temp->next) {
temp = temp->next;
}
pre->next = NULL;
return temp;
}
}
void push(stack *s, elemType x) {
stack *new = (stack *) malloc(sizeof(struct stack));
new->next = NULL;
new->item = x;
while (NULL != s->next) {
s = s->next;
}
s->next = new;
}