contributed by < SimonLiu423
>
What we care about is what to fill in AAAA
to DDDD
, which are all in the the function of list_insert_before
. So we could ignore how testing is done.
We only focus on:
list_insert_before
document of the function
definition of list_item_t
, list_t
So the list would look like this:
The document says
This function traverses the list to locate the position immediately before the item pointed to by @before and inserts @item in that position.
and by looking at the code, we could intuitively know that:
AAAA
is the indirect pointer of the first element in the listCCCC
is the next element's indirect pointerwhich are &l->head
and &(*p)->next
respectively.
To determine the value of BBBB
, the statement after the loop *p = item
assigns item
to value of p
, which means it's connecting a certain node to *item
. In this case, the node should be the node before *before
. If the next node is *before
, *p
would be before
. Therefore, the answer for BBBB
is before
.
Finally, we should connect *item
's next to before
, so DDDD
should be (*p)->next
.
AAAA
: &l->head
BBBB
: before
CCCC
: &(*p)->next
DDDD
: (*p)->next
Looking at rebuild_list_link
, we could guess that this function should probably make sure all nodes inside the list have next
and prev
pointers set properly.
The while loop inside the function:
Links the current node
's prev
pointer to the previous node.
And outside the loop:
the last node's next
is linked to the first node, so GGGG
should link the first node's prev
to the last node. Therefore, it should be head->prev = prev
.
The rest of the fields to fill are all in the implementation of quick_sort
using Linux style linked list.
Looking back to the original version,
we can see that it should be the pivot's value. Using Linux style list, we can get the address of the struct that list_head
is in by calling list_entry(pivot, node_t, list)
and get its value by list_entry(pivot, node_t, list)->value
.
Same for n_value
:
we can get its value by list_entry(n,node_t,list)->value
Finally,
comparing to the original version:
for index i
~ i+2
it should be left
, pivot
, right
.
GGGG
: head->prev=prev
HHHH
: list_entry(pivot,node_t,list)->value
IIII
: list_entry(n,node_t,list)->value
JJJJ
: pivot
KKKK
: right
According to last week's Q3, we know how to perform quick sort on linked list.
The pivot should be first entry of the list, therefore, AAAA
should be list_first_entry
.
Then, we need to iterate the list entries and move the nodes to different lists based on whether its value is larger or smaller than pivot.
Deleting the pivot from the list first allows us to use list_for_each_entry_safe
more conveniently. So BBBB
should be list_del
.
After comparison, the nodes should move to the tail of list_less
or list_greater
. This ensures that the sorting is stable. So CCCC
should be list_move_tail
.
Then, perform quick sort on list_less
and list_greater
.
Finally, we should move the nodes back to original list head
. We first add pivot
back to the list using list_add
, then use list_splice
to move list_less
to beginning of head
and list_splice_tail
to move list_greater
to end of head
.
AAAA
: list_first_entry
BBBB
: list_del
CCCC
: list_move_tail
DDDD
: list_add
EEEE
: list_splice
FFFF
: list_splice_tail