-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSA PROJECT.c
233 lines (207 loc) · 4.68 KB
/
DSA PROJECT.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
//JOEL ANANTH A - 2021503022
//SONG QUEUE USING LINKED LIST
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedef struct Node
{
char song[100];
struct Node* next;
}*NODE;
NODE makeNode(char song[])
{
NODE t;
t=(NODE)malloc(sizeof(struct Node));
strcpy(t->song,song);
t->next=NULL;
return t;
}
void display(NODE l,int i)
{
if(l)
{
printf("%d) %s\n",i,l->song);
display(l->next,i+1);
}
}
NODE insertHead(NODE l,char song[])
{
NODE t=makeNode(song);
t->next=l;
return t;
}
NODE insertEnd(NODE l,char song[])
{
if(!l)
{
return makeNode(song);
}
l->next=insertEnd(l->next,song);
return l;
}
NODE insertAt(NODE l,char song[],int pos,int cp)
{
if(!l)
return makeNode(song);
if(cp==pos)
{
NODE t;
t=makeNode(song);
t->next=l;
return t;
}
l->next=insertAt(l->next,song,pos,cp+1);
return l;
}
NODE deleteHead(NODE l)
{
if(l)
return l->next;
}
NODE deleteEnd(NODE l)
{
if(!l)
return NULL;
if(!l->next)
return NULL;
NODE t=l;
while(t->next->next!=NULL)
t=t->next;
t->next=NULL;
return l;
}
NODE deleteAt(NODE l,int pos,int cp)
{
if(!l)
return NULL;
if(cp==pos)
return l->next;
l->next=deleteAt(l->next,pos,cp+1);
return l;
}
void currentSong(NODE l)
{
if(!l)
{
return NULL;
}
printf("%s\n\n",l->song);
}
int countSongs(NODE l)
{
int i=0;
while (l->next!=NULL)
{
l=l->next;
i++;
}
i++;
return i;
}
void disRandom(NODE l)
{
char a[100];
if(!l)
return;
srand(time(NULL));
strcpy(a,l->song);
NODE t= l;
int n;
for (n=2; t!=NULL; n++)
{
if (rand()%n==0)
strcpy(a,t->song);
t = t->next;
}
printf("Randomly selected Song is %s\n", a);
}
void main()
{
NODE n=NULL;
int c,x;
char a[100];
n=insertHead(n,"Azhagae");
n=insertHead(n,"wasted");
n=insertHead(n,"engaeyo");
while(1)
{
printf("1. Display song queue \n");
printf("2. Show current song \n");
printf("3. Add song to the queue\n");
printf("4. Add song to play next\n");
printf("5. Add song in between the queue \n");
printf("6. Delete last song from the queue \n");
printf("7. Delete current song \n");
printf("8. Delete song in between the queue \n");
printf("9. Number of songs in the queue \n");
printf("10. Play a song randomly \n");
printf("11. Exit \n\n");
printf("Enter your choice : ");
scanf("%d",&c);
printf("\n");
switch(c)
{
case 1:
printf("SONG QUEUE:\n");
display(n,1);
printf("\n\n\n");
break;
case 2:
printf("CURRENT SONG:\n");
currentSong(n);
printf("\n\n\n");
break;
case 3:
printf("Enter name of the song: ");
scanf("%s",a);
n=insertEnd(n,a);
printf("\n\n\n");
break;
case 4:
printf("Enter the name of the song: ");
scanf("%s",a);
n=insertAt(n,a,2,1);
printf("\n\n\n");
break;
case 5:
printf("Enter the name of the song: ");
scanf("%s",a);
printf("\n");
printf("Enter the position to be added to: ");
scanf("%d",&x);
n=insertAt(n,a,x,1);
printf("\n\n\n");
break;
case 6:
n=deleteEnd(n);
printf("Song has been deleted");
printf("\n\n\n");
break;
case 7:
n=deleteHead(n);
printf("Song has been deleted");
printf("\n\n\n");
break;
case 8:
printf("Enter the position of the song to be deleted to: ");
scanf("%d",&x);
n=deleteAt(n,x,1);
printf("Song has been deleted");
printf("\n\n\n");
break;
case 9:
printf("Number of songs in the queue is: \n");
printf("%d",countSongs(n));
printf("\n\n\n");
break;
case 10:
disRandom(n);
printf("\n\n\n");
break;
case 11:
printf("\n\n\t\t\tThank You !\n\n\n");
exit(0);
}
}
}