contributed by < bauuuu1021
>
Your task is to modify the code in queue.h and queue.c to fully implement the following functions.
Create a new, empty queue.
Free all storage used by a queue.
Attempt to insert a new element at the head of the queue (LIFO discipline).
Attempt to insert a new element at the tail of the queue (FIFO discipline).
Attempt to remove the element at the head of the queue.
Compute the number of elements in the queue.
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
在 sturct queue_t 中新增 int size
來紀錄 queue 中 element 數
且需留意 q = NULL 時 q->size 未被定義,故需額外處理
new
來建立 queue,會出現以下的錯誤訊息回到 q_insert_head() 中可以發現以下註解
及
所以加上
即解決
ih
而是呼叫 it
copy the removed string to *sp
,原本使用 strdup(),但發現會造成以下問題後來以 strncpy() 取代 strdup(),即解決問題
traces/trace-09-robust.cmd
中的 rhq
會產生以下錯誤訊息使用 gdb 發現是 q_remove_head()
這個 function 出現問題
再重新閱讀 rhq
的作業需求 : Remove from head of queue without reporting value
且 qtest.c 的 q_remove_head() 中原本用來承接 removed string 的參數為 NULL
所以將原本的函式多加上檢查 sp 的判斷條件,修改如下
TOTAL 100/100
bauuuu1021
, sysprog
,2019spring