-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path05_deque_v1.c
272 lines (231 loc) · 6.06 KB
/
05_deque_v1.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
Project #05 (CSeries)
05_DEQUE_V1.C
-------------
Description:
This is a deque (en).
It transform the Project 04 code in English.
It form a deque of integer.
It's purpose is to implementation of Project #10 - MUSIC ALBUM APP!
Deque_v1 or Double Ended Queue is a generalized version of Queue data structure
that allows insert and delete at both ends.
Supported Operations:
insertFront(): Adds an item at the front of Deque.
insertLast(): Adds an item at the rear of Deque.
insertMid(): Adds an item at the middle of deque.
deleteFront(): Deletes an item from front of Deque.
deleteLast(): Deletes an item from rear of Deque.
removeFromDeque(int): removes an item from entering position (initial is 1)
displayDeque(): Displays a list of itens in console.
TODO:
in the future (V2), we will also support these following operations:
getFront(): Gets the front item from queue.
getRear(): Gets the last item from queue.
isEmpty(): Checks whether Deque is empty or not.
isFull(): Checks whether Deque is full or not.
BUG KNOWN:
Display empty item
Remove duplicated items or Avoid entering duplicated items
Applications of Deque:
Since Deque supports both stack and queue operations, it can be used as both.
The Deque data structure supports clockwise and anticlockwise rotations
in O(1) time which can be useful in certain applications.
This Deque supports these operations:
*****************************************************************************
Output:
_____________________________________
1.Insert at beginning of deque
2.Insert at end of deque
3.Insert at middle of deque
4.Remove from deque
5.Display deque items
6.Exit
_____________________________________
Which operation you chose? 5
0 1 2 3
*****************************************************************************
Edited By: J3
Date: Nov, 2021
*/
#include<stdio.h>
#include<stdlib.h>
int menu();
void insertFront(int num);
void insertLast(int num);
void insertMid(int num, int position);
int removeFromDeque(int num);
void displayDeque();
struct SimpleDeque_Item {
int data;
struct SimpleDeque_Item *next;
} *head;
int main() {
int op, num, pos, c, res;
head = NULL;
while (1) {
op = menu();
switch (op) {
case 1:
printf("Enter the desired number: ");
scanf_s("%d", &num);
while ((c = getchar()) != '\n' && c != EOF) {}
insertFront(num);
break;
case 2:
printf("Enter the desired number: ");
scanf_s("%d", &num);
while ((c = getchar()) != '\n' && c != EOF) {}
insertLast(num);
break;
case 3:
printf("Enter the desired number: ");
scanf_s("%d", &num);
while ((c = getchar()) != '\n' && c != EOF) {}
printf("Enter the position you want to insert: ");
scanf_s("%d", &pos);
while ((c = getchar()) != '\n' && c != EOF) {}
insertMid(num, pos);
break;
case 4:
printf("Enter the number to be remove from Dequed: ");
scanf_s("%d", &num);
while ((c = getchar()) != '\n' && c != EOF) {}
res = removeFromDeque(num);
if (res == 1)
printf("Number removed from Deque!");
else
printf("Number not found!");
break;
case 5:
displayDeque();
break;
case 6:
return 0;
default:
printf("Invalid Number!\n");
}
}
return 0;
}
int menu() {
int op, c;
system("Cls");
printf("This Deque supports these operations:\n");
printf("_____________________________________\n\n");
printf("1.Insert at beginning of deque\n");
printf("2.Insert at end of deque\n");
printf("3.Insert at middle of deque\n");
printf("4.Remove from deque\n");
printf("5.Display deque items\n");
printf("6.Exit\n");
printf("_____________________________________\n");
printf("Which operation you chose? ");
scanf_s("%d", &op);
while ((c = getchar()) != '\n' && c != EOF) {}
system("Cls");
return op;
}
void insertFront(int num)
{
struct SimpleDeque_Item *NewItem;
NewItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
NewItem->data = num;
if (head == NULL)
{
head = NewItem;
head->next = NULL;
}
else
{
NewItem->next = head;
head = NewItem;
}
}
void insertLast(int num)
{
struct SimpleDeque_Item *NewItem;
NewItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
struct SimpleDeque_Item *ScanItem;
ScanItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
NewItem->data = num;
if (head == NULL)
{
head = NewItem;
head->next = NULL;
}
else
{
ScanItem = head;
while (ScanItem->next != NULL)
ScanItem = ScanItem->next;
ScanItem->next = NewItem;
NewItem->next = NULL;
}
}
void insertMid(int num, int position)
{
struct SimpleDeque_Item *NewItem;
NewItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
struct SimpleDeque_Item *ScanItem;
ScanItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
struct SimpleDeque_Item *AuxiliaryItem;
AuxiliaryItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
NewItem->data = num;
if (position == 0)
{
head = NewItem;
head->next = NULL;
}
else
{
ScanItem = head;
for (int i = 0; i < position - 1; i++)
ScanItem = ScanItem->next;
AuxiliaryItem = ScanItem->next;
ScanItem->next = NewItem;
NewItem->next = AuxiliaryItem;
}
}
int removeFromDeque(int num)
{
struct SimpleDeque_Item *ScanItem;
ScanItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
struct SimpleDeque_Item *Anterior;
Anterior = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
ScanItem = head;
while (ScanItem != NULL) {
if (ScanItem->data == num) {
if (ScanItem == head) {
head = ScanItem->next;
free(ScanItem);
return 1;
}
else {
Anterior->next = ScanItem->next ;
free(ScanItem);
return 1;
}
}
else {
Anterior = ScanItem;
ScanItem = ScanItem->next;
}
}
return 0;
}
void displayDeque()
{
struct SimpleDeque_Item *ScanItem;
ScanItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
ScanItem = head;
if (ScanItem == NULL) {
return;
}
while (ScanItem != NULL) {
printf("%d ", ScanItem->data);
ScanItem = ScanItem->next;
}
printf("\n");
system("pause");
return;
}