DSA C++ Project – Music Play List

Program 1

// Music Play List Based on Duobly Linked List
#include <iostream>
#include <string>
using namespace std;

// Node representing a song
struct Song 
{
    Song* prev;
    string title;
    Song* next;

    //Song(string t) : title(t), prev(nullptr), next(nullptr) {}
    Song(string t) 
    {
        title = t;
        prev = nullptr;
        next = nullptr;
    }
};

// Playlist class
class MusicPlaylist 
{
private:
    Song* head;
    Song* tail;
    Song* current;

public:
    //MusicPlaylist() : head(nullptr), tail(nullptr), current(nullptr) {}

    MusicPlaylist() 
    {
        head = nullptr;
        tail = nullptr;
        current = nullptr;
    }

    // Add song to end
    void addSong(const string& title) 
    {
        Song* newSong = new Song(title);
        if (!head) 
        {
            head = tail = current = newSong;
        } 
        else 
        {
            tail->next = newSong;
            newSong->prev = tail;
            tail = newSong;
        }
        cout << "Added: " << title << endl;
    }

    // Delete current song
    void deleteCurrentSong() 
    {
        if (!current)
         {
            cout << "No song to delete.\n";
            return;
        }

        cout << "Deleting: " << current->title << endl;

        if (current->prev!=NULL)
            current->prev->next = current->next;
        else
            head = current->next;

        if (current->next)
            current->next->prev = current->prev;
        else
            tail = current->prev;

        Song* temp = current;
        current = current->next ? current->next : current->prev;
        delete temp;
    }

    // Go to next song
    void nextSong() 
    {
        if (current  && current->next) 
        {
            current = current->next;
            cout << "Now playing: " << current->title << endl;
        } else 
        {
            cout << "You're at the end of the playlist.\n";
        }
    }

    // Go to previous song
    void prevSong() 
    {
        if (current && current->prev)
         {
            current = current->prev;
            cout << "Now playing: " << current->title << endl;
        } else {
            cout << "You're at the start of the playlist.\n";
        }
    }

    // Display the playlist
    void displayPlaylist() 
    {
      if(head==NULL)  
        cout<<"\n List is empty";
      else
     {   
        cout << "\n--- Playlist ---\n";
        Song* temp = head;
        while (temp) 
        {
            if (temp == current)
                cout << "-> " << temp->title << " [CURRENT]\n";
            else
                cout << "   " << temp->title << endl;
            temp = temp->next;
        }
    }  
    }

    // Destructor to free memory
    ~MusicPlaylist() 
    {
        Song* temp;
        while (head) 
        {
            temp = head;
            head = head->next;
            delete temp;
        }
    }
};

// Main menu
int main() 
{
    MusicPlaylist playlist;
    int choice;
    string title;

    while (true) 
    {
        cout << "\n=== Music Playlist Menu ===\n";
        cout << "1. Add Song\n";
        cout << "2. Delete Current Song\n";
        cout << "3. Next Song\n";
        cout << "4. Previous Song\n";
        cout << "5. Show Playlist\n";
        cout << "6. Exit\n";
        cout << "Enter your choice: ";
        cin >> choice;
        cin.ignore(); // Clear newline from input buffer

        switch (choice) 
        {
            case 1:
                cout << "Enter song title: ";
                getline(cin, title);
                playlist.addSong(title);
                break;
            case 2:
                playlist.deleteCurrentSong();
                break;
            case 3:
                playlist.nextSong();
                break;
            case 4:
                playlist.prevSong();
                break;
            case 5:
                playlist.displayPlaylist();
                break;
            case 6:
                cout << "Exiting playlist.\n";
                return 0;
            default:
                cout << "Invalid choice. Try again.\n";
        }
    }

    return 0;
}
courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *