Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://onlinegdb.com/6d6gsdAp-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct Song {
- char title[100], artist[100];
- struct Song* next;
- } Song;
- Song* createSong(const char* title, const char* artist) {
- Song* newSong = (Song*)malloc(sizeof(Song));
- strcpy(newSong->title, title);
- strcpy(newSong->artist, artist);
- newSong->next = NULL;
- return newSong;
- }
- void insertSong(Song** head, const char* title, const char* artist, int position) {
- Song* newSong = createSong(title, artist);
- if (*head==NULL || position == 0) {
- newSong->next = *head;
- *head = newSong;
- return;
- }
- Song* temp = *head;
- int i=0;
- while (temp->next!=NULL && i<position-1){
- temp = temp->next;
- i++;
- }
- newSong->next = temp->next;
- temp->next = newSong;
- }
- void removeSong(Song** head, const char* title) {
- Song *temp = *head, *prev = NULL;
- while (temp!=NULL && strcmp(temp->title, title)) {
- prev = temp;
- temp = temp->next;
- }
- if (temp==NULL){
- printf("Song not found\n");
- return;
- }
- if (prev==NULL)
- *head = temp->next;
- else
- prev->next = temp->next;
- printf("Removed: %s\n", temp->title);
- free(temp);
- }
- void displayPlaylist(Song* head) {
- for (Song* temp = head; temp!=NULL; temp = temp->next)
- printf("%s - %s\n", temp->title, temp->artist);
- }
- int main() {
- Song* playlist = NULL;
- int choice, position;
- char title[100], artist[100];
- while(1){
- printf("1. Insert Song\n2. Remove Song\n3. Display Playlist\n4. Exit\nEnter choice: ");
- scanf("%d", &choice);
- getchar();
- switch (choice) {
- case 1:
- printf("Title: ");
- scanf(" %[^\n]", title);
- getchar();
- printf("Artist: ");
- scanf(" %[^\n]", artist);
- getchar();
- printf("Position: ");
- scanf("%d", &position);
- getchar();
- insertSong(&playlist, title, artist, position);
- break;
- case 2:
- printf("Title to remove: ");
- scanf(" %[^\n]", title);
- getchar();
- removeSong(&playlist, title);
- break;
- case 3:
- displayPlaylist(playlist);
- break;
- case 4:
- while (playlist!=NULL) {
- Song* temp = playlist;
- playlist = playlist->next;
- free(temp);
- }
- exit(0);
- default:
- printf("Invalid choice\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement