2022q1 Homework1 (lab0)
contributed by < Destiny0504
>
實驗環境
針對佇列的操作
q_new
commit 7183b8e
初始化 queue
commit c747f08
不要只張貼程式碼,要解說和列出後續的改進。
在 GitHub 上都有 commit 提交日期,你不需要在此標注日期。
不要輕易說 "done",工程總有可持續改進之處。
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
q_insert_head
commit 7183b8e
創造一個新的 node 加入整個 queue 的開頭
- 如果成功加入新的 node 會回傳 ture , 反之則回傳 false 。
commit 7e331e1
- 前面的 function 在 implement 的時候 string copy 的時候少算了 '\0'
commit 05a5ad4
- 前面的 function 沒有考慮到 malloc 失敗之後要釋放掉所有已經 allocate 的記憶體。
q_remove_head
commit 7183b8e
移除整個 queue 中的第一個 node
- 如果整個 queue 為空,則會回傳 NULL 。( 由第一個 if 判條件進行判斷 )
- 如果 sp 為 NULL,則會因為無法確定要 remove 的 string 是什麼,所以回傳 NULL 。( 由第二個 if 判條件進行判斷 )
commit c747f08
commit 05a5ad4
q_size
commit 7183b8e
計算目前 queue 中有多少 node
q_free
commit 05a5ad4
釋放所有 allocated 的記憶體
q_delete_mid
commit 4e06235
刪除正中間的的 node
- 如果整個 queue 為空,則會回傳 false 。( 由第一個 if 判條件進行判斷 )
- 如果成功刪除會回傳 true ,否則回傳 false 。
待解決問題
- 需要搞清楚為什麼我們需要多創造出一個 node 來刪除 list_head 的 pointer( 因為要刪除整個 node ,所以需要一個 pointer 指向整個 node )
q_remove_tail
commit 4e06235
移除整個 queue 中的最後一個 node
- 如果整個 queue 為空,則會回傳 NULL 。( 由第一個 if 判條件進行判斷 )
- 如果 sp 為 NULL,則會因為無法確定要 remove 的 string 是什麼,所以回傳 NULL 。( 由第二個 if 判條件進行判斷 )
commit 05a5ad4
q_insert_tail
commit 4e06235
創造一個新的 node 加入整個 queue 的結尾。
- 如果成功在尾端加入新的 node 會回傳 ture , 反之則回傳 false 。
commit 7e331e1
- 前面的 function 在 implement 的時候 string copy 的時候少算了 '\0'
commit 05a5ad4
- 前面的 function 沒有考慮到 malloc 失敗之後要釋放掉所有已經 allocate 的記憶體。
q_reverse
commit 7e331e1
q_sort
commit 05a5ad4
此函式透過將最後一筆對 head 的連結打斷,將原本的雙向佇列改為單向,並傳入 merge sort function 進行排序,最後將排序好的 queue 回復為雙向佇列。
merge
commit 05a5ad4
此函式將兩條以排序完的 queue 依照 data 的大小順序合併為一個新的 queue 。
- 回傳值為一個 pointer 指向合併完成的 queue 的第一筆資料。
mergesort
commit 05a5ad4
此函式將 queue 中的 data 切割成只包含一筆 data 的 list ,再交由 merge function 將 data 按照大小順序合併。
- 回傳值為一個 pointer 指向排序完成的 queue 的第一筆資料
- 為了確保無論何種情況下,排序的時間複雜度皆為 ,所以選擇使用 merge sort 演算法進行排序。
善用 List APIs 改寫上述程式碼。
使用通順的漢語書寫,日後你在面試場合,可能會不經意說出過去寫下的話語,現在就開始要求!
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
- 在 github 上面通過所有的 test

q_shuffle
此函式將 queue 中的 data 依照 Fisher–Yates shuffle 演算法隨機 shuffle
- Fisher–Yates shuffle 演算法的優勢在於可以在 的時間複雜度下完成 shuffle
Reference
Merger sort implementation