From 351da5c108f8ef0c6aa750ef1856e7fdaed285da Mon Sep 17 00:00:00 2001 From: Guanngxu <446678850@qq.com> Date: Sat, 18 Mar 2023 18:31:47 +0800 Subject: [PATCH] update C linked_list init (#421) * docs: update C linked_list init * Update linked_list.md * Update linked_list.c --------- Co-authored-by: Yudong Jin --- .../linked_list.md | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 1a2c3eb3b..ea9b29a96 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -96,6 +96,18 @@ int val; // 结点值 struct ListNode *next; // 指向下一结点的指针(引用) }; + + // typedef 作用是为一种数据类型定义一个新名字 + typedef struct ListNode ListNode; + + /* 构造函数,初始化一个新结点 */ + ListNode *newListNode(int val) { + ListNode *node, *next; + node = (ListNode *) malloc(sizeof(ListNode)); + node->val = val; + node->next = NULL; + return node; + } ``` === "C#" @@ -258,7 +270,18 @@ === "C" ```c title="linked_list.c" - + /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ + // 初始化各个结点 + ListNode* n0 = newListNode(1); + ListNode* n1 = newListNode(3); + ListNode* n2 = newListNode(2); + ListNode* n3 = newListNode(5); + ListNode* n4 = newListNode(4); + // 构建引用指向 + n0->next = n1; + n1->next = n2; + n2->next = n3; + n3->next = n4; ``` === "C#"