contributed by < kevin30292
>
code GitHub
queue.c 僅提供介面但尚未有完整程式實作,需要補完並逐步精進,包含以下:
q_size
由於要在 O(1) 的常數時間內執行完成,所以也不可能走訪整個鏈結串列,以取得佇列的容積。
新增 size 值紀錄 queue 的大小,又提示
Return 0 if q is NULL or empty
new_q
TODO: What if malloc returned NULL?
malloc
回傳 NULL 可能為記憶體不足,因此失敗。
加入檢查機制:
q_free
需逐一 free
所有queue中的值及字串
q_insert_head
第19行需要 free(newh)
因為第10行的 malloc
失敗,但第6行的malloc是有成功的
q_insert_tail
不能從
head
慢慢找 tail
加入 tail
性質
queue.h
加入:
需修改 code 以維護此參數
new_q
加入:
q_insert_head
加入:
12-16行,避免 head
缺失情況
測試時發現亂碼及 Segmentation fault
Segmentation fault 部分,檢查發現 new_q
沒有:
q->size
q
是否為 NULL改寫 q_new
亂碼部分,檢查發現 q_insert_head
length 值錯誤
改為:
q_reverse
用兩個 pointer 紀錄,過程中的 head
及 tail
命名為 tmph
及 tmpt
從舊 head
開始反轉,head->next
指回 head
以此類推。
維持 q->head
指向尚未反轉的佇列的首項
圖解:
tail
移至 head
head->next
為 NULLq_sort
卡在merge_sort(),不知道應該用什麼型態的 input ,考慮到 recursive 呼叫。
參考前人實作:ZhuMon,採用 pointer to pointer 的方式,即可不用考慮回傳 type 的問題。
發現 sorting 在包含大寫的時候會不正常: