-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicList.cpp
More file actions
68 lines (59 loc) · 1.83 KB
/
MusicList.cpp
File metadata and controls
68 lines (59 loc) · 1.83 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
#include "MusicList.h"
// Default constructor
MusicList::MusicList() : head(nullptr), tail(nullptr), size(0) {}
// Add track to the end of the list
void MusicList::addMusic(const std::string& track) {
MusicNode* newMusic = new MusicNode(track); // Create new node
if (head == nullptr) { // Checking for empty list
head = tail = newMusic;
}
else {
tail->next = newMusic; // Adding to a non-empty list
tail = newMusic;
}
size++; // Increment the size
}
// Remove from the head of the list and return the first track
std::string MusicList::removeMusic() {
if (isEmpty()) {
throw std::out_of_range("The music list is empty"); // Use exception handling
}
MusicNode* temp = head; // Store the current head
std::string track = head->musicTrack; // Get the track
head = head->next; // Move head to the next node
if (head == nullptr) { // If the list is now empty
tail = nullptr;
}
delete temp; // Free memory
size--; // Decrement list size
return track; // Return the track
}
// Check if the queue is empty
bool MusicList::isEmpty() const {
return head == nullptr;
}
// Method to get the current size of the queue
int MusicList::getSize() const {
return size;
}
// Method to read and print all music tracks
void MusicList::listAllMusic() const {
if (head == nullptr) {
std::cout << "No music in the list." << std::endl;
return;
}
MusicNode* current = head; // Start from the head of the linked list
// Iterate through the linked list until the end is reached
while (current) {
std::cout << current->musicTrack << std::endl;
current = current->next;
}
}
// Destructor
MusicList::~MusicList() {
while (head != nullptr) {
MusicNode* temp = head;
head = head->next;
delete temp;
}
}