contributed by < york56
>
System-Software-2019
GUTS
Done. Yet to documentate the installation process.
Done. Done. Done.
https://people.engr.ncsu.edu/efg/210/s99/Notes/LinkedList.1.html
linked list
a list consisting of items in which each item knows the location of the next item.
node(element)
an item in a linked list. Each node contains a piece of list data and the location of the next node (item).
link
(of a node) the location of the next node.
head node
first node in a linked list
head pointer
points to the head node
The last node has a reference to null.
I can't understand this:
type struct
Now I understood the it by this.
[C,C++] typedef struct 用法說明 - 李山姆的部落格 - 痞客邦
char *value
Now I understood the it by this.
C語言: 超好懂的指標,初學者請進~
This array needs to be explicitly allocated and freed
】Being in a status where the memory for the datum is prepared.
??? ??? ??? ??? ???
queue.h
list_ele_t
instead of struct ELE
, since we have already typedefined it, resulting
.
in the second part, but has the need to writ:
in the first part.
According to Prof. Jserv, it is
.
I can't understand it.
I thought a queue is something like:
.
So it should be something like:
, but after reading
https://www.interviewbit.com/courses/programming/topics/linked-lists/
I realize we merely have to hold two pointers in hands (one pointing to the beginning element and the other to the ending element), letting every element floating elsewhere.
Also it seems like we have to calculate the size of the queue in advance and store it.
If the queue_t data only has head pointer, tail pointer, and queue size, how to get the nth element?
Count along the queue?
q_new
It seems like
is enough to the task
Create empty queue.
Return NULL if could not allocate space.
, but I don't know why.
QwQ QwQ QwQ
I interpret the code as:
define a pointer
q_new
pointing toward something of type-queue_t.
I thought we should declare a queue_t variable and let the pointer inside point to a null.
q_free
void q_free(queue_t *q)
is enough??
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.
I have no idea.
I refer to < DyclexiaS
>. https://hackmd.io/s/Byjnik5F7
The conditions causing failures include:
newh = malloc(sizeof(list_ele_t))
去開記憶體,開給 list_ele_t 這種 datatype。然後把開出來的記憶體的address指派給newh
我不懂的的是我以為list_ele_t的大小是固定的,為什麼要一直重算?
去要記憶體,如果發生newh == NULL
,也就是要不到,就回傳false。
而且我以為list_ele_t的宣告不論怎樣都會被執行,newh有機會是NULL嗎?
sizeof()
This operator gives the byte size of the inputted operator.
https://www.geeksforgeeks.org/sizeof-operator-c/
malloc()
The C library function void *malloc(size_t size)
allocates the requested memory and returns a pointer to it.
https://en.cppreference.com/w/c/memory/malloc
https://openhome.cc/Gossip/CGossip/MallocFree.html
再造一個 pointer ( 其指向 s 這個 pointer 指向的 char )
並 assign 給 value (其為 newh 這個 pointer 指向的 struct 裡面的 pointer ),
但這個 assigning 也有可能失敗。(會什麼會失敗啊???)
失敗了的話就把 newh 清掉然後回傳 false 。
->
http://squall.cs.ntou.edu.tw/cprog/content/12 struct and other datatypes_bw.pdf
char * strdup(const char *str1);
Returns a pointer to a null-terminated byte string, which is a duplicate of the string pointed to by str1. The returned pointer must be passed to free to avoid a memory leak.
char * strdup(const char *str1);
的輸入如果是NULL會發生什麼?In computer programming, a null-terminated string is a character string stored as an array containing the characters and terminated with a null character.
https://en.wikipedia.org/wiki/Null-terminated_string
???
void free( void* ptr );
Deallocates the space previously allocated by malloc(), calloc(), aligned_alloc, (since C11) or realloc().
If ptr
is a null pointer, the function does nothing.
???
但如果放之前是空的,頭尾都要這個指向新加進去的元素。
如上,假如 head 裡面沒東西(所以是空的 queue )就讓 把 newh 指派給 tail 。然後等一下把 newh 也指派給 head ,這樣頭尾都指向同一個 element ,所以就從空的長大成有一個元素的,合理。
無論 head 是不是 NULL 我們都要做以下。
讓 newh 的 next 指向 原本的第一個 element ,也就是
把 q 裡面的 head 指派給 newh (我們要加進去的 element 的 pointer ) 的 next 。
我們都是靠 q 來知道頭尾在哪裡的,讓 q 的 head 指向整顆 newh 。
把 q 裡面的 q_size 加一。
這樣我們應該就完成了。
為什麼以下是分兩步
不是一步