0% found this document useful (0 votes)
16 views5 pages

Circular Playlist Management in C++

The document is a C++ program that implements a circular linked list to manage a music playlist. It includes functions to add, delete, display, and play songs in a loop. The main function provides a menu for user interaction to perform these operations.

Uploaded by

avaneeshmac12
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)
16 views5 pages

Circular Playlist Management in C++

The document is a C++ program that implements a circular linked list to manage a music playlist. It includes functions to add, delete, display, and play songs in a loop. The main function provides a menu for user interaction to perform these operations.

Uploaded by

avaneeshmac12
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

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

You might also like