Try   HackMD

2023q1 Homework1 (quiz1)

contributed by < kart81604 >

注意細節!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
jserv

測驗一

AAA = item
BBB = list
CCC = list_for_each_entry_safe
DDD = list_move_tail
EEE = list_move_tail
FFF = list_splice_tail

程式碼運作原理

參數介紹 :
pivot : queue 中第一個元素,拿來做比較。
list_less : 小於 pivot 的 item 會接上此 queue 。
list_greater : 大於等於 pivot 的 item 會接上此 queue 。

將 input 的 queue 的第一個 item 作為 pivot ,再將比 pivot 小的 item 組成新的 list_less queue ,其餘的為 list_greater queue 。







init



head

head



pivot

6



head->pivot





7

7



pivot->7





5

5



7->5





9

9



5->9





2

2



9->2











sort



greater

list_greater



7

7



greater->7





less

list_less



5

5



less->5





2

2



5->2





9

9



7->9





6

6



再利用遞迴,對這兩個 queue 進行排序,這邊展示排序 list_less

這邊的 list_less'list_greater' ,與第一輪的不同。







sort



greater

list_greater'



less

list_less'



2

2



less->2





pivot

5









sort



less

list_less



2

2



less->2





5

5



2->5





排序完成後回到第一輪,再將兩者組合起來。







sort



greater

list_greater



7

7



greater->7





less

list_less



2

2



less->2





5

5



2->5





9

9



7->9





6

6









sort



head

head



2

2



head->2





5

5



2->5





6

6



5->6





7

7



6->7





9

9



7->9






測驗二

GGGG = list_for_each_entry_safe
HHHH = list_add_tail
IIII = stack[++top]
JJJJ = stack[++top]
KKKK = stack[top--]

程式碼運作原理

參數介紹 :
partition : 用作對於 stack 最上面非空且非單的 queue ,進行分割。
pivot : queue中的第一個元素當作比較的依據。
list_less : 將 queue 中小於 pivot 的插入此 queue 的第一個。
list_greater : 將 queue 中大於等於 pivot 的插入此 queue 的第一個。

下方的列舉 (即 - 開頭的項目) 非必要,使用簡潔且清晰的描述。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
jserv

好的,已更改

在進到第一個 while 迴圈之前,建立了一個 list_head 陣列 stack ,再將 input 的 queue 接到 stack[0] 上面。(圖僅列舉有連結 queue 的部分,且 queue 是用doubly-linked list 連結,下圖應注意所連結的 queue 的狀態)







sta


cluster_stack

stack



a

0



7

7



a->7





5

5



7->5





8

8



5->8





4

4



8->4





9

9



4->9





進入 while 迴圈,初始化 partition ,然後將 stack 最上面的 queue 接到 partition 後面。
(紅色表示 pivot )







partition



part

partition



pivot

7



part->pivot





5

5



pivot->5





8

8



5->8





4

4



8->4





9

9



4->9





如果 partition 不為空且不為單,則初始化 list_lesslist_greater ,將 partition 所接的 queue 的第一個獨立出來作為 pivot ,對 queue 中每個值做比較,比 pivot 小的接到 list_less , 大於等於就接到 list_greater ,然後將剛剛獨立出來的 pivot 接到 list_less 的 tail ,接著將 list_greaterlist_less 接到 stack 上。







sta


cluster_stack

stack



b

1



4

4



b->4





a

0



9

9



a->9





pivot

7



8

8



9->8





5

5



4->5





5->pivot





如果 partition 為空或為單,則把 stack 最上面的queue,接到 head 的後面。







sta


cluster_stack

stack



stack2

2



4

4



stack2->4





stack1

1



7

7



stack1->7





stack0

0



9

9



stack0->9





8

8



9->8





5

5



7->5











sta


cluster_stack

stack



stack2

2



stack1

1



7

7



stack1->7





stack0

0



9

9



stack0->9





8

8



9->8





5

5



7->5





head

head



4

4



head->4






測驗三

LLLL = (uintptr_t)(a) ^ (uintptr_t)(b)
MMMM = &list->tailnode
PPPP = real_next->cmp

程式碼運作原理

藉由 xorlist_for_each 的程式碼,其中的參數 rp 代表目前所指的點, node 指的是 rp 的下一個點, rn 指的是 node 的下一個點,也就是 rp 的下下一個點, rn 藉由
address_of(rp, node->cmp) 來得到node 下一個點的記憶體地址。