-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment-3
More file actions
148 lines (130 loc) · 3.84 KB
/
Copy pathAssignment-3
File metadata and controls
148 lines (130 loc) · 3.84 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
#include <iostream>
using namespace std;
class Node {
public:
string song;
Node *next;
Node() {
song = "";
next = NULL;
}
Node(string s) {
song = s;
next = NULL;
}
};
class PlayList {
Node *head;
public:
PlayList() : head(NULL) {} // Initialize head to NULL
void AddSong(string s) {
Node *nn = new Node(s);
if (head == NULL) {
head = nn; // Set head to the new node if list is empty
} else {
nn->next = head; // Insert new node at the beginning
head = nn; // Update head to new node
}
}
void DeleteSong(int pos) {
if (head == NULL) { // List is empty
cout << "List is empty. No song to delete." << endl;
return;
}
Node* temp = head;
if (pos == 1) { // Deleting the head node
head = temp->next; // Move head to the next node
delete temp; // Free memory
cout << "Deleted song at position " << pos << endl;
return;
}
// Traverse to the node just before the desired position
int currpos = 1;
while (temp != NULL && currpos < pos - 1) {
temp = temp->next;
currpos++;
}
// If the position is beyond the length of the list
if (temp == NULL || temp->next == NULL) {
cout << "Position does not exist." << endl;
return;
}
// Node to be deleted
Node* toDelete = temp->next;
temp->next = toDelete->next; // Unlink the node from the list
delete toDelete; // Free memory
cout << "Deleted song at position " << pos << endl;
}
void DisplayPlayList() {
Node *temp = head;
if (temp == NULL) {
cout << "Playlist is empty." << endl;
return;
}
while (temp != NULL) {
cout << temp->song << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
void DisplaySpecificSongs(int pos) {
Node *temp = head;
int currpos = 1;
while (temp != NULL && currpos < pos) {
temp = temp->next;
currpos++;
}
// Check if the position is valid
if (temp == NULL) {
cout << "Position does not exist." << endl;
} else {
cout << "Song at position " << pos << ": " << temp->song << endl;
}
}
};
int main() {
PlayList pl;
int ch;
char agree = 'Y';
do {
cout << "---MENU---";
cout << "\n1. Add songs";
cout << "\n2. Delete Songs";
cout << "\n3. Display the entire playlist";
cout << "\n4. Play specific songs";
cout << "\nChoose from the above: ";
cin >> ch;
cin.ignore(); // Clear the newline character from input buffer
switch (ch) {
case 1: {
string song_name;
cout << "\nEnter the song name: ";
getline(cin, song_name); // Use getline to allow multi-word names
pl.AddSong(song_name);
break;
}
case 2: {
int pos;
cout << "\nEnter the position where you want to delete: ";
cin >> pos;
pl.DeleteSong(pos);
break;
}
case 3:
pl.DisplayPlayList();
break;
case 4: {
int pos;
cout << "\nWhich song do you want to play? ";
cin >> pos;
pl.DisplaySpecificSongs(pos);
break;
}
default:
cout << "\nInvalid choice!" << endl;
}
cout << "\nDo you want to continue? (Y/N): ";
cin >> agree;
} while (agree == 'y' || agree == 'Y');
return 0;
}