contributed by < gnkuan0712
>
有關在寫作業前的環境設定寫在這裡: Link
Programming style
使用工具來調整作業程式要求的風格 $ clang-format -i *.[ch]
用四個空格不要用縮排 Tab
函式的左大括號應該放在行首( if else 那些就放在同行即可)
int function(int x)
{
body of function
}
正確的寫法們, 反正就是重要的字後面在空格就好
s = sizeof(struct file);
unsigned long long memparse(char *ptr, char **retptr);
Commit 基本規範
queue.c
放要更改的檔案名稱Improve q_size function to handle null input
Update the q_size function so that it first checks for a null head
pointer and returns 0 when no list is provided.
$ valgrind --tool=<toolname> <program>
toolname 選擇: memcheck
$ sudo apt install libc6-dbg
Kuanch
, weihsinyeh
, vax-r
struct list_head *q_new()
{
struct list_head *head =
(struct list_head *) malloc(sizeof(struct list_head));
if (!head || !head->prev)
return NULL;
INIT_LIST_HEAD(head);
return head;
}
struct list_head *q_new()
{
struct list_head *new_q = malloc(sizeof(struct list_head));
if (new_q)
INIT_LIST_HEAD(new_q);
return new_q;
}
struct list_head *q_new()
{
struct list_head *new_qhead = malloc(sizeof(struct list_head));
if (!new_qhead)
return NULL;
INIT_LIST_HEAD(new_qhead);
return new_qhead;
}
歸納出 q_new() 應該要直接用以下方法
struct list_head *q_new()
{
struct list_head *new_qhead = malloc(sizeof(struct list_head));
+ if (!new_qhead)
+ return NULL;
INIT_LIST_HEAD(new_qhead);
return new_qhead;
}
void q_free(struct list_head *l)
{
if (!l)
return;
for (struct list_head *node = l->next, *next; node != l; node = next) {
next = node->next;
element_t *e = list_entry(node, element_t, list);
free(e->value);
free(e);
}
free(l);
return;
}
void q_free(struct list_head *l)
{
if (!l)
return;
element_t *entry = NULL, *safe = NULL;
list_for_each_entry_safe (entry, safe, l, list) {
list_del(&(entry->list));
free(entry->value);
free(entry);
}
list_del_init(l);
free(l);
return;
}
void q_free(struct list_head *l)
{
if (!l)
return;
element_t *entry, *safe;
list_for_each_entry_safe (entry, safe, l, list) {
free(entry->value);
free(entry);
}
free(l);
return;
}
歸納出 q_free() 可以用以下方法
l 是 dummy node 用來管理整個鏈表
void q_free(struct list_head *l)
{
if (!l)
return; //鏈表不存在 不做後續操作
element_t *entry, *safe; //entry當前元素 //safe是entry的下一個節點
list_for_each_entry_safe(entry, safe, l, list) {
free(entry->value);
free(entry);
}
+ INIT_LIST_HEAD(l); // 清空 list,確保指標不指向已釋放的記憶體
free(l); // 釋放 list_head 本身
}
bool q_insert_head(struct list_head *head, char *s)
{
if (!s)
return false;
element_t *new_e = e_new(s);
if (!new_e)
return false;
list_add(&new_e->list, head);
return true;
}
bool q_insert_head(struct list_head *head, char *s)
{
if (!head)
return false;
element_t *new_element = create_new_element(s);
if (!new_element)
return false;
list_add(&(new_element->list), head);
return true;
}
bool q_insert_head(struct list_head *head, char *s)
{
if (!head)
return false;
element_t *new_ele = malloc(sizeof(element_t));
if (!new_ele)
return false;
INIT_LIST_HEAD(&new_ele->list);
new_ele->value = strdup(s);
if (!new_ele->value) {
free(new_ele);
return false;
}
list_add(&new_ele->list, head);
return true;
}
歸納出 q_insert_head() 和 q_insert_tail 都需要用到
*
bool q_insert_tail(struct list_head *head, char *s)
{
if (!s)
return false;
element_t *new_e = e_new(s);
if (!new_e)
return false;
list_add_tail(&new_e->list, head);
return true;
}
bool q_insert_tail(struct list_head *head, char *s)
{
if (!head)
return false;
element_t *new_element = create_new_element(s);
if (!new_element)
return false;
// list_add_tail(&(new_element->list), head->prev); //another way
list_add_tail(&(new_element->list), head);
return true;
}
bool q_insert_tail(struct list_head *head, char *s)
{
if (!head)
return false;
element_t *new_ele = malloc(sizeof(element_t));
if (!new_ele)
return false;
INIT_LIST_HEAD(&new_ele->list);
new_ele->value = strdup(s);
if (!new_ele->value) {
free(new_ele);
return false;
}
list_add_tail(&new_ele->list, head);
return true;
}
歸納出 q_insert_head() 可以用以下方法
*