# 数据结构 | 双向循环链表实现及图示

————————————————————————————————————————————

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

``` 1 /* 初始化双向循环链表，head的头尾均指向本身 */
2 void InitList(pNode **head)
3 {
4     pNode *p;
5     *head = (pNode *)malloc(sizeof(pNode));
6     if ((*head) == NULL)
7         exit(0);
10 }
11 /* 插入，在链表第n个位置插入元素 */
12 pNode *InsertList(pNode **head)
13 {
14     pNode *p, *s;
15     int i = 0;
16     p = (*head)->prev; //p始终指向尾节点
17     int n;
18     printf("The input to the position of the insert:");
19     scanf("%d", &n);
20     s = (pNode *)malloc(sizeof(pNode));
21     if (s == NULL)
22         exit(0);
23     printf("Input to insert element value:");
24     scanf("%d", &s->data);
25     if (s->data <= 0)
26         return p;
27     while(i < n - 1)
28     {
29         i++;
30         p = p->next;
31     }
32     s->prev = p;
33     s->next = p->next;
34     p->next->prev = s;
35     p->next = s;
37 }```

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

``` 1 /* 清空链表，保留头指针 */
2 void ClearList(pNode **head)
3 {
4     pNode *p;
5     p = (*head)->next;
6     while(p != (*head))
7     {
8         p = p->next;
9         free(p->prev);
10     }
12 }
13 /* 彻底销毁链表，头指针置空 */
14 void DestroyList(pNode **head)
15 {
16     pNode *p;
17     p = (*head)->next;
18     while(p != (*head))
19     {
20         p = p->next;
21         free(p->prev);
22     }
24     (*head) = NULL;
25 }```

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

``` 1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 typedef struct Node pNode;
5 typedef struct Node
6 {
7     int data;
8     struct Node *prev, *next;
9 } Node;
10 /* 初始化双向循环链表，head的头尾均指向本身 */
11 void InitList(pNode **head)
12 {
13     pNode *p;
14     *head = (pNode *)malloc(sizeof(pNode));
15     if ((*head) == NULL)
16         exit(0);
19 }
20 /* 插入，在链表第n个位置插入元素 */
21 pNode *InsertList(pNode **head)
22 {
23     pNode *p, *s;
24     int i = 0;
25     p = (*head)->prev; //p始终指向尾节点
26     int n;
27     printf("The input to the position of the insert:");
28     scanf("%d", &n);
29     s = (pNode *)malloc(sizeof(pNode));
30     if (s == NULL)
31         exit(0);
32     printf("Input to insert element value:");
33     scanf("%d", &s->data);
34     if (s->data <= 0)
35         return p;
36     while(i < n - 1)
37     {
38         i++;
39         p = p->next;
40     }
41     s->prev = p;
42     s->next = p->next;
43     p->next->prev = s;
44     p->next = s;
46 }
47 /* 遍历打印 */
48 void PrintList(pNode *head)
49 {
50     pNode *p;
51     p = head->next;//从头结点之后开始循环打印
52     while(p != head)
53     {
54         printf("%d ", p->data);
55         p = p->next;
56     }
57     printf("\n");
58 }
59 /* 清空链表，保留头指针 */
60 void ClearList(pNode **head)
61 {
62     pNode *p;
63     p = (*head)->next;
64     while(p != (*head))
65     {
66         p = p->next;
67         free(p->prev);
68     }
70 }
71 /* 彻底销毁链表，头指针置空 */
72 void DestroyList(pNode **head)
73 {
74     pNode *p;
75     p = (*head)->next;
76     while(p != (*head))
77     {
78         p = p->next;
79         free(p->prev);
80     }
82     (*head) = NULL;
83 }
84 int main(int argc, char const *argv[])
85 {
86     pNode *head, *last;
88     last = InsertList(&head);