contributed by < yaohwang99
>
詳細閱讀 lab0 說明,依據指示著手修改 queue.[ch] 和連帶的檔案,測試後用 Git 管理各項修改。
q_new
: 建立新的「空」佇列q_free
: 釋放佇列所佔用的記憶體q_insert_head
: 在佇列開頭 (head) 插入 (insert) 給定的新節點 (以 LIFO 準則)q_insert_tail
: 在佇列尾端 (tail) 插入 (insert) 給定的新節點 (以 FIFO 準則)q_remove_head
: 在佇列開頭 (head) 移去 (remove) 給定的節點q_release_element
: 釋放特定節點的記憶體空間q_size
: 計算目前佇列的節點總量q_delete_mid
: 移走佇列中間的節點q_delete_dup
: 在已經排序的狀況,移走佇列中具備重複內容的節點q_swap
: 交換佇列中鄰近的節點q_reverse
: 以反向順序重新排列鏈結串列,該函式不該配置或釋放任何鏈結串列節點,換言之,它只能重排已經存在的節點q_sort
: 以遞增順序來排序鏈結串列的節點Create empty queue.
malloc()
will return NULL if allocate failed.next
and prev
both points to head
because it is a circular listCopy
q_size()
from lab0
Attempt to insert element at head of queue.
Use strlen and strcpy to copy string.
Attempted to insert node by writing the code myself, but later discovered that we can uselist_add()
fromlist.h
. Because the list is circular, the insert procedure will always be the same.
First attempt:
Because strcpy
causes memory leak, use strdup
:
You should explain why it matters.
'\0'
in the source string. If the source string is longer than dst string, strcpy() will overwrite some unallocated memory contiguous to the intended destination.Same as q_insert_head()
list.h
Because I didn't read though list.h
, I spent a lot of time writing q_insert()
. I decide to read through list.h
before continuing.
Uselist_for_each_entry_safe()
to safely remove element.
Fix "Error: Failed to store removed value"
The error occurs because we need to copy the value of deleted element to sp.
Cannot use strndup() because that makes sp point to other memory.
Use strncpy() to copy the value to sp.
Same as q_remove_head()
Use two pointer from head and tail and move torward mid, remove target when they point to mid.
Use two pointer to sequence of identical values and move them to rec.
Call q_free() to delete rec.
Use prev, next, and curr to reverse list.
make test
result:Need to fix some error in allocating memory and string copy.
Everything else pass.
q_remove_head()
and q_remove_tail()
If sp
is NULL, do nothing.
Compare strlen(tmp->value)
with bufsize-1
. Copy tmp->value
to sp
and set last element of sp to '\0'
.
q_insert_head()
and q_insert_tail()
Return false
when strdup()
failed (malloc
failed).
Free tmp
and return false
when malloc
for new element failed.
q_sort()
Return if queue is empty.
Create list_sort.h file, copy-paste the code from list_sort.c and modify the code to suit our need.
head
to NULL
in line 19 in merge()
to avoid compile warning/error.list_cmp_func_t
, likely()
,unlikely()
and compare function compare_entry
for comparison.linux_q_sort()
in qtest.cdo_lsort()
by modifying do_sort()
in qtest.c.DON'T DO THAT. You can introduce single sort
command with different options, which allow run-time switching.
Delete the lsort command and change the sort command for different option.
sort
Compare the two method with traces/trace-sort.cmd
result: 0.58/0.68=0.85, the sort from linux is about 15% faster.
Add the following code in list_sort.h.
Pre-allocate array to store pointers to achieve O(n) time complexity.
tiny.c
into tiny.c
and tiny.h
. Include tiny.h
in other file.extern int listenfd;
extern bool noise;
because we need to use them in other script.process()
The web server has to respond something to form a connection. Otherwise, the browser will display "unable to connect".
result:
TODO:
web
command.