-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkStack.c
193 lines (170 loc) · 5.02 KB
/
LinkStack.c
1
2
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// Created by Maylon on 2022/8/12.
//
#include "LinkStack.h"
void InitLinkStack(LinkStack *S) {
*S = NULL;
}
Status DestroyLinkStack(LinkStack *S) {
if (*S == NULL) {
return false;
}
LinkStackNode *p;
while (*S != NULL) {
p = *S;
*S = (*S)->next;
free(p);
}
return true;
}
Status LinkStack_Push(LinkStack *S, ElemType e) {
LinkStackNode *p = (LinkStackNode *) malloc(sizeof(LinkStackNode));
if (p == NULL) {
return false;
}
if (*S == NULL) {
*S = p;
(*S)->data = e;
(*S)->next = NULL;
}
else {
p->data = e;
p->next = *S;
*S = p;
}
return true;
}
Status LinkStack_Pop(LinkStack *S, ElemType *e) {
if (*S == NULL) {
return false;
}
LinkStackNode *p = *S;
*e = (*S)->data;
*S = (*S)->next;
free(p);
return true;
}
void LinkStack_Get_Top(LinkStack S, ElemType *e) {
*e = S->data;
}
Status LinkStack_Empty(LinkStack S) {
return S == NULL;
}
int LinkStack_Len(LinkStack S) {
int count = 0;
LinkStack p = S;
while (p != NULL) {
count++;
p = p->next;
}
return count;
}
void LinkStack_Traverse(LinkStack S, void(*visit)(ElemType e)) {
LinkStack p = S;
while (p != NULL) {
visit(p->data);
p = p->next;
}
printf("NULL!\n");
}
void linkstack_menu(void) {
int choice;
LinkStack S = NULL;
ElemType e;
Status result;
do {
linkstack_menu_show_details();
choice = judge_int();
system("cls");
switch (choice) {
case 0: // exit
break;
case 1: // Initialize
InitLinkStack(&S);
printf("Succeeded!\n");
break;
case 2: // Destroy
if (S == 0) {
printf("The stack is already NULL!\n");
}
else {
if (DestroyLinkStack(&S) == true) {
printf("Succeeded!\n");
}
else {
printf("Failed!\n");
}
}
break;
case 3: // Push stack
get_input_element(&e);
system("cls");
result = LinkStack_Push(&S, e);
if (result == true) {
printf("Succeeded!\n");
}
else {
printf("Failed!\n");
}
break;
case 4: // Pop stack
if (LinkStack_Empty(S) == true) {
printf("The stack is NULL!\n");
}
else {
result = LinkStack_Pop(&S, &e);
if (result == true) {
printf("Successfully deleted data: %d\n", e);
}
else {
printf("Failed!\n");
}
}
break;
case 5: // Get top
if (LinkStack_Empty(S) == true) {
printf("The stack is NULL!\n");
}
else {
LinkStack_Get_Top(S, &e);
printf("Stack Top data: %d\n", e);
}
break;
case 6: // Get length
printf("Stack length: %d\n", LinkStack_Len(S));
break;
default:
printf("Wrong input, please re-enter!\n");
break;
}
if (choice >= 1 && choice <= 6) {
printf("Current stack (from top to bottom): ");
LinkStack_Traverse(S, visit);
}
} while (choice != 0);
// release memory
if (S != 0) {
DestroyLinkStack(&S);
}
}
void linkstack_menu_show_details(void) {
printf("\n");
printf("\t********************************************\n");
printf("\t* Linked Stack (without head) *\n");
printf("\t*------------------------------------------*\n");
printf("\t* 1 | Initialize (Reset) *\n");
printf("\t*------------------------------------------*\n");
printf("\t* 2 | Destroy *\n");
printf("\t*------------------------------------------*\n");
printf("\t* 3 | Push stack *\n");
printf("\t*------------------------------------------*\n");
printf("\t* 4 | Pop stack *\n");
printf("\t*------------------------------------------*\n");
printf("\t* 5 | Get top *\n");
printf("\t*------------------------------------------*\n");
printf("\t* 6 | Get length *\n");
printf("\t*------------------------------------------*\n");
printf("\t* 0 | Back *\n");
printf("\t********************************************\n");
printf("\nPlease enter the corresponding number(0-6): ");
}