2020q1 Homework1 (lab0)
contributed by < markhuang3310
>
說明
開發環境部屬
Container
- 在安裝 snap 卡關 snapd 沒辦法用 systemd 叫起來
VM
- 照著作業流程建立
- 想在 host (Windows)上編輯檔案
mount -t vboxsf linux2020 linux2020 -o dmode=644
- 於 make 時會發生沒辦法建立 soft-link 的問題
- 手動複製 hook 到 .git/hook 並註解掉 hook install script 中 ln -s 的部分
- make test 時候會發生找不到檔案
Call of './qtest -v 1 -f ./traces/trace-01-ops.cmd' failed: [Errno 13] Permission denied: './qtest'
- 睡個 10 秒就正常了
make clean;make;sleep 10; scripts/driver.py -c
TODO: 探討為何 symbolic links 在你的開發環境 (需要描述) 中無法運作
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_new
Create a new, empty queue.
- 處理 malloc fail
- 增加 tail 與 size
下方共筆內容的縮排深度 (depth) 不利於編輯,可比照 q_new
這節的方式重新排版
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_free
Free all storage used by a queue.
- 處理 q 為 NULL
- 用 free_ptr 存當前要被 free 的 element
- 用 next_ptr 存下一個要被 free 的 element
- 當 free_ptr 指向的 element 被 free 完,處理下一個(next_ptr)
- 當 free_ptr 為空則代表已經到 tail 出迴圈
q_insert_head
Attempt to insert a new element at the head of the queue (LIFO discipline).
- 分為四個步驟
- 新的 element
- 處理字串複製
- 處理結點的 link
- 處理 size
q_insert_tail
Attempt to insert a new element at the tail of the queue (FIFO discipline).
- 邏輯同 q_insert_head
- 必須判斷 empty queue ,否則直接取 q->tail->next 時就會噴 NULL pointer dereference
q_remove_head
Attempt to remove the element at the head of the queue.
- 分為四步
- 處理 q 為 NULL 或 empty
- 處理字串複製到 sp, 若 sp 為 NULL 則跳過該步驟
- 處理 link
- 更新 size
q_size
Compute the number of elements in the queue.
只需處理 q 為 NULL
q_reverse
Reorder the list so that the queue elements are reversed in order. This function should not allocate or free any list elements (either directly or via calls to other functions that allocate or free list elements.) Instead, it should rearrange the existing elements.
解讀:
- 仿照 q_free
- tmp_ptr 存當前要反轉的 element
- pre_ptr 存前一個 element (for tmp_ptr-> next)
- 直到 tmp_ptr 為 null (也就是 pre_ptr 為反轉前的 tail)
- 更新 q->tail 和 q->head
考慮以下變更:
要點:
- 精簡又可辨識的變數名稱;
- 用
for
迴圈改寫後,程式碼精簡且易讀;
- 移除多餘的
return
敘述;
- 合併
if
敘述;
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_sort
- Sort elements of queue in ascending order