Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views3 pages

Music Playlist Program

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Music Playlist Program

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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;
}

You might also like