2019q1 Homework1 (lab0)
contributed by < warrenanson
>
開發環境(已更改)
雙系統linux
bash on window 虛擬器(舊版)
不要將 Linux 安裝在虛擬機器上,也不要用雲端主機,否則之後用 perf 一類的工具將無法測量出實際程式效能
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
作業目標
Your task is to modify the code in queue.h and queue.c to fully implement the following functions.
- q_new:
- Create a new, empty queue.
- q_free:
- Free all storage used by a queue.
- q_insert_head:
- Attempt to insert a new element at the head of the queue (LIFO discipline).
- q_insert_tail:
- Attempt to insert a new element at the tail of the queue (FIFO discipline).
- q_remove_head:
- Attempt to remove the element at the head of the queue.
- q_size:
- Compute the number of elements in the queue.
- 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
實作
Queue structure
新增 size 以及 tail
則 q_size 和 q_insert_tail 可以在 內完成
q_new
初始化Queue之設定,若malloc失敗則回傳NULL
q_free
無法一次釋放掉整個Queue,必須從head到tail一個個釋放
將q->head移動到next,再將prev本身和其value釋放
q_insert_head
由於strcpy回傳的長度不含\0
,所以 newh->value所需空間大小要多一
q_insert_tail
和q_insert_head
差不多
差別在於當q->head
為空時,q->tail = newh
當ㄉq->head
不為空時, q->tail->next = newh;
q_remove_head
依照題目要求當Queue為NULL或空時回傳false
以及copy the removed string to *sp
(up to a maximum of bufsize-1 characters, plus a null terminator.)
所以 sp[bufsize - 1] = '\0'
此處要加上\0
q_size
若Queue不回空就直接回傳size,可在 內完成
q_reverse
這部分本來的寫法是將value從tail開始依序丟到另一個Queue當中
後來改寫成目前這樣,將指標反轉即可達到效果,不需要額外的空間