| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 
 | #define _CRT_SECURE_NO_WARNINGS
 #include <stdio.h>
 #include <stdlib.h>
 
 
 typedef int ElemType;
 typedef struct Node
 {
 ElemType data;
 struct Node* next;
 } Node, * LinkStack;
 
 
 LinkStack InitLinkStack();
 
 int StackEmpty(LinkStack S);
 
 int StackLength(LinkStack S);
 
 int PushLinkStack(LinkStack S, ElemType e);
 
 int PopLinkStack(LinkStack S, ElemType* e);
 
 void PrintLinkStack(LinkStack S);
 
 void ClearLinkStack(LinkStack S);
 
 int GetTopLinkStack(LinkStack S, ElemType* e);
 
 
 int main(void)
 {
 ElemType e;
 printf("初始化链栈成功!\n");
 LinkStack S = InitLinkStack();
 int i = 0;
 printf("\n执行9次入栈操作!\n");
 for (i = 0; i < 9; i++)
 {
 PushLinkStack(S, i + 1);
 }
 printf("执行完毕!当前栈长度为:%d\n打印当前栈:", StackLength(S));
 PrintLinkStack(S);
 printf("\n执行4次出栈操作!\n");
 for (i = 0; i < 4; i++)
 {
 PopLinkStack(S, &e);
 printf("第%d次:栈顶元素%d出栈成功!\n", i + 1, e);
 }
 printf("执行完毕!当前栈长度为:%d\n打印当前栈:", StackLength(S));
 PrintLinkStack(S);
 GetTopLinkStack(S, &e);
 printf("\n执行取栈顶元素操作:当前栈顶元素为:%d\n", e);
 printf("\n执行清空栈操作!\n");
 ClearLinkStack(S);
 if (StackEmpty(S)) printf("执行完毕!当前栈为空!\n");
 else printf("清空栈失败!\n");
 return 0;
 }
 
 
 LinkStack InitLinkStack()
 {
 LinkStack S = (LinkStack)malloc(sizeof(Node));
 S->next = NULL;
 return S;
 }
 
 
 int StackEmpty(LinkStack S)
 {
 if (S == NULL) return 0;
 return S->next == NULL;
 }
 
 
 int StackLength(LinkStack S)
 {
 int len = 0;
 if (StackEmpty(S)) return 0;
 
 Node* p = S->next;
 while (p != NULL)
 {
 len++;
 p = p->next;
 }
 return len;
 }
 
 
 int PushLinkStack(LinkStack S, ElemType e)
 {
 
 Node* newNode = (Node*)malloc(sizeof(Node));
 
 if (!newNode) return 0;
 
 newNode->data = e;
 newNode->next = S->next;
 S->next = newNode;
 
 return 1;
 }
 
 
 int PopLinkStack(LinkStack S, ElemType* e)
 {
 
 if (StackEmpty(S)) return 0;
 
 Node* top = S->next;
 
 *e = top->data;
 S->next = top->next;
 
 free(top);
 
 return 1;
 }
 
 
 void PrintLinkStack(LinkStack S)
 {
 
 Node* p = S->next;
 while (p)
 {
 printf("%d ", p->data);
 p = p->next;
 }
 printf("\n");
 }
 
 
 void ClearLinkStack(LinkStack S)
 {
 Node* p = S->next;
 while (p)
 {
 Node* q = p;
 p = p->next;
 free(q);
 }
 S->next = NULL;
 }
 
 
 int GetTopLinkStack(LinkStack S, ElemType* e)
 {
 
 if (StackEmpty(S)) return 0;
 else
 {
 *e = S->next->data;
 
 return 1;
 }
 }
 
 |