3.2 Music Player using linked list

PHOTO EMBED

Fri Aug 25 2023 06:27:10 GMT+0000 (Coordinated Universal Time)

Saved by @109_Vivek

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

struct Node
{
    char data[30];
    struct Node *prev;
    struct Node *next;
};

struct Node *head = NULL;
struct Node *current = NULL;

struct Node *createNode()
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    printf("Enter Song Name :  ");
    scanf("%s", &newNode->data);
    newNode->next = NULL;
    newNode->prev = NULL;
    return newNode;
}

void insertAtEnd()
{
    if (head == NULL)
    {
        head = createNode();
        head->next = head;
        head->prev = head;
    }
    else
    {
        struct Node *newNode = createNode();
        newNode->prev = head->prev;
        newNode->next = head;
        head->prev->next = newNode;
        head->prev = newNode;
    }
}

int size()
{
    struct Node*temp;
    int count;
    if (head == NULL)
	return 0;
    count = 1;
    temp = head;
    while (temp->next != head)
    {
        count++;
        temp = temp->next;
    }
    return count;
}
void deleteAtPosition(int pos)
{
    if (size() == 1)
    {
        free(head);
        head = NULL;
    }
    else if (pos == 1)
    {
        struct Node *last = head->prev;
        head = head->next;
        free(head->prev);
        last->next = head;
        head->prev = last;
    }
    else if (pos < size())
    {
	int j;
	struct Node *temp = head;
	for (j = 1; j < pos; j++)
        {
            temp = temp->next;
        }
        temp->prev->next = temp->next;
        temp->next->prev = temp->prev;
    }
    else if (pos == size())
    {
        head->prev = head->prev->prev;
        free(head->prev->next);
        head->prev->next = head;
    }
    else
    {
        printf("Invalid Position");
    }
}

void traverse()
{
    int i = 1;
    if (head == NULL)
    {
        printf("Empty Playlist ! ");
    }
    else
    {
        struct Node *temp = head;
        do
        {
            printf("\n %d. %s", i, temp->data);
            temp = temp->next;
            i++;
        } while (temp != head);
    }
    printf("\n");
}
void playNext()
{
    if (current == NULL)
    {
        current = head;
    }
    else
    {
        current = current->next;
    }
}

void playPrev()
{
    if (current == NULL)
    {
        current = head;
    }
    else
    {
        current = current->prev;
    }
}
void playSpecificSong(int num)
{
    int i;
    if (num <= size()&& num>=1)
    {
	struct Node *temp = head;
	for (i = 1; i < num; i++)
        {
            temp = temp->next;
        }
        current = temp;
    }else{
        printf("Invalid Song ! ");
    }
}
void main()
{
    int ch;
    clrscr();
    while (1)
    {
	printf("\n---------------------------");
	printf("\n1. Add Music\n");
	printf("2. Remove Music\n");
	printf("3. Show Playlist\n");
	printf("4. Play next Song\n");
	printf("5. Play Previous Song\n");
	printf("6. Play Specific Song\n");
	printf("7. Exit\n");
	printf("\n---------------------------");

	if (current != NULL)
	{
	    printf("\n---------------------------");
	    printf("\nPlaying %s.....", current->data);
	    printf("\n---------------------------");
	}

	printf("\nEnter Your Choice : ");
	scanf("%d", &ch);
	if (ch == 1)
	{
	    insertAtEnd();
	}
	else if (ch == 2)
	{
	    int pos;
	    printf("Enter Position : ");
	    scanf("%d", &pos);
	    deleteAtPosition(pos);
	}
	else if (ch == 3)
	{
	    traverse();
	}
	else if (ch == 4)
	{
	    playNext();
	}
	else if (ch == 5)
	{
	    playPrev();
	}
	else if (ch == 6)
	{
	    int num;
	    printf("Enter Song No. : ");
	    scanf("%d", &num);
	    playSpecificSong(num);
	}
	else if (ch == 7)
	{
	    exit(0);
	}

	else
	{
	    printf("Invalid Input");
	}
    }
}
content_copyCOPY