Skip to content

Commit c2296b3

Browse files
committed
Implement q_shuffle
Add the q_shuffle function that shuffle node in the queue.The function following the Fisher-Yates shuffle algorithm that shuffles the nodes in the queue by randomly selecting a node from the remaining portion and moving it to the tail, ensuring an unbiased shuffle consistent with the method. Change-Id: Ic4ee6a77b848641c72ad183db082f0ba0defccf4
1 parent db8c33f commit c2296b3

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

qtest.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,50 @@ static bool do_merge(int argc, char *argv[])
913913
return ok && !error_check();
914914
}
915915

916+
// static bool do_shuffle(int argc, char *argv[])
917+
// {
918+
// if (argc != 1 && argc != 2) {
919+
// report(1, "%s takes 0-1 arguments", argv[0]);
920+
// return false;
921+
// }
922+
923+
// int reps = 1;
924+
// bool ok = true;
925+
// if (argc == 2) {
926+
// if (!get_int(argv[1], &reps))
927+
// report(1, "Invalid number of calls to size '%s'", argv[2]);
928+
// }
929+
930+
// if (!current || !current->q)
931+
// report(3, "Warning: Calling size on null queue");
932+
// error_check();
933+
934+
// if (current && exception_setup(true)) {
935+
// for (int r = 0; ok && r < reps; r++) {
936+
// q_shuffle(current->q);
937+
// ok = ok && !error_check();
938+
// }
939+
// }
940+
// exception_cancel();
941+
942+
// if (current && ok) {
943+
// int expected_size = current->size;
944+
// int actual_size = q_size(current->q);
945+
946+
// if (expected_size != actual_size) {
947+
// report(1,
948+
// "ERROR: Queue size mismatch after shuffle. Expected: %d, "
949+
// "Got: %d",
950+
// expected_size, actual_size);
951+
// ok = false;
952+
// }
953+
// }
954+
955+
// q_show(3);
956+
957+
// return ok && !error_check();
958+
// }
959+
916960
static bool is_circular()
917961
{
918962
struct list_head *cur = current->q->next;
@@ -1086,6 +1130,7 @@ static void console_init()
10861130
ADD_COMMAND(dedup, "Delete all nodes that have duplicate string", "");
10871131
ADD_COMMAND(merge, "Merge all the queues into one sorted queue", "");
10881132
ADD_COMMAND(swap, "Swap every two adjacent nodes in queue", "");
1133+
// ADD_COMMAND(shuffle, "Shuffle the nodes in queue", "");
10891134
ADD_COMMAND(ascend,
10901135
"Remove every node which has a node with a strictly less "
10911136
"value anywhere to the right side of it",

queue.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,32 @@ int q_size(struct list_head *head)
146146
return len;
147147
}
148148

149+
/* Shuffle the node in the queue*/
150+
void q_shuffle(struct list_head *head)
151+
{
152+
int len = q_size(head);
153+
154+
if (len <= 1) {
155+
return;
156+
}
157+
158+
for (int remain = len; remain > 0; remain--) {
159+
int rand_num = rand() % remain;
160+
161+
struct list_head *node;
162+
163+
list_for_each (node, head) {
164+
if (rand_num == 0) {
165+
break;
166+
}
167+
rand_num--;
168+
}
169+
170+
list_del(node);
171+
list_add_tail(node, head);
172+
}
173+
}
174+
149175
/* Delete the middle node in queue */
150176
bool q_delete_mid(struct list_head *head)
151177
{

0 commit comments

Comments
 (0)