数据结构 | 循环链表实现

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

循环单链表

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

尾指针是指向终端结点的指针,用它来表示单循环链表可以使得查找链表的开始结点和终端结点方便。

设置尾指针因为开始结点和终端结点查询的时间复杂度都是O(1)。而设置头指针,查找终端结点的时间复杂度为O(n)。

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

结构体定义://其他操作方法与单链表相似,不一一列举

代码实现

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 typedef int ElemType;
 5 typedef int Status;
 6 struct LNode
 7 {
 8     ElemType data;
 9     struct LNode *next;
10 };

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

结点初始化/插入结点:

代码实现:

 1 void InitList_CL(struct LNode **L)
 2 {
 3     *L = (struct LNode *)malloc(sizeof(struct LNode));
 4     if (!*L)
 5         exit(0);
 6     (*L)->next = (*L); //指针域指向头结点
 7 }
 8 int ListInsert_CL(struct LNode **L)
 9 {
10     int n, number, i = 0;
11     printf("The input position n:");
12     scanf("%d", &n);
13     printf("The input digital number:");
14     scanf("%d", &number);
15     if (number < 1)
16     {
17         return 0;
18     }
19     struct LNode *p, *s;
20     p = (*L)->next;
21     while(i < n - 1)
22     {
23         p = p->next;
24         i++;
25     }
26     s = (struct LNode *)malloc(sizeof(struct LNode));
27     s->data = number;
28     s->next = p->next;
29     p->next = s;
30     if(p == *L)
31     {
32         *L = s;
33     }
34     ListInsert_CL(L);
35 }

 

图解:当输入位置为1,值为1 2 3 4时过程如下:

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

结点打印:

打印时指针指向头结点,循环打印,直到指向尾节点L

代码实现:

 1 void ListPrint_CL(struct LNode *L)
 2 {
 3     struct LNode *p;
 4     p = L->next;
 5     while(p != L)
 6     {
 7         p = p->next;
 8         printf("%d ", p->data);
 9     }
10     printf("\n");
11 }

 

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

完整代码

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 typedef int ElemType;
 5 typedef int Status;
 6 struct LNode
 7 {
 8     ElemType data;
 9     struct LNode *next;
10 };
11 void ListEmpty_CL(struct LNode *L)
12 {
13     if (L->next == L) //当指针域指向头结点时证明没有其他结点,链表为空
14     {
15         printf("The list is empty\n");
16     }
17 }
18 void InitList_CL(struct LNode **L)
19 {
20     *L = (struct LNode *)malloc(sizeof(struct LNode));
21     if (!*L)
22         exit(0);
23     (*L)->next = (*L); //指针域指向头结点
24 }
25 int ListInsert_CL(struct LNode **L)
26 {
27     int n, number, i = 0;
28     printf("The input position n:");
29     scanf("%d", &n);
30     printf("The input digital number:");
31     scanf("%d", &number);
32     if (number < 1)
33     {
34         return 0;
35     }
36     struct LNode *p, *s;
37     p = (*L)->next;
38     while(i < n - 1)
39     {
40         p = p->next;
41         i++;
42     }
43     s = (struct LNode *)malloc(sizeof(struct LNode));
44     s->data = number;
45     s->next = p->next;
46     p->next = s;
47     if(p == *L)
48     {
49         *L = s;
50     }
51     ListInsert_CL(L);
52 }
53 void ListPrint_CL(struct LNode *L)
54 {
55     struct LNode *p;
56     p = L->next;
57     while(p != L)
58     {
59         p = p->next;
60         printf("%d ", p->data);
61     }
62     printf("\n");
63 }
64 int main(int argc, char const *argv[])
65 {
66     struct LNode *L;
67     int j;
68     Status i;
69     InitList_CL(&L); //初始化链表
70     ListEmpty_CL(L); //判断链表是否为空
71     ListInsert_CL(&L); //在链表中第n个位置插入元素
72     ListPrint_CL(L); //打印链表
73     return 0;
74 }

 

posted @ 2017-04-28 20:14  hugh.dong  阅读(901)  评论(0编辑  收藏  举报