contributed by < qwe661234
>
Doubly-linked list and its value is string.
Add new queue node to the end of the list by list_add
.
If malloc
fails to allocate space for string that store in new node, you should free the memory of new node. Otherwise, it would cause memory leak.
Instead of char *str = (char *) malloc((slen + 1) * sizeof(char))
, you can simply write as char *str = malloc((slen + 1) * sizeof(char))
which is a valid C statement.
Expect for adding new queue node to the end of the list by list_add_tail
, other operations are the same as q_insert_head
.
Get first node of queue by list_first_entry
.
Check if the buffer size - 1 is larger than the length of copy string. Otherwise, it would overflow destination buffer.
Undefined behavior for strncpy
bufsize
'\0'
Except for getting first node of queue by list_last_entry
, other operations are the same as q_remove_head
.
If queue is singular, delete the only node in queue.
If queue is non-singular, use two pointer to get middle node. The first pointer is point to the first node and the second pointer is point to the last node. The first pointer iterate from the first to the the last node and the last pointer iterate oppositely. When these two pointers point to the same node or the firat pointer cross the second pointer, the node pointed by the second pointer would be the middle node.
Becasue these two pointers meet the terminal condition in the beginning, we use do while
.
A queue node would be delete in two condition:
Iterate the queue and record the number of iterated node. If the number is even, move current node to the position which is previos to previous node.
Except for the last node, move node to the queue tail from node is previous to the last node to the first node.
This version is more concise.
Change the value of pointer *prev
and *next
of all queue nodes and head
By means of Linux APIs, you can shorten the above code snip.
Sort the queue by merge sort.
I want to create another head to maintain when I divide the queue, but malloc
is disallowed in q_sort
. Therefore, I remove queue head and pass the first node and the last node as parameters.
Because the mergeSort
only return the first node of sorted queue, we should iterate sorted queue to find out the last node. Then, connect removed queue head to the first node and the last node of sorted queue.
All commands store in a command list, and it is a singly-linked list
ADD_COMMAD
automatically link command xxx
to its operation function do_xxx
You should explain why it works.
cmd_list
is the head of command list
what while
loop in funciton add_cmd
does is sorting commands in alphabetical order. The operaion like insertion sort, find out the suit position from the head of list and insert new command element into command list.
Follow the modern shuffle algorithm to shuffle queue.
0
~ n - 1
0
.n
means node number of tail node.When user call command shuffle, funciton do_shuffle
would be called.
Function ADD_COMAND
connect command parameter1
to do_parameter1
automatically, so when user call command shuffle
, then function do_shuffle
would be called.
parameter2
is the information of command. When user call command help
, the "| shuffle queue"
will show in the information of the command shuffle
.
TODO List