-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeqQueue.c
234 lines (204 loc) · 6.62 KB
/
SeqQueue.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
226
227
228
229
230
231
232
233
234
//
// Created by Maylon on 2022/9/2.
//
#include "SeqQueue.h"
Status InitSeqQueue(SeqQueue *Q) {
(*Q).data = (ElemType *) calloc(DeltaSize, sizeof(ElemType));
if ((*Q).data == NULL) {
return false;
}
(*Q).front = (*Q).rear = 0;
(*Q).MaxSize = DeltaSize;
return true;
}
Status SeqQueue_Increase_Capacity(SeqQueue *Q) {
if ((*Q).data == NULL) {
return false;
}
ElemType *p = (*Q).data;
(*Q).data = (ElemType *) calloc((*Q).MaxSize + DeltaSize, sizeof(ElemType));
if ((*Q).data == NULL) {
return false;
}
for (int i = (*Q).front; i != (*Q).rear; i = (i + 1) % (*Q).MaxSize) {
(*Q).data[i] = p[i];
}
(*Q).MaxSize += DeltaSize;
free(p);
return true;
}
void DestroySeqQueue(SeqQueue *Q) {
free((*Q).data);
}
Status EnSeqQueue(SeqQueue *Q, ElemType e) {
char choice;
if ((*Q).data == NULL) {
return false;
}
if (SeqQueue_Full(*Q) == true) {
printf("The queue is 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 (SeqQueue_Increase_Capacity(Q) == false) {
printf("Failed to increase capacity!\n");
return false;
}
}
}
(*Q).data[(*Q).rear] = e;
(*Q).rear = ((*Q).rear + 1) % (*Q).MaxSize;
return true;
}
Status DeSeqQueue(SeqQueue *Q, ElemType *e) {
if ((*Q).data == NULL || SeqQueue_Empty(*Q)) {
return false;
}
*e = (*Q).data[(*Q).front];
(*Q).front = ((*Q).front + 1) % (*Q).MaxSize;
return true;
}
void SeqQueue_Get_Head(SeqQueue Q, ElemType *e) {
*e = Q.data[Q.front];
}
Status SeqQueue_Empty(SeqQueue Q) {
return Q.rear == Q.front;
}
Status SeqQueue_Full(SeqQueue Q) {
return (Q.rear + 1) % Q.MaxSize == Q.front;
}
int SeqQueue_Len(SeqQueue Q) {
return (Q.rear + Q.MaxSize - Q.front) % Q.MaxSize;
}
void SeqQueue_Traverse(SeqQueue Q, void(*visit)(ElemType e)) {
int i = Q.front;
while (i != Q.rear) {
visit(Q.data[i]);
i = (i + 1) % Q.MaxSize;
}
printf("NULL\n");
}
void seqqueue_menu(void) {
int choice;
SeqQueue Q;
int init_flag = 0;
ElemType e;
Status result;
do {
seqqueue_menu_show_details();
choice = judge_int();
system("cls");
switch (choice) {
case 0: // exit
break;
case 1: // Initialize
if (InitSeqQueue(&Q) == true) {
init_flag = 1;
printf("Succeeded!\n");
printf("Current queue (from front to rear): NULL\n");
}
else {
printf("Failed!\n");
}
break;
case 2: // Destroy
if (init_flag == 0) {
printf("The stack isn't initialized!\n");
}
else {
DestroySeqQueue(&Q);
init_flag = 0;
printf("Succeeded!\n");
}
break;
case 3: // EnQueue
if (init_flag == 0) {
printf("The stack isn't initialized!\n");
}
else {
get_input_element(&e);
system("cls");
result = EnSeqQueue(&Q, e);
if (result == true) {
printf("Succeeded!\n");
}
else {
printf("Failed!\n");
}
}
break;
case 4: // DeQueue
if (init_flag == 0) {
printf("The stack isn't initialized!\n");
}
else if (SeqQueue_Empty(Q) == true) {
printf("The stack is NULL!\n");
}
else {
result = DeSeqQueue(&Q, &e);
if (result == true) {
printf("Successfully deleted data: %d\n", e);
}
else {
printf("Failed!\n");
}
}
break;
case 5: // Get head
if (init_flag == 0) {
printf("The stack isn't initialized!\n");
}
else if (SeqQueue_Empty(Q) == true) {
printf("The stack is NULL!\n");
}
else {
SeqQueue_Get_Head(Q, &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", SeqQueue_Len(Q));
}
break;
default:
printf("Wrong input, please re-enter!\n");
break;
}
if (choice >= 3 && choice <= 6 && init_flag != 0) {
printf("Current queue (from front to rear): ");
SeqQueue_Traverse(Q, visit);
}
} while (choice != 0);
// release memory
if (init_flag != 0) {
DestroySeqQueue(&Q);
}
}
void seqqueue_menu_show_details(void) {
printf("\n");
printf("\t********************************************\n");
printf("\t* Sequential Queue *\n");
printf("\t*------------------------------------------*\n");
printf("\t* 1 | Initialize (Reset) *\n");
printf("\t*------------------------------------------*\n");
printf("\t* 2 | Destroy *\n");
printf("\t*------------------------------------------*\n");
printf("\t* 3 | EnQueue *\n");
printf("\t*------------------------------------------*\n");
printf("\t* 4 | DeQueue *\n");
printf("\t*------------------------------------------*\n");
printf("\t* 5 | Get head *\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): ");
}