Skip to content

Music playlist implementation  #59

@keertan5

Description

@keertan5

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

struct Node {
char song[100];
struct Node* next;
};

struct Node* head = NULL;

void createPlaylist(char song[]) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
strcpy(newNode->song, song);
if (head == NULL) {
head = newNode;
newNode->next = head;
} else {
struct Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
}

void displayPlaylist() {
if (head == NULL) {
printf("Playlist is empty.\n");
} else {
struct Node* temp = head;
do {
printf("%s\n", temp->song);
temp = temp->next;
} while (temp != head);
}
}

void deleteSong(char song[]) {
if (head == NULL) {
printf("Playlist is empty.\n");
} else if (strcmp(head->song, song) == 0) {
struct Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
head = head->next;
free(temp);
} else {
struct Node* prev = head;
struct Node* curr = head->next;
while (curr != head && strcmp(curr->song, song) != 0) {
prev = curr;
curr = curr->next;
}
if (curr != head) {
prev->next = curr->next;
free(curr);
} else {
printf("Song not found.\n");
}
}
}

int main() {
int choice;
char song[100];

while (1) {
    printf("\n1. Create Playlist\n");
    printf("2. Display Playlist\n");
    printf("3. Delete Song\n");
    printf("4. Exit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("Enter song name: ");
            scanf("%s", song);
            createPlaylist(song);
            break;
        case 2:
            displayPlaylist();
            break;
        case 3:
            printf("Enter song name to delete: ");
            scanf("%s", song);
            deleteSong(song);
            break;
        case 4:
            exit(0);
        default:
            printf("Invalid choice.\n");
    }
}

return 0;

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions