contributed by < MicahDoo
>
Linux on Macbook Air
詳細閱讀 C Programming Lab ,依據指示著手修改 queue.[ch] 和連帶的檔案,測試後用 Git 管理各項修改,記得也該實作 q_sort 函式。
Settings -> Region & Language -> +(Under Input Sources) -> Other -> Chinese(Chewing)
There are no late days, grace days, or extensions for this assignment.
Late days/period: 可以接受遲交的日子。(通常會扣分)
Grace days/period: 可以遲交並且不扣分的日子。
Extensions: 延長繳交。
All handins are electronic.
Ther material covered should all be review for you.
Explicit memory management, as required in C.
用C實際人工手打程式碼做記憶體管理,而非讓編譯器自動完成。
Creating and manipulating pointer-based data structures.
學會使用創建及操縱指針類的結構。
Ehancing the performance of key operations by storing redundant information in data structures.
利用在資料結構中儲存多餘的資訊以強化關鍵操作的效能。
Implementing robust code that operates correctly with invalid arguments in data structures.
實做出能處理無效輸入的穩健程式碼。
The lab involves implementing a queue, suppporting both LIFO and FIFO queuing disciplines. The underlying data structure is a singly-linked list, enhanced to make some of the operations more efficient.
One queue is one queue_t
variable whose ->head
points to the head of the list.
One node is one ELE
/list_ele_t
variable storing the string ->value
and the next node ->next
.
本次作業不同於CMU的作業,因此有多處已不甚相同!
make test
Tested make test
before putting in any code and found two successful traces:
In MakeFile
:
In scripts/driver.py
:
If the file is run directly, call run(sys.arg[0]), sys.argv[1:])
, which is a method in the file. Here, sys.arg[0] == 'scripts/driver.py'
and sys.argv[1:]
is a list that contains only one string -c
.
In def run(name, args):
:
In run(self, tid)
under the class Tracer
:
In runTrace(t)
:
With self.traceDirectiory == "./traces"
and self.traceDict == "trace-13-malloc"
.
…
clist == "./qtest -v 0 -f ./traces/trace-13-malloc"
In trace-13-malloc.cmd
:
Only the commands option
and new
are used.
In qtest.c
:
These are the command and parameters used in trace-13.
In static bool do_new(int argc, char *argv[])
:
In show_queue()
:
Seems like nothing else is being checked after q_new()
is called, as show_queue()
returns true
right after a NULL
queue is being detected, which is why the test passed.
TODO: revise the above code to include ./qtest -f traces/trace-13-malloc.cmd
which will tell us that the reason it passes is because it only returns a WARNING when the intialized list is NULL
.
Why?
Nothing has been put into the code yet so naturally the complexity is expected to be O(0)
.
element_t
Upon inspection of the given queue.h
file, the difference in struct
declaration between the CMU version and the NCKU version is clear:
The first node doesn't have to be wrapped in element_t
.
option malloc 50
: malloc
will return NULL
on average half of the times.Warning: malloc returning NULL
: the creation of a new queue failed, giving l = NULL
, it should be l = []
.new
when l != NULL
, the old queue will be freed. This is where errors happen. We need to finish q_free()
before we can complete q_new()
.Remember to free value
first before freeing the whole element.
Remember to click fetch upstream
in your repository to keep up with updates.
Two pointers going from the opposite sides inward at the same pace.
When both pointers meet(l == r
, odd length) or when they are one node apart (l->next == r
, even length), the left pointer should be deleted.
I use a pointer to pointer to make removing the current node easy.
In each iteration, if the current string in the node is the same as the next, I delete the current string, then continue deleting nodes until a different string is found or the end is reached.
Special attention required on the order in which we assign pointers:
left
and right
.
right->next->prev
is originally left
, but since we can already reference that node with left
, we can change it as early as possible.left->prev
, I can no longer access the left neighbor of the pair anymore.Because this operation involves swapping two values (it->next
& it->prev
), so a tmp
node is inevitable.
MEMO: 觀摩@LGP-TW的實作,看到教授留下一行:針對上方的若干操作,提取出可共用的 inline function。
不甚解其意,待問。
The 14th trace failed. Time limit exceeded.
Since my computer is from 2011 I think this error might be due to my computer being too old. It could be my algorithm as well. I shall ask a classmate to test my code for me on a different computer.
Update:
Works fine on Github Workflow.