mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 12:16:29 +08:00
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 <krahets@163.com>
This commit is contained in:
parent
28f3c98697
commit
351da5c108
1 changed files with 24 additions and 1 deletions
|
@ -96,6 +96,18 @@
|
||||||
int val; // 结点值
|
int val; // 结点值
|
||||||
struct ListNode *next; // 指向下一结点的指针(引用)
|
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#"
|
=== "C#"
|
||||||
|
@ -258,7 +270,18 @@
|
||||||
=== "C"
|
=== "C"
|
||||||
|
|
||||||
```c title="linked_list.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#"
|
=== "C#"
|
||||||
|
|
Loading…
Reference in a new issue