Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- typedef struct Song {
- int* id;
- char* name;
- char* artist;
- struct Song* next;
- }Song;
- Song* createSong(int id, char* name, char* artist);
- void printSongDetails(Song* song);
- void freeSong(Song* song);
- void addSongToTheEndOfTheList(Song* head, Song* songToAdd);
- void printPlaylist(Song* head);
- void freePlaylist(Song* head);
- void removeSong(Song* head, Song* songToRemove);
- int countList(Song* head);
- void swapSong(Song* head, Song* Source, int id);
- void shuffleList(Song* head);
- void main() {
- Song* head = createSong(1, "aaaa", "aaaa");
- Song* song2 = createSong(2, "bbbb", "bbbb");
- Song* song3 = createSong(3, "cccc", "cccc");
- addSongToTheEndOfTheList(head, song2);
- addSongToTheEndOfTheList(head, song3);
- printPlaylist(head);
- shuffleList(head);
- printPlaylist(head);
- //freePlaylist(head);
- }
- Song* createSong(int id, char* name, char* artist) {
- Song* newSong = (Song*)malloc(sizeof(Song));
- if (newSong) {
- newSong->id = id;
- newSong->name = name;
- newSong->artist = artist;
- newSong->next = NULL;
- //printf("Created New Song\n");
- //printSongDetails(newSong);
- }
- else {
- printf("Could not allocate memory\n");
- exit(1);
- }
- return newSong;
- }
- void printSongDetails(Song* song) {
- //Song name: hdfhfhd, Artist: hdfhdb
- printf("Song name: %s, Artist name: %s\n",song->name,song->artist);
- }
- void freeSong(Song* song) {
- free(song);
- }
- void addSongToTheEndOfTheList(Song* head, Song* songToAdd) {
- Song* currentSong = head;
- if (currentSong) {
- while (currentSong->next) {
- currentSong = currentSong->next;
- }
- currentSong->next = songToAdd;
- songToAdd->next = NULL;
- }
- else {
- printf("List is empty!\n");
- }
- }
- void printPlaylist(Song* head) {
- Song* currentSong = head;
- if (currentSong)
- {
- printf("The playlist: \n");
- for (int i = 1; currentSong; i++)
- {
- printf("%d. ", i);
- printSongDetails(currentSong);
- currentSong = currentSong->next;
- }
- }
- else
- {
- printf("The playlist is empty!\n");
- }
- }
- void freePlaylist(Song* head) {
- Song* currentSong = head;
- Song* temp;
- if (head) {
- while (currentSong)
- {
- temp = currentSong->next;
- freeSong(currentSong);
- currentSong = temp;
- }
- printf("The playlist has been emptied");
- }
- else {
- printf("The list is already empty.");
- }
- }
- void removeSong(Song* head, Song* songToRemove) {
- printf("Removing song: ");
- printSongDetails(songToRemove);
- Song* currentSong = head;
- Song* nextSong = songToRemove->next;
- while (currentSong && currentSong->next != songToRemove) {
- currentSong = currentSong->next;
- }
- if (currentSong) {
- freeSong(songToRemove);
- currentSong->next = nextSong;
- }
- else {
- printf("The requested song is not in the list");
- }
- }
- int countList(Song* head) {
- Song* currentSong = head;
- int i = 0;
- if (currentSong)
- {
- while (currentSong->next)
- {
- currentSong = currentSong->next;
- i++;
- }
- i++;
- }
- return i;
- }
- void swapSong(Song* head,Song* Source, int id) {
- Song* tempSong = (Song*)malloc(sizeof(Song));
- Song* currentSong = head;
- while(currentSong && currentSong->id != id){
- currentSong = currentSong->next;
- }
- if (currentSong) {
- tempSong->id = currentSong->id;
- tempSong->name = currentSong->name;
- tempSong->artist = currentSong->artist;
- tempSong->next = currentSong->next;
- currentSong->id = Source->id;
- currentSong->name = Source->name;
- currentSong->artist = Source->artist;
- currentSong->next = Source->next;
- Source->id = tempSong->id;
- Source->name = tempSong->name;
- Source->artist = tempSong->artist;
- Source->next = tempSong->next;
- free(tempSong);
- }
- else {
- printf("The list is empty.");
- }
- }
- void shuffleList(Song* head) {
- Song* currentSong = head;
- int listLength = countList(head);
- int randNum;
- srand(time(NULL));
- if (currentSong) {
- for (int i = 1; currentSong;i++) {
- swapSong(head, currentSong, rand()%listLength+1);
- currentSong = currentSong->next;
- }
- }
- else {
- printf("The list is empty.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement