###### tags: `jserv課程筆記` # Linux linked list note 根據 [你所不知道的 C 語言: linked list 和非連續記憶體](https://hackmd.io/@sysprog/c-linked-list?type=view)做的筆記 主要是把appendList裡面的程式碼從需要判斷是head或是next的pointer,改成像appendIndirect裡面的從存取這些pointer的位置去travel  --- ## 程式碼 ``` cpp #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct List{ int value; struct List *next; }list_entry; void travelList(list_entry **head) { list_entry **indirect = head; if(*indirect != NULL) { printf("[%x] = %d\n", *indirect, (*indirect)->value); travelList(&(*indirect)->next); } } void appendIndirect(list_entry **head, int value) { list_entry **indirect = head; while(*indirect != NULL) { indirect = &(*indirect)->next; } list_entry *node = new list_entry[1]; node->value = value; node->next = NULL; *indirect = node; } void appendList(list_entry **head, int value) { list_entry *pre = NULL; list_entry *cur = *head; list_entry *node = new list_entry[1]; node->value = value; node->next = NULL; while(cur != NULL) { pre = cur; cur = cur->next; } if(pre == NULL) *head = node; else pre->next = node; } int main(void) { list_entry *head = NULL; appendList(&head, 0); appendList(&head, 1); appendList(&head, 2); appendIndirect(&head, 3); appendIndirect(&head, 4); appendIndirect(&head, 5); travelList(&head); system("pause"); return 0; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up