Advertisement
Guest User

Untitled

a guest
May 29th, 2019
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. typedef struct Song {
  6. int* id;
  7. char* name;
  8. char* artist;
  9. struct Song* next;
  10. }Song;
  11.  
  12. Song* createSong(int id, char* name, char* artist);
  13. void printSongDetails(Song* song);
  14. void freeSong(Song* song);
  15. void addSongToTheEndOfTheList(Song* head, Song* songToAdd);
  16. void printPlaylist(Song* head);
  17. void freePlaylist(Song* head);
  18. void removeSong(Song* head, Song* songToRemove);
  19. int countList(Song* head);
  20. void swapSong(Song* head, Song* Source, int id);
  21. void shuffleList(Song* head);
  22.  
  23. void main() {
  24. Song* head = createSong(1, "aaaa", "aaaa");
  25. Song* song2 = createSong(2, "bbbb", "bbbb");
  26. Song* song3 = createSong(3, "cccc", "cccc");
  27. addSongToTheEndOfTheList(head, song2);
  28. addSongToTheEndOfTheList(head, song3);
  29. printPlaylist(head);
  30. shuffleList(head);
  31. printPlaylist(head);
  32. //freePlaylist(head);
  33. }
  34.  
  35. Song* createSong(int id, char* name, char* artist) {
  36. Song* newSong = (Song*)malloc(sizeof(Song));
  37. if (newSong) {
  38. newSong->id = id;
  39. newSong->name = name;
  40. newSong->artist = artist;
  41. newSong->next = NULL;
  42. //printf("Created New Song\n");
  43. //printSongDetails(newSong);
  44. }
  45. else {
  46. printf("Could not allocate memory\n");
  47. exit(1);
  48. }
  49. return newSong;
  50. }
  51.  
  52. void printSongDetails(Song* song) {
  53. //Song name: hdfhfhd, Artist: hdfhdb
  54. printf("Song name: %s, Artist name: %s\n",song->name,song->artist);
  55. }
  56.  
  57. void freeSong(Song* song) {
  58. free(song);
  59. }
  60.  
  61. void addSongToTheEndOfTheList(Song* head, Song* songToAdd) {
  62. Song* currentSong = head;
  63. if (currentSong) {
  64. while (currentSong->next) {
  65. currentSong = currentSong->next;
  66. }
  67. currentSong->next = songToAdd;
  68. songToAdd->next = NULL;
  69. }
  70. else {
  71. printf("List is empty!\n");
  72. }
  73. }
  74.  
  75. void printPlaylist(Song* head) {
  76. Song* currentSong = head;
  77. if (currentSong)
  78. {
  79. printf("The playlist: \n");
  80. for (int i = 1; currentSong; i++)
  81. {
  82. printf("%d. ", i);
  83. printSongDetails(currentSong);
  84. currentSong = currentSong->next;
  85. }
  86. }
  87. else
  88. {
  89. printf("The playlist is empty!\n");
  90. }
  91. }
  92.  
  93.  
  94. void freePlaylist(Song* head) {
  95. Song* currentSong = head;
  96. Song* temp;
  97. if (head) {
  98. while (currentSong)
  99. {
  100. temp = currentSong->next;
  101. freeSong(currentSong);
  102. currentSong = temp;
  103. }
  104. printf("The playlist has been emptied");
  105. }
  106. else {
  107. printf("The list is already empty.");
  108. }
  109. }
  110.  
  111. void removeSong(Song* head, Song* songToRemove) {
  112. printf("Removing song: ");
  113. printSongDetails(songToRemove);
  114. Song* currentSong = head;
  115. Song* nextSong = songToRemove->next;
  116. while (currentSong && currentSong->next != songToRemove) {
  117. currentSong = currentSong->next;
  118. }
  119. if (currentSong) {
  120. freeSong(songToRemove);
  121. currentSong->next = nextSong;
  122. }
  123. else {
  124. printf("The requested song is not in the list");
  125. }
  126. }
  127. int countList(Song* head) {
  128. Song* currentSong = head;
  129. int i = 0;
  130. if (currentSong)
  131. {
  132. while (currentSong->next)
  133. {
  134. currentSong = currentSong->next;
  135. i++;
  136. }
  137. i++;
  138. }
  139. return i;
  140. }
  141. void swapSong(Song* head,Song* Source, int id) {
  142. Song* tempSong = (Song*)malloc(sizeof(Song));
  143. Song* currentSong = head;
  144. while(currentSong && currentSong->id != id){
  145. currentSong = currentSong->next;
  146. }
  147. if (currentSong) {
  148. tempSong->id = currentSong->id;
  149. tempSong->name = currentSong->name;
  150. tempSong->artist = currentSong->artist;
  151. tempSong->next = currentSong->next;
  152. currentSong->id = Source->id;
  153. currentSong->name = Source->name;
  154. currentSong->artist = Source->artist;
  155. currentSong->next = Source->next;
  156. Source->id = tempSong->id;
  157. Source->name = tempSong->name;
  158. Source->artist = tempSong->artist;
  159. Source->next = tempSong->next;
  160. free(tempSong);
  161. }
  162. else {
  163. printf("The list is empty.");
  164. }
  165.  
  166. }
  167. void shuffleList(Song* head) {
  168. Song* currentSong = head;
  169. int listLength = countList(head);
  170. int randNum;
  171. srand(time(NULL));
  172. if (currentSong) {
  173. for (int i = 1; currentSong;i++) {
  174. swapSong(head, currentSong, rand()%listLength+1);
  175. currentSong = currentSong->next;
  176. }
  177. }
  178. else {
  179. printf("The list is empty.");
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement