1 #include<stdio.h>
2 #include<stdlib.h>
3
4 typedef struct Node
5 {
6 int data;
7 struct Node *next;
8 }Node,*pNode;
9
10 typedef struct Stack
11 {
12 int count;
13 pNode top;//注意:非 pNode *top
14 }LinkStack;
15
16 int Push(LinkStack *S,int e)
17 {
18 pNode s = (pNode)malloc(sizeof(Node)); //结构分为Node和pNode的用处
19 s->data = e;
20 s->next = S->top;
21 S->top = s;
22 S->count++;
23 return 0;
24 }
25
26 int Pop(LinkStack *S,int *e)
27 {
28 //注意:先判断栈是否为空
29 pNode p;
30 if(S->count == 0)
31 return 0;
32 *e = S->top->data;
33 p = S->top;
34 S->top=S->top->next;
35 free(p);//pop必须要释放节点
36 S->count--;
37 return 0;
38 }