Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
master-embedded-systems
c-exercise-solutions public
Commits
26364134
Commit
26364134
authored
Dec 13, 2017
by
Dominik Widhalm
Browse files
Further improved shuffle algorithm of task 6.06
parent
ca096daa
Changes
1
Hide whitespace changes
Inline
Side-by-side
ch_6/task_06/main.c
View file @
26364134
...
...
@@ -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 */
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment