contributed by < mingjer
>
$ uname -a
Linux kdd179 5.4.0-47-generic #51-Ubuntu SMP Fri Sep 4 19:50:52 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
$ gcc --version
gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
在 queue.[c/h]
中完成以下實作
q_new
: 建立新的「空」佇列;q_free
: 釋放佇列所佔用的記憶體;q_insert_head
: 在佇列開頭 (head) 加入 (insert) 給定的新元素 (以 LIFO 準則);q_insert_tail
: 在佇列尾端 (tail) 加入 (insert) 給定的新元素 (以 FIFO 準則);q_remove_head
: 在佇列開頭 (head) 移除 (remove) 給定的元素。q_size
: 計算佇列中的元素數量。q_reverse
: 以反向順序重新排列鏈結串列,該函式不該配置或釋放任何鏈結串列元素,換言之,它只能重排已經存在的元素;q_sort
: 以遞增順序來排序鏈結串列的元素q_size
和 q_insert_tail
兩個funtion能在 queue_t
中新增兩個參數 ( tail
, size
)/* Queue structure */
typedef struct
{
/* Linked list of elements */
/* TODO: You will need to add more fields to this structure
* to efficiently implement q_size and q_insert_tail.
*/
/* TODO: Remove the above comment when you are about to implement. */
list_ele_t *head;
list_ele_t *tail;
int size;
} queue_t;
q_new
queue_t *q_new()
{
queue_t *q = malloc(sizeof(queue_t));
/* TODO: What if malloc returned NULL? */
if(q != NULL)
{
q->head = NULL;
q->tail = NULL;
q->size = 0;
return q;
}
else
{
return NULL;
}
}
q_free
q_insert_head
bool q_insert_head(queue_t *q, char *s)
{
list_ele_t *newh;
/* TODO: What should you do if the q is NULL? */
if(q == NULL)
{
return FALSE;
}
newh = malloc(sizeof(list_ele_t));
if(newh == NULL)
{
return FALSE;
}
/* Don't forget to allocate space for the string and copy it */
newh->value = malloc(sizeof(s));
/* What if either call to malloc returns NULL? */
newh->next = q->head;
q->head = newh;
return true;
}
q_insert_tail
q_remove_head
q_size
q->size
中取值,就可以在 int q_size(queue_t *q)
{
/* TODO: You need to write the code for this function */
/* Remember: It should operate in O(1) time */
/* TODO: Remove the above comment when you are about to implement. */
if(q != NULL)
{
return q->size;
}
else
{
return 0;
}
}
q_reverse
q_sort
爬蟲 網路爬蟲是一種機器人,可以幫您自動地瀏覽網際網路並擷取目標資訊 自動化爬取目標網站,並蒐集目標資料 爬蟲 Moudle ( Python ) requests
Nov 7, 2020contributed by < mingjer > 測驗 1 LeetCode 461. Hamming Distance 提及,兩個整數間的 Hamming distance 為其二進位的每個位元的差。請計算輸入參數兩整數 x 與 y 的 Hamming distance,例如整數 1 的二進位為 0 0 0 1,而整數 4 的二進位為 0 1 0 0,則 1 與 4 的 Hamming distance 為 2。 int hammingDistance(int x, int y) { return __builtin_popcount(x OP y); } OP = ^
Oct 12, 2020contributed by < mingjer > 測驗 1 #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <string.h> bool is_ascii(const char str[], size_t size) { if (size == 0)
Sep 27, 2020contributed by < mingjer > :penguin: 作業要求 重新回答第 1 周測驗題,連同附帶的「延伸問題」。 解釋程式運作原理時,應提供對應的 Graphviz 圖例,可參照 Linked List 題目 1 + 分析 比照 課前測驗參考解答: Q1 和 Linked list 題目分析 的模式來撰寫共筆,需要詳細分析自己的思路、參閱的材料 (以第一手材料為主,包含 C 語言規格書的章節),以及==進行相關實驗==。
Sep 27, 2020or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up