2019q1 Homework1 (lab0)
contributted by < chengWei1123
>
開發環境
作業要求
實作以下 LIFO FIFO Queue 之功能
-
q_new
- Create empty queue.
- Return NULL if could not allocate space.
-
q_free
- Free ALL storage used by queue.
- No effect if q is NULL.
-
q_insert_head
- Attempt to insert element at head of queue.
- Return true if successful.
- Return false if q is NULL or could not allocate space.
- Argument s points to the string to be stored.
- The function must explicitly allocate space and copy the string into it.
-
q_insert_tail
- Attempt to insert element at tail of queue.
- Return true if successful.
- Return false if q is NULL or could not allocate space.
- Argument s points to the string to be stored.
- The function must explicitly allocate space and copy the string into it.
-
q_remove_head
- Attempt to remove element from head of queue.
- Return true if successful.
- Return false if queue is NULL or empty.
- If sp is non-NULL and an element is removed, copy the removed string to sp
(up to a maximum of bufsize-1 characters, plus a null terminator.)
- The space used by the list element and the string should be freed.
-
q_size
- Return number of elements in queue.
- Return 0 if q is NULL or empty
-
q_reverse
- Reverse elements in queue
- No effect if q is NULL or empty
- This function should not allocate or free any list elements
(e.g., by calling q_insert_head, q_insert_tail, or q_remove_head).
- It should rearrange the existing ones.
-
詳細說明 :
實作
queue structure
為了讓 q_insert_tail 和 q_size 能有題目要求的 時間複雜度,所以要增加 tail
和 size 兩個feild
「時間複雜度」和「時間」是兩件事,請改善用語,工程人員說話要精準明確
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
要記得考慮 malloc 失敗的情況
q_free
直接使用 q_remove_head 直到 queue 變空
q_insert_head
strlen 回傳長度不包含 '\0',所以第13行中 strlen 後面要加1
此外我發現我分不太清楚 strcpy 和 strdup 之間的差別
參考 strcpy vs strdup 後,我的理解是 strdup 是 malloc + strcpy,也因為 strdup 有 hidden malloc,所以使用完字串後 progammer 特別容易忘記 free 掉此空間
q_insert_tail
q_remove_head
我原本以為是如果 buffer 放不下被刪除的資料,就直接不放,但後來發現我理解錯題意了
q_size
記得考慮 q 為 NULL 的狀況
q_reverse
程式最後一行,我原本寫 q->head = mid;
讓我 debug 很久,最後才發現執行完 while loop 後 mid pointer 指到的是 NULL
自動評分系統運作的原理
待補
qtest
的行為和裡頭的技巧
待補