-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
210 lines (164 loc) · 4.37 KB
/
main.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
/*
===Program: PA01
===Programmer: William Youse
===Date Due: 2/8/2023
===Purpose: To create a doubly linked list to (currently) store 'songs', travsersable, able to add and delete.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "song.h"
//temp variables for user input
int userInput = 0;
char artName[4096], songName[4096];
int songLength = 0;
//creates a new song node, throws in data collected from user, ammends linked list at the end
void insertEnd(struct song** head, char *artName, char *songName, int time){
struct song* newNode = (struct song*) malloc(sizeof(struct song));
newNode->artist = malloc(strlen(artName) + 1);
newNode->name = malloc(strlen(songName) + 1);
strcpy(newNode->artist, artName);
strcpy(newNode->name, songName);
newNode->length = time;
newNode->next = NULL;
newNode->prev = NULL;
if(*head==NULL){
*head = newNode;
return;
}
struct song *temp = *head;
while(temp->next!=NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
//allows for a single node deletion/ammend after deletion
void deleteUnit(struct song** head, struct song* temp)
{
if (*head == NULL || temp == NULL) {
return;
}
if (*head == temp) {
*head = temp->next;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}
free(temp);
}
void deleteArtist(struct song** head, char *artistCheck)
{
if ((*head) == NULL)
return;
struct song* ptr = *head;
struct song* next;
while (ptr != NULL) {
if (strcmp(ptr->artist, artistCheck) == 0) {
next = ptr->next;
deleteUnit(head, ptr);
ptr = next;
} else {
ptr = ptr->next;
}
}
}
void deleteSong(struct song **head, char *songCheck)
{
if ((*head) == NULL)
return;
struct song *ptr = *head;
struct song *next;
while (ptr != NULL) {
if (strcmp(ptr->name, songCheck) == 0) {
next = ptr->next;
deleteUnit(head, ptr);
ptr = next;
} else {
ptr = ptr->next;
}
}
}
void deleteAll(struct song** head)
{
if ((*head) == NULL)
return;
struct song* ptr = *head;
struct song* next;
while (ptr != NULL) {
if (ptr->length >= 0) {
next = ptr->next;
deleteUnit(head, ptr);
ptr = next;
} else {
ptr = ptr->next;
}
}
}
void displayList(struct song *hold) {
struct song *last;
while (hold != NULL) {
printf("Artist: %s\n", hold->artist);
printf("Song: %s\n", hold->name);
printf("Time: %d seconds\n", hold->length);
printf("----------\n");
last = hold;
hold = hold->next;
}
}
int main() {
struct song *head = NULL;
while (userInput != -99) {
strcpy(artName, "");
strcpy(songName, "");
printf("=====Song Menu=====\n");
printf("\n");
printf("1) Add a Song\n");
printf("2) Delete a song\n");
printf("3) Delete an Artist\n");
printf("4) Print Songs\n");
printf("5) Exit\n\n");
printf("Choice: ");
scanf("%d", &userInput);
/*!!!!!REQUIRED - INPUT NON-FUNCTIONAL WITHOUT IT!!!!!*/
/* (Maybe I should not use scanf for userInput) */
int c;
while ((c = getchar()) != '\n' && c != EOF);
switch (userInput)
{
case (1):
printf("Enter Artists Name: ");
fgets(artName, sizeof(artName), stdin);
printf("Enter Songs Name: ");
fgets(songName, sizeof(songName), stdin);
printf("Enter Songs Length (in seconds): ");
scanf("%d", &songLength);
insertEnd(&head, artName, songName, songLength);
break;
case (2):
printf("Enter song name: ");
fgets(songName, sizeof(songName), stdin);
deleteSong(&head, songName);
break;
case (3):
printf("Enter artist name: ");
fgets(artName, sizeof(artName), stdin);
deleteArtist(&head, artName);
break;
case (4):
displayList(head);
break;
case (5):
printf("Exiting Program, hope you enjoyed!\n");
deleteAll(&head);
userInput = -99;
break;
default:
break;
}
}
return 0;
}