Commit 26364134 authored by Dominik Widhalm's avatar Dominik Widhalm
Browse files

Further improved shuffle algorithm of task 6.06

parent ca096daa
......@@ -313,17 +313,10 @@ void songs_shuffle (song_t **head) {
/* Go to next element */
curr = curr->next;
}
/* Randomly replace to list elements "number of elements"-times */
for (int i=0; i<entries; i++) {
/* Get a random number between 0 and (entries-1) */
int cnt1 = rand()%entries, cnt2;
/* Get a second number which differs from the first one */
do {
cnt2 = rand()%entries;
} while (cnt2 == cnt1);
/* Swap the two elements */
songs_swap(head, cnt1, cnt2);
/* Switch every element with a random one */
for (int i=1; i<=entries; i++) {
/* Swap the current element with a random one */
songs_swap(head, i, rand()%entries);
}
/* A void function has nothing to return */
......@@ -347,6 +340,11 @@ void songs_shuffle (song_t **head) {
******************************************************************************/
void songs_swap (song_t **head, int x, int y) {
/** Preparation before swapping **/
/* Check if x and y are the same */
if (x == y) {
/* No swap needed */
return;
}
/* Get address of the first element and store its neighbors */
song_t *prevX = NULL, *currX = *head;
for (int i=0; i < x; i++) {
......@@ -408,6 +406,7 @@ int main (void) {
printf("Please enter the name of the directory to scan: ");
/* Read in the user input */
fgets(path,STRING_MAX,stdin);
/* Terminate the directory path string */
path[strlen(path)-1] = '\0';
/* Scan directory and read in the songs */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment