# 2025q1 Homework1 (lab0) contributed by < `dainel40911` > ### Reviewed by `Andrushika` 目前作業好像還沒有完成,我想我可以先對提交 commit 上給一些建議。 在 Commit [bd74e0e](https://github.com/dainel40911/lab0-c/commit/bd74e0eff7d7682ae7897a12ad213ef7b72dd4bc) 中,雖然標題寫 Implement q_free function,但仔細看內容,會發現也對 `q_insert_head`, `q_insert_tail` 等 function 做了修改。建議一個 commit 中不要包含太多無關的修改。 {%hackmd NrmQUGbRQWemgwPfhzXj6g %} ## 開發環境 ```shell $ gcc --version gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 $ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian Address sizes: 39 bits physical, 48 bits virtual CPU(s): 8 On-line CPU(s) list: 0-7 Thread(s) per core: 2 Core(s) per socket: 4 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 126 Model name: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz Stepping: 5 CPU MHz: 1200.000 CPU max MHz: 3600.0000 CPU min MHz: 400.0000 BogoMIPS: 2380.80 Virtualization: VT-x L1d cache: 192 KiB L1i cache: 128 KiB L2 cache: 2 MiB L3 cache: 6 MiB NUMA node0 CPU(s): 0-7 ``` ## 中間發生環境問題 * awk: line XX: regular expression compile failed (missing operand) * () * commit-msg.hook 腳本內的 TRAILERS_BY_REGEX 變數在 regex 組裝時,組成了空的 ()。 * awk 無法解析空的正則 /()/,因此 crash。 ## q_new ### 目標 透過 malloc 分配 head 大小的記憶體空間 檢查 malloc 是否成功,若是失敗,返回 `NULL` 通過檢查後將 `prev` 以及 `next` 兩個指標指向 `head` ,最後 return `head` 。 改進: 參考了 **HotMercury** 在 **HenryChaing** 底下的建議,發現可以使用 linux list.h 提供的API進行檢查的改寫。 ```diff - if (!head) { + if (list_empty(head)) { return NULL; } ``` 改進: 後續發現可以用 list.h 中的 `INIT_LIST_HEAD` ## q_free ### 目標 釋放整個佇列佔用的所有內存,包括佇列元素及其存儲的數據。 初始想法是透過 `*next` 去依序找到,後來發現可以直接使用 list.h 中的 `list_for_each_entry_safe` 去走訪每個節點,使用 list_del 將要刪除的節點從 list 中斷開,透過 釋放每個元素佔用的記憶體。最後,釋放 head 節點的記憶體 ## q_insert_head ### 目標 在 `list` 的開頭插入一個元素,先創建一個新的element_t,為字串分配記憶體並複製輸入字串,透過 `list_add()` 將這個新元素加到佇列的開頭,若有任何的記憶體分配失敗,則返回 false。 ## q_insert_tail ### 目標 跟 q_insert_head 一樣的概念,只是變成將元素插入在尾部 後續看到教授在 `chiangkd` 的建議,採用 q_insert_head 來實作 q_insert_tail ## q_remove_head and q_remove_tail ### 目標 從佇列的頭部或尾部移除一個節點 如果使用者提供緩衝區,會附註被移除節點內的字串給使用者 傳回被移除的節點的指標,若佇列釋空的或無效的話,則回傳 NULL 最多複製 `bufsize-1` 個字符,因為要留一個給 `'\0'` ## q_delete_mid ### 目標 透過快、慢,兩個指標去找到中間點並移除
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up