🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 链表的类型 - 单链,双链和循环链 > 原文: [https://www.programiz.com/dsa/linked-list-types](https://www.programiz.com/dsa/linked-list-types) #### 在本教程中,您将学习不同类型的链表。 此外,您还将在 C 中找到链表的实现。 链表有三种常见类型。 1. [单链表](#singly) 2. [双链表](#doubly) 3. [循环链表](#circular) * * * ## 单链表 这是最常见的。 每个节点都有数据和指向下一个节点的指针。 ![singly linked list](https://img.kancloud.cn/a8/63/a863a1b7fe796e6effcb5801c40b06d7_1260x196.png "Singly linked list") 单链表 节点表示为: ``` struct node { int data; struct node *next; } ``` 可以将三个成员的单链表创建为: ``` /* Initialize nodes */ struct node *head; struct node *one = NULL; struct node *two = NULL; struct node *three = NULL; /* Allocate memory */ one = malloc(sizeof(struct node)); two = malloc(sizeof(struct node)); three = malloc(sizeof(struct node)); /* Assign data values */ one->data = 1; two->data = 2; three->data = 3; /* Connect nodes */ one->next = two; two->next = three; three->next = NULL; /* Save address of first node in head */ head = one; ``` * * * ## 双链表 我们在双向链表中添加一个指向前一个节点的指针。 因此,我们可以朝任一方向前进:前进或后退。 ![doubly linked list](https://img.kancloud.cn/46/28/46281c2c2d9934ffee0ee399f4df8d41_1614x196.png "Doubly linked list") 双链表 节点表示为 ``` struct node { int data; struct node *next; struct node *prev; } ``` 可以创建一个三人双向链表 ``` /* Initialize nodes */ struct node *head; struct node *one = NULL; struct node *two = NULL; struct node *three = NULL; /* Allocate memory */ one = malloc(sizeof(struct node)); two = malloc(sizeof(struct node)); three = malloc(sizeof(struct node)); /* Assign data values */ one->data = 1; two->data = 2; three->data = 3; /* Connect nodes */ one->next = two; one->prev = NULL; two->next = three; two->prev = one; three->next = NULL; three->prev = two; /* Save address of first node in head */ head = one; ``` * * * ## 循环链表 循环链表是链表的变体,其中最后一个元素链接到第一个元素。 这形成一个循环。 ![circular linked list](https://img.kancloud.cn/e7/37/e737690a155d2d2476ba8d311a988435_1156x244.png "Circular linked list") 循环链表 循环链表可以是单链或双链。 * 对于单链表,最后一项的下一个指针指向第一个项 * 在双向链表中,第一个项目的`prev`指针也指向最后一个项目。 可以将三元循环单链表创建为: ``` /* Initialize nodes */ struct node *head; struct node *one = NULL; struct node *two = NULL; struct node *three = NULL; /* Allocate memory */ one = malloc(sizeof(struct node)); two = malloc(sizeof(struct node)); three = malloc(sizeof(struct node)); /* Assign data values */ one->data = 1; two->data = 2; three->data = 3; /* Connect nodes */ one->next = two; two->next = three; three->next = one; /* Save address of first node in head */ head = one; ```