數據結構 | 雙向循環鏈表實現及圖示


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

雙向循環鏈表 //遍歷等執行方法與普通雙向鏈表相同,不單獨列舉

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

初始化+尾插法

圖示:

實現代碼

 1 /* 初始化雙向循環鏈表,head的頭尾均指向本身 */
 2 void InitList(pNode **head)
 3 {
 4     pNode *p;
 5     *head = (pNode *)malloc(sizeof(pNode));
 6     if ((*head) == NULL)
 7         exit(0);
 8     (*head)->next = (*head);
 9     (*head)->prev = (*head);
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;
36     InsertList(head);
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     }
11     (*head)->next = (*head)->prev = (*head);//頭節點的兩個指針域指向自身
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     }
23     free(*head);
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);
17     (*head)->next = (*head);
18     (*head)->prev = (*head);
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;
45     InsertList(head);
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     }
69     (*head)->next = (*head)->prev = (*head);//頭節點的兩個指針域指向自身
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     }
81     free(*head);
82     (*head) = NULL;
83 }
84 int main(int argc, char const *argv[])
85 {
86     pNode *head, *last;
87     InitList(&head);
88     last = InsertList(&head);
89     PrintList(head);
90     ClearList(&head);
91     printf("%p %p %p\n", head, head->next, head->prev); //驗證是否頭節點指向自身
92     DestroyList(&head);
93     printf("%p\n",head);//驗證是否已經完全銷毀
94     return 0;
95 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM