-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeqStack.c
225 lines (197 loc) · 6.33 KB
/
SeqStack.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//
// Created by Maylon on 2022/8/12.
//
#include "SeqStack.h"
Status InitSeqStack(SeqStack *S) {
(*S).data = (ElemType *) calloc(DeltaSize, sizeof(ElemType));
if ((*S).data == NULL) {
return false;
}
(*S).top = -1;
(*S).MaxSize = DeltaSize;
return true;
}
Status SeqStack_Increase_Capacity(SeqStack *S) {
if ((*S).data == NULL) {
return false;
}
ElemType *p = (*S).data;
(*S).data = (ElemType *) calloc((*S).MaxSize + DeltaSize, sizeof(ElemType));
if ((*S).data == NULL) {
return false;
}
for (int i = 0; i <= (*S).top; i++) {
(*S).data[i] = p[i];
}
(*S).MaxSize += DeltaSize;
free(p);
return true;
}
void DestroySeqStack(SeqStack *S) {
free((*S).data);
}
Status SeqStack_Push(SeqStack *S, ElemType e) {
char choice;
if ((*S).data == NULL) {
return false;
}
if ((*S).top + 1 == (*S).MaxSize) {
printf("The stack if full, do you want to increase the capacity?(y/n) ");
choice = get_choice();
system("cls");
if (choice == 'N' || choice == 'n') {
return false;
}
else if (choice == 'Y' || choice == 'y') {
if (SeqStack_Increase_Capacity(S) == false) {
printf("Failed to increase capacity!\n");
return false;
}
}
}
(*S).data[++(*S).top] = e;
return true;
}
Status SeqStack_Pop(SeqStack *S, ElemType *e) {
if ((*S).data == NULL || SeqStack_Empty((*S))) {
return false;
}
*e = (*S).data[(*S).top--];
return true;
}
void SeqStack_Get_Top(SeqStack S, ElemType *e) {
*e = S.data[S.top];
}
Status SeqStack_Empty(SeqStack S) {
return S.top == -1;
}
int SeqStack_Len(SeqStack S) {
return S.top + 1;
}
void SeqStack_Traverse(SeqStack S, void(*visit)(ElemType e)) {
for (int i = 0; i <= S.top; i++) {
visit(S.data[i]);
}
printf("NULL\n");
}
void seqstack_menu(void) {
int choice;
SeqStack S;
int init_flag = 0;
ElemType e;
Status result;
do {
seqstack_menu_show_details();
choice = judge_int();
system("cls");
switch (choice) {
case 0: // exit
break;
case 1: // Initialize
if (InitSeqStack(&S) == true) {
init_flag = 1;
printf("Succeeded!\n");
printf("Current stack (from bottom to top): NULL\n");
}
else {
printf("Failed!\n");
}
break;
case 2: // Destroy
if (init_flag == 0) {
printf("The stack isn't initialized!\n");
}
else {
DestroySeqStack(&S);
init_flag = 0;
printf("Succeeded!\n");
}
break;
case 3: // Push stack
if (init_flag == 0) {
printf("The stack isn't initialized!\n");
}
else {
get_input_element(&e);
system("cls");
result = SeqStack_Push(&S, e);
if (result == true) {
printf("Succeeded!\n");
}
else {
printf("Failed!\n");
}
}
break;
case 4: // Pop stack
if (init_flag == 0) {
printf("The stack isn't initialized!\n");
}
else if (SeqStack_Empty(S) == true) {
printf("The stack is NULL!\n");
}
else {
result = SeqStack_Pop(&S, &e);
if (result == true) {
printf("Successfully deleted data: %d\n", e);
}
else {
printf("Failed!\n");
}
}
break;
case 5: // Get top
if (init_flag == 0) {
printf("The stack isn't initialized!\n");
}
else if (SeqStack_Empty(S) == true) {
printf("The stack is NULL!\n");
}
else {
SeqStack_Get_Top(S, &e);
printf("Stack Top data: %d\n", e);
}
break;
case 6: // Get length
if (init_flag == 0) {
printf("The stack isn't initialized!\n");
}
else {
printf("Stack length: %d\n", SeqStack_Len(S));
}
break;
default:
printf("Wrong input, please re-enter!\n");
break;
}
if (choice >= 3 && choice <= 6 && init_flag != 0) {
printf("Current stack (from bottom to top): ");
SeqStack_Traverse(S, visit);
}
} while (choice != 0);
// release memory
if (init_flag != 0) {
DestroySeqStack(&S);
}
}
void seqstack_menu_show_details(void) {
printf("\n");
printf("\t********************************************\n");
printf("\t* Sequential Stack *\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): ");
}