c 的世界可能高頻業務都依賴 list 增刪改查. 這裏簡單交流下自己在 c 裏面使用 list
1. 學生時代, 那會學習 C 數據結構, 比較簡單
struct person { int id; char name[64+1]; struct person * next; };
類似上面這樣, 需要什麼依賴 next 指針來回調整, 然後手工 print F5 去 debug 熬.
2. 剛工作青年時代, 主要花活, 隨大流
類似
structc/modular/test/list.h at master · wangzhione/structc
#pragma once #include "struct.h" // // list.h 似魔鬼的步伐, 單鏈表庫 // $LIST 需要嵌入 struct 的第一行 // void * list = nullptr; // create list // list_delete(list, fide); // [可選] delete list // struct $list { struct $list * next; }; #define $LIST struct $list $node;
或者類似
ccan/ccan/list/list.h at master · rustyrussell/ccan
/** * struct list_node - an entry in a doubly-linked list * @next: next entry (self if empty) * @prev: previous entry (self if empty) * * This is used as an entry in a linked list. * Example: * struct child { * const char *name; * // Linked list of all us children. * struct list_node list; * }; */ struct list_node { struct list_node *next, *prev; }; /** * struct list_head - the head of a doubly-linked list * @h: the list_head (containing next and prev pointers) * * This is used as the head of a linked list. * Example: * struct parent { * const char *name; * struct list_head children; * unsigned int num_children; * }; */ struct list_head { struct list_node n; };
雜技, 理解的心智負擔稍微高一點, 但使用上對方有了單元測試, 比較成熟, list 結構問題較少, 除了業務的內存錯位自己 debug 稍微麻煩點.
3. 35歲中年之後, 又想起剛開始那會
skynet/skynet-src/socket_server.c at master · cloudwu/skynet
類似這樣
struct write_buffer { struct write_buffer * next; const void *buffer; char *ptr; size_t sz; bool userobject; }; struct write_buffer_udp { struct write_buffer buffer; uint8_t udp_address[UDP_ADDRESS_SIZE]; }; struct wb_list { struct write_buffer * head; struct write_buffer * tail; };
需要 list , 還是直接 next 指針來回調整.
當下各種 ai 加持, 這種方式可能是最簡單最直接, 當然 c 寫代碼相對麻煩, 多做好單元測試.
人生也類似, 兜兜轉轉一個圈, 那種圈在時空維度看, 是螺旋上升的.
不知道有沒有人好奇, 為什麼不直接一開始就上升呢, 可能生命不需要趕着投胎吧, 浪費不是時間, 也不是人生, 也可能是享受到了時間, 享受到了自己來回波動的人生.
: ) Good luckly ~
4. 未來時代, 對於個人而言, C 融入自己思維一部分, 可能不再去主動寫了.
類似嬰兒那會記憶, 與其説忘了, 已經存在於腦海最底層機制裏面了.