Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Music playlist implementation #59

Open
keertan5 opened this issue Dec 22, 2024 · 0 comments
Open

Music playlist implementation #59

keertan5 opened this issue Dec 22, 2024 · 0 comments

Comments

@keertan5
Copy link

keertan5 commented Dec 22, 2024

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

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant