-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path06_musicalbum_v1.c
More file actions
339 lines (285 loc) · 9.71 KB
/
06_musicalbum_v1.c
File metadata and controls
339 lines (285 loc) · 9.71 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
Project #05 (CSeries)
06_MUSICALBUM_V1.C
------------------
(Please, see cmplete project at: 10_MUSICALBUM_V2.C)
Description:
This is a deque. It implements a J3 MUSIC ALBUM!
Welcome!
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.
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.
******************************************************************************
OUTPUT:
This Deque supports these operations:
_____________________________________
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? 1
Enter the desired Track number: 1
Enter the Artist name: Queens
Enter the Music name: We Are The Champions
Enter the Duration of the track: 451
Which operation you chose? 5
_____________________________________
Track: 1
Artist: Queens
Music: We Are The Champions
Duration: 451
_____________________________________
******************************************************************************
UNINTER - ESCOLA SUPERIOR POLITECNICA
Edited By: J3
Date: Nov, 2021
*/
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int menu();
void insertFront(int track, char[], char[], int);
void insertLast(int track, char[], char[], int);
void insertMid(int track, char artist[50], char music[50], int duration, int position);
int removeFromDeque(int track);
void displayDeque();
struct SimpleDeque_Item {
int track;
char artist[50];
char music[50];
int duration;
struct SimpleDeque_Item *next;
} *head;
int main() {
int op, num, pos, c, res, track, duration;
char artist[50], music[50];
head = NULL;
while (1) {
op = menu();
switch (op) {
case 1: // 1.Insert at beginning of deque
printf("Enter the desired Track number: ");
scanf_s("%d", &track);
while ((c = getchar()) != '\n' && c != EOF) {}
printf("Enter the Artist name: ");
scanf_s("%[a-zA-z ]]",&artist);
while ((c = getchar()) != '\n' && c != EOF) {}
printf("Enter the Music name: ");
scanf_s("%[a-zA-z ]]",&music);
while ((c = getchar()) != '\n' && c != EOF) {}
printf("Enter the Duration of the track: ");
scanf_s("%d", &duration);
while ((c = getchar()) != '\n' && c != EOF) {}
insertFront(track, artist, music, duration);
break;
case 2: // 2.Insert at end of deque
printf("Enter the desired Track number: ");
scanf_s("%d", &track);
while ((c = getchar()) != '\n' && c != EOF) {}
printf("Enter the Artist name: ");
scanf_s("%[a-zA-z ]]",&artist);
while ((c = getchar()) != '\n' && c != EOF) {}
printf("Enter the Music name: ");
scanf_s("%[a-zA-z ]]",&music);
while ((c = getchar()) != '\n' && c != EOF) {}
printf("Enter the Duration of the track: ");
scanf_s("%d", &duration);
while ((c = getchar()) != '\n' && c != EOF) {}
insertLast(track, artist, music, duration);
break;
case 3: // 3.Insert at middle of deque
printf("Enter the desired Track number: ");
scanf_s("%d", &track);
while ((c = getchar()) != '\n' && c != EOF) {}
printf("Enter the Artist name: ");
scanf_s("%[a-zA-z ]]",&artist);
while ((c = getchar()) != '\n' && c != EOF) {}
printf("Enter the Music name: ");
scanf_s("%[a-zA-z ]]",&music);
while ((c = getchar()) != '\n' && c != EOF) {}
printf("Enter the Duration of the track: ");
scanf_s("%d", &duration);
while ((c = getchar()) != '\n' && c != EOF) {}
printf("Enter the position you want to insert the new track: ");
scanf_s("%d", &pos);
while ((c = getchar()) != '\n' && c != EOF) {}
insertMid(track, artist, music, duration, pos);
break;
case 4: // 4.Remove from deque
printf("Enter the desired Track number: ");
scanf_s("%d", &track);
while ((c = getchar()) != '\n' && c != EOF) {}
res = removeFromDeque(track);
if (res == 1)
printf("Track Number removed from Deque!");
else
printf("Track Number not found!");
break;
case 5: // 5.Display deque items
displayDeque();
break;
case 6: // 6.Exit
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 track, char artist[50], char music[50], int duration)
{
struct SimpleDeque_Item *NewItem;
NewItem = (struct SimpleDeque_Item *)malloc(sizeof(struct SimpleDeque_Item));
NewItem->track = track;
strcpy(NewItem->artist , artist);
strcpy(NewItem->music , music);
NewItem->duration = duration;
if (head == NULL)
{
head = NewItem;
head->next = NULL;
}
else
{
NewItem->next = head;
head = NewItem;
}
}
void insertLast(int track, char artist[50], char music[50], int duration)
{
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->track = track;
strcpy(NewItem->artist , artist);
strcpy(NewItem->music , music);
NewItem->duration = duration;
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 track, char artist[50], char music[50], int duration, 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->track = track;
strcpy(NewItem->artist , artist);
strcpy(NewItem->music , music);
NewItem->duration = duration;
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->track == 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("_____________________________________\n");
printf("Track: %i\n", ScanItem->track);
printf("Artist: %s\n", ScanItem->artist);
printf("Music: %s\n", ScanItem->music);
printf("Duration: %i\n", ScanItem->duration);
printf("_____________________________________\n");
ScanItem = ScanItem->next;
}
printf("\n");
system("pause");
return;
}