Implement a music playlist manager using a
doubly linked list. Support play-next, playprevious, add song, and delete song.
Here is a simple C++ implementation of a Music Playlist Manager using a doubly linked list,
supporting the operations: play-next, play-previous, add song, and delete song.
AIM
To create a music playlist manager that allows navigation (next, previous), addition, and deletion
of songs using a doubly linked list.
THEORY
A doubly linked list allows traversal in both forward and backward directions, making it an ideal
structure for playlist navigation. Each node stores song information and pointers to previous and
next nodes for easy addition, deletion, and navigation of songs.
PROGRAM
#include <iostream>
#include <string>
using namespace std;
struct Song {
string title;
Song* prev;
Song* next;
Song(string t) : title(t), prev(nullptr), next(nullptr) {}
};
class Playlist {
Song* head;
Song* current;
public:
Playlist() : head(nullptr), current(nullptr) {}
void addSong(string title) {
Song* newSong = new Song(title);
if (!head) {
head = current = newSong;
return;
}
Song* temp = head;
while (temp->next)
temp = temp->next;
temp->next = newSong;
newSong->prev = temp;
}
void deleteSong(string title) {
Song* temp = head;
while (temp) {
if (temp->title == title)
break;
temp = temp->next;
}
if (!temp) {
cout << "Song \"" << title << "\" not found in the playlist.\n";
return;
}
if (temp->prev)
temp->prev->next = temp->next;
else
head = temp->next; // Deleted head
if (temp->next)
temp->next->prev = temp->prev;
// If current points to deleted song, move it
if (current == temp) {
current = temp->next ? temp->next : temp->prev;
}
delete temp;
cout << "Deleted song \"" << title << "\" from the playlist.\n";
}
void playNext() {
if (!current) {
cout << "Playlist empty.\n";
return;
}
if (current->next) {
current = current->next;
cout << "Playing next song: " << current->title << endl;
} else {
cout << "Already at the last song.\n";
}
}
void playPrevious() {
if (!current) {
cout << "Playlist empty.\n";
return;
}
if (current->prev) {
current = current->prev;
cout << "Playing previous song: " << current->title << endl;
} else {
cout << "Already at the first song.\n";
}
}
void showCurrentSong() {
if (!current) {
cout << "Playlist empty.\n";
} else {
cout << "Now playing: " << current->title << endl;
}
}
void displayPlaylist() {
if (!head) {
cout << "Playlist is empty.\n";
return;
}
Song* temp = head;
cout << "Playlist: ";
while (temp) {
cout << "\"" << temp->title << "\" ";
temp = temp->next;
}
cout << endl;
}
};
int main() {
Playlist playlist;
playlist.addSong("Song A");
playlist.addSong("Song B");
playlist.addSong("Song C");
playlist.addSong("Song D");
playlist.displayPlaylist();
playlist.showCurrentSong();
playlist.playNext();
playlist.playNext();
playlist.playPrevious();
playlist.deleteSong("Song B");
playlist.displayPlaylist();
playlist.playNext();
playlist.deleteSong("Song A");
playlist.displayPlaylist();
playlist.showCurrentSong();
return 0;
}
EXPECTED OUTPUT
Playlist: "Song A" "Song B" "Song C" "Song D"
Now playing: Song A
Playing next song: Song B
Playing next song: Song C
Playing previous song: Song B
Deleted song "Song B" from the playlist.
Playlist: "Song A" "Song C" "Song D"
Playing next song: Song C
Deleted song "Song A" from the playlist.
Playlist: "Song C" "Song D"
Now playing: Song C
This program manages a playlist with linked songs and supports navigation, addition, and
deletion operations effectively.