#include <iostream>
using namespace std;
struct Song {
string tle;
Song* next;
};
// Add a new song at the end of the playlist
void addSong(Song*& head, string name) {
Song* newSong = new Song{name, nullptr};
if (head == nullptr) {
head = newSong;
head->next = head; // circular link
return;
Song* temp = head;
while (temp->next != head)
temp = temp->next;
temp->next = newSong;
newSong->next = head; // connect last to first
// Display the full playlist
void displayPlaylist(Song* head) {
if (!head) {
cout << "Playlist is empty!";
return;
}
Song* temp = head;
cout << " Playlist Songs:";
do {
cout << "- " << temp-> tle << endl;
temp = temp->next;
} while (temp != head);
// Delete a song from playlist
void deleteSong(Song*& head, string name) {
if (!head) {
cout << "\nPlaylist is empty!\n";
return;
Song* current = head;
Song* prev = nullptr;
// Search for the song
do {
if (current-> tle == name) break;
prev = current;
current = current->next;
} while (current != head);
// If song not found
if (current-> tle != name) {
cout << " Song not found!";
return;
}
// Only one song in playlist
if (current == head && current->next == head) {
head = nullptr;
delete current;
cout << " Song deleted! Playlist is now empty.";
return;
// Dele ng the head
if (current == head) {
Song* last = head;
while (last->next != head)
last = last->next;
head = head->next;
last->next = head;
delete current;
else {
prev->next = current->next;
delete current;
cout << "Song deleted successfully!";
// Play songs con nuously in loop
void playSongs(Song* head) {
if (!head) {
cout << "\nPlaylist is empty!\n";
return;
Song* current = head;
char choice;
do {
cout << "Now Playing: " << current-> tle << endl;
cout << "[N]ext | [E]xit: ";
cin >> choice;
if (choice == 'N' || choice == 'n')
current = current->next; // automa cally loops
} while (choice != 'E' && choice != 'e');
int main() {
Song* playlist = nullptr;
int op on;
string name;
do {
cout << "====== Playlist Menu ======";
cout << "1. Add Song";
cout << "2. Delete Song";
cout << "3. Display Playlist";
cout << "4. Play Songs";
cout << "5. Exit";
cout << "Enter your choice: ";
cin >> op on;
switch (op on) {
case 1:
cout << "Enter song name: ";
[Link]();
getline(cin, name);
addSong(playlist, name);
break;
case 2:
cout << "Enter song name to delete: ";
[Link]();
getline(cin, name);
deleteSong(playlist, name);
break;
case 3:
displayPlaylist(playlist);
break;
case 4:
playSongs(playlist);
break;
case 5:
cout << "\nExi ng playlist... \n";
break;
default:
cout << "\nInvalid choice!\n";
} while (op on != 5);
return 0;