contributed by < Rsysz
>
sysprog
由add_entry
, find_entry
, remove_entry
, swap_pair
, reverse
, print_list
構成
add_entry
void add_entry(node_t **head, int new_value)
{
node_t **indirect = head;
node_t *new_node = malloc(sizeof(node_t));
new_node->value = new_value;
new_node->next = NULL;
assert(new_node);
while (*indirect)
indirect = &(*indirect)->next;
*indirect = new_node;
}
透過node_t **indirect
存取head
內保存的記憶體位址,接著建立新節點new_node
分配記憶體空間與賦值,另外由此可知node_t **head
是多餘的,程式碼可修改為void add_entry(node_t **indirect, int new_value)
利用assert(new_node)
確保正確分配記憶體,透過*indirect
遍歷Link list找到末端節點
並接上新節點
find_entry
node_t *find_entry(node_t *head, int value)
{
node_t *current = head;
for (; current && current->value != value; current = current->next);
/* interate */
return current;
}
透過head
遍歷Link list尋找value對應的節點
remove_entry
void remove_entry(node_t **head, node_t *entry)
{
node_t **indirect = head;
while ((*indirect) != entry)
indirect = &(*indirect)->next;
*indirect = entry->next;
free(entry);
}
透過find_entry
找到對應的節點後將其放入entry
,再使用remove_entry
刪除指定的節點
而remove_entry
使用*indrect
遍歷Link list找尋指定的節點並予以刪除
swap_pair
node_t *swap_pair(node_t *head)
{
for (node_t **node = &head; *node && (*node)->next; node = &(*node)->next->next) //BB1
{
node_t *tmp = *node;
*node = (*node)->next; //BB2
tmp->next = (*node)->next;
(*node)->next = tmp;
}
return head;
}
透過*node
遍歷Link list 每兩個節點交換一次
reverse
node_t *reverse(node_t *head)
{
node_t *cursor = NULL;
while (head) {
node_t *next = head->next;
head->next = cursor; //CCC
cursor = head;
head = next;
}
return cursor;
}
透過next
保留後續Link list
透過cursor
反轉Link list
print_list
void print_list(node_t *head)
{
for (node_t *current = head; current; current = current->next)
printf("%d ", current->value);
printf("\n");
}
透過head
遍歷Link list印出所有value
void swap_pair(node_t **head)
{
for (node_t **node = head; *node && (*node)->next; node = &(*node)->next->next) {
node_t *tmp = *node;
*node = (*node)->next;
tmp->next = (*node)->next;
(*node)->next = tmp;
}
}
void reverse(node_t **head)
{
node_t *cursor = NULL;
while (*head) {
node_t *next = (*head)->next;
(*head)->next = cursor;
cursor = *head;
*head = next;
}
*head = cursor;
}
node_t *rev_recursive(node_t *cursor, node_t *head)
{
if(!head)
return cursor;
node_t *next = head->next;
head->next = cursor;
return rev_recursive(head, next);
}
void reverse(node_t **head)
{
if(!(*head))
return;
*head = rev_recursive(NULL, *head);
}
void shuffle(node_t **head)
{
srand(time(NULL));
int size = 0;
node_t *tmp = *head;
while(tmp){
size++;
tmp = tmp->next;
}
while(size != 0)
{
node_t *tail, *prev_tmp = NULL, *prev_tail = NULL;
int rad = rand() % size;
tmp = tail = *head;
for(int i = 0; i < rad; i++){
prev_tmp = tmp;
tmp = tmp->next;
}
for(int j = 0; j < size - 1; j++){
prev_tail = tail;
tail = tail->next;
}
/*prev_swap & head*/
if(prev_tmp != NULL)
prev_tmp->next = tail;
else
*head = tail;
if(prev_tail != NULL)
prev_tail->next = tmp;
else
*head = tmp;
/*swap*/
node_t *next = tmp->next;
tmp->next = tail->next;
tail->next = next;
size--;
}
}
GitHub/MBP-bluetooth patchGitHub/MBP-sound driverGitHub/bluezGitHub/bluez-alsaGitHub/Bluez-GATT-HID-Keyboard-Emulator-ExampleGoogle Git/Fluoride
May 9, 2025{%hackmd theme-dark %} DPDK GitHub-DPDK GitHub-Pktgen-DPDK OpenNetVM GitHub-openNetVM
May 27, 2021{%hackmd theme-dark %} Server Hardware Dell PowerEdge R240 1U * 2 Laptop * 1 Hardware Info $lshw -class network -businfo //check PCI
May 24, 2021contributed by < Rsysz > Gameboy 模擬器 GitHub Intro 首先根據老師給予的 jitboy 內的描述,了解到當前模擬器有兩種較常見的實作方向。 一是模擬 Game boy 的環境,就能讓舊有的 ROM 順利執行,但缺點就是因 CPU instruction 不兼容,因此須將指令逐行翻譯,採用靜態編譯,因此 branch miss 將無法避免 第二種便是透過 JITcompiler 達成動態編譯,也就是在執行期才對下段指令編譯,因更好的效能而為多數模擬器所採用,相對於前者更消耗資源
Mar 19, 2021or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up