DSA Project – Music Playlist

Learn DSA & Get Ready for MAANG Companies Start Now!!

Program 1

// Music Play List Based on Duobly Linked List

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_TITLE_LEN 100

// Doubly Linked List Node
typedef struct Song 
{
    struct Song* prev;
    char title[MAX_TITLE_LEN];
    struct Song* next;
} Song;

Song* head = NULL;
Song* tail = NULL;
Song* current = NULL;

// Function to create a new song node
Song* createSong(char* title) 
{
    Song* newSong = (Song*)malloc(sizeof(Song));
    strcpy(newSong->title, title);
    newSong->prev = NULL;
    newSong->next = NULL;
    return newSong;
}

// Add song to end of playlist
void addSong(char* title) 
{
    Song* newSong = createSong(title);
    if (head == NULL) 
    {
        head = tail = current = newSong;
    } else 
    {
        tail->next = newSong;
        newSong->prev = tail;
        tail = newSong;
    }
    printf("Added: %s\n", title);
}

// Delete current song
void deleteCurrentSong() 
{
    if (current == NULL) {
        printf("No song to delete.\n");
        return;
    }

    printf("Deleting: %s\n", current->title);

    if (current->prev)
        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;
    free(temp);
}

// Move to next song
void nextSong() 
{
    if (current!=NULL && current->next!=NULL) 
    {
        current = current->next;
        printf("Now playing: %s\n", current->title);
    } 
    else 
    {
        printf("You're at the end of the playlist.\n");
    }
}

// Move to previous song
void prevSong() {
    if (current && current->prev) 
    {
        current = current->prev;
        printf("Now playing: %s\n", current->title);
    } else {
        printf("You're at the start of the playlist.\n");
    }
}

// Display playlist forward
void displayPlaylist() 
{
   if(head==NULL) 
    printf("\n Play list is empty......");
  else
  {  
    Song* temp = head;
    printf("\nPlaylist:\n");
    while (temp != NULL) 
    {
        if (temp == current)
            printf("-> %s [CURRENT]\n", temp->title);
        else
            printf("   %s\n", temp->title);
        temp = temp->next;
    }
    printf("\n");
}  
}

// Main menu
int main() {
    int choice;
    char title[MAX_TITLE_LEN];

    while (1) 
    {
        printf("\n--- Music Playlist ---\n");
        printf("1. Add Song\n");
        printf("2. Delete Current Song\n");
        printf("3. Next Song\n");
        printf("4. Previous Song\n");
        printf("5. Show Playlist\n");
        printf("6. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        getchar(); // consume newline

        switch (choice) 
        {
            case 1:
                printf("Enter song title: ");
                fgets(title, MAX_TITLE_LEN, stdin);
                title[strcspn(title, "\n")] = '\0'; // remove newline
                addSong(title);
                break;
            case 2:
                deleteCurrentSong();
                break;
            case 3:
                nextSong();
                break;
            case 4:
                prevSong();
                break;
            case 5:
                displayPlaylist();
                break;
            case 6:
                printf("Exiting playlist.\n");
                return 0;
            default:
                printf("Invalid choice.\n");
        }
    }

    return 0;
}

 

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

courses

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 *