Advertisement
GamerBhai02

7. SLL Music Management System

Nov 4th, 2024 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | Source Code | 0 0
  1. https://onlinegdb.com/6d6gsdAp-
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. typedef struct Song {
  7.     char title[100], artist[100];
  8.     struct Song* next;
  9. } Song;
  10. Song* createSong(const char* title, const char* artist) {
  11.     Song* newSong = (Song*)malloc(sizeof(Song));
  12.     strcpy(newSong->title, title);
  13.     strcpy(newSong->artist, artist);
  14.     newSong->next = NULL;
  15.     return newSong;
  16. }
  17. void insertSong(Song** head, const char* title, const char* artist, int position) {
  18.     Song* newSong = createSong(title, artist);
  19.     if (*head==NULL || position == 0) {
  20.         newSong->next = *head;
  21.         *head = newSong;
  22.         return;
  23.     }
  24.     Song* temp = *head;
  25.     int i=0;
  26.     while (temp->next!=NULL && i<position-1){
  27.         temp = temp->next;
  28.         i++;
  29.     }
  30.     newSong->next = temp->next;
  31.     temp->next = newSong;
  32. }
  33. void removeSong(Song** head, const char* title) {
  34.     Song *temp = *head, *prev = NULL;
  35.     while (temp!=NULL && strcmp(temp->title, title)) {
  36.         prev = temp;
  37.         temp = temp->next;
  38.     }
  39.     if (temp==NULL){
  40.         printf("Song not found\n");
  41.         return;
  42.     }
  43.     if (prev==NULL)
  44.         *head = temp->next;
  45.     else
  46.         prev->next = temp->next;
  47.     printf("Removed: %s\n", temp->title);
  48.     free(temp);
  49. }
  50. void displayPlaylist(Song* head) {
  51.     for (Song* temp = head; temp!=NULL; temp = temp->next)
  52.         printf("%s - %s\n", temp->title, temp->artist);
  53. }
  54. int main() {
  55.     Song* playlist = NULL;
  56.     int choice, position;
  57.     char title[100], artist[100];
  58.     while(1){
  59.         printf("1. Insert Song\n2. Remove Song\n3. Display Playlist\n4. Exit\nEnter choice: ");
  60.         scanf("%d", &choice);
  61.         getchar();
  62.         switch (choice) {
  63.             case 1:
  64.                 printf("Title: ");
  65.                 scanf(" %[^\n]", title);
  66.                 getchar();
  67.                 printf("Artist: ");
  68.                 scanf(" %[^\n]", artist);
  69.                 getchar();
  70.                 printf("Position: ");
  71.                 scanf("%d", &position);
  72.                 getchar();
  73.                 insertSong(&playlist, title, artist, position);
  74.                 break;
  75.             case 2:
  76.                 printf("Title to remove: ");
  77.                 scanf(" %[^\n]", title);
  78.                 getchar();
  79.                 removeSong(&playlist, title);
  80.                 break;
  81.             case 3:
  82.                 displayPlaylist(playlist);
  83.                 break;
  84.             case 4:
  85.                 while (playlist!=NULL) {
  86.                     Song* temp = playlist;
  87.                     playlist = playlist->next;
  88.                     free(temp);
  89.                 }
  90.                 exit(0);
  91.             default:
  92.                 printf("Invalid choice\n");
  93.         }
  94.     }
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement