C++ Music Playlist Program
#include <iostream>
#include <string>
using namespace std;
struct Song {
string title;
Song* prev;
Song* next;
Song(string t) {
title = t;
prev = NULL;
next = NULL;
}
};
class Playlist {
private:
Song* head;
Song* tail;
Song* current;
public:
Playlist() {
head = NULL;
tail = NULL;
current = NULL;
}
void addSong(string title) {
Song* newSong = new Song(title);
if (head == NULL) {
head = tail = current = newSong;
} else {
tail->next = newSong;
newSong->prev = tail;
tail = newSong;
}
cout << "Added: " << title << endl;
}
void playCurrentSong() {
if (current != NULL) {
cout << "Now Playing: " << current->title << endl;
} else {
cout << "Playlist is empty." << endl;
}
}
void nextSong() {
if (current != NULL && current->next != NULL) {
current = current->next;
playCurrentSong();
} else {
cout << "This is the last song." << endl;
}
}
void previousSong() {
if (current != NULL && current->prev != NULL) {
current = current->prev;
playCurrentSong();
} else {
cout << "This is the first song." << endl;
}
}
};
int main() {
Playlist playlist;
int choice;
string title;
do {
cout << "\n--- Music Playlist Menu ---\n";
cout << "1. Add a new song\n";
cout << "2. Play current song\n";
cout << "3. Next song\n";
cout << "4. Previous song\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
cin.ignore();
switch (choice) {
case 1:
cout << "Enter song title: ";
getline(cin, title);
playlist.addSong(title);
break;
case 2:
playlist.playCurrentSong();
break;
case 3:
playlist.nextSong();
break;
case 4:
playlist.previousSong();
break;
case 5:
cout << "Exiting playlist..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 5);
return 0;
}