带头结点的循环单链表----------C语言

  1 /*****************************************************
  2 Author:Simon_Kly    Version:0.1    Date: 20170520
  3 Description:循环单链表
  4 Mail:degaullekong@gmail.com
  5 Funcion List: 
  6 *****************************************************/
  7 
  8 #include <stdio.h>
  9 #include <stdlib.h>
 10 
 11 typedef struct node
 12 {
 13     int data;
 14     struct node *next;
 15 }Node, *Link;
 16 
 17 /*判断malloc是否成功执行*/
 18 void is_malloc_ok(Link node)
 19 {
 20     if(node == NULL)
 21     {
 22         printf("malloc error!\n");
 23         exit(-1);
 24     }
 25 }
 26 
 27 /*创建单链表*/
 28 void create_link(Link * head)
 29 {
 30     *head = (Link)malloc(sizeof(Node));
 31     is_malloc_ok(*head);
 32     (*head)->next = *head;
 33 }
 34 
 35 /*创建节点*/
 36 void create_node(Link * new_node)
 37 {
 38     *new_node = (Link)malloc(sizeof(Node));
 39     is_malloc_ok(*new_node);
 40     (*new_node)->next = NULL;
 41 }
 42 
 43 /*插入节点尾插法*/
 44 void insert_node_tail(Link head, Link new_node)
 45 {
 46     Link p = NULL;
 47 
 48     p = head;
 49 
 50     while (p->next != head)
 51     {
 52         p = p->next;
 53     }
 54     new_node->next = head;
 55     p->next = new_node;
 56 }
 57 
 58 /*插入节点头插*/
 59 void insert_node_head(Link head, Link new_node)
 60 {
 61     new_node->next = head->next;
 62     head->next = new_node;
 63 }
 64 
 65 /*输出链表*/
 66 void output_link(Link head)
 67 {
 68     Link p = NULL;
 69 
 70     if (head == NULL)
 71     {
 72         printf("link is not exist!\n");
 73         return ;
 74     }
 75     else if(head->next == head)
 76     {
 77         printf("link is empty!\n");
 78         return ;
 79     }
 80 
 81     p = head->next;
 82 
 83     while (p != head)
 84     {
 85         printf("%d\n", p->data);
 86         p = p->next;
 87     }
 88 }
 89 
 90 /*置为空链*/
 91 void make_link_empty(Link *head)
 92 {
 93     Link p = NULL;
 94 
 95     p = (*head)->next;
 96 
 97     while (*head != (*head)->next)
 98     {
 99         (*head)->next = (*head)->next->next;
100         free(p);
101         p = (*head)->next;
102     }
103 }
104 
105 /*销毁链*/
106 void release_link(Link * head)
107 {
108     make_link_empty(head);
109     free(*head);
110     *head = NULL;
111 }
112 
113 int main()
114 {
115     int i;
116     Link head = NULL;
117     Link new_node = NULL;
118     
119     output_link(head);
120     create_link(&head);
121     output_link(head);
122     for (i = 0; i < 10; i++)
123     {
124         create_node(&new_node);
125         new_node->data = i + 1;
126         insert_node_tail(head, new_node);
127     }
128     output_link(head);
129 
130     create_node(&new_node);
131     new_node->data = 20;
132     insert_node_head(head, new_node);
133     output_link(head);
134 
135     release_link(&head);
136 
137     output_link(head);
138     return 0;
139 }

循环单链表的于单链表唯一的不同:在对链表中添加或者删除节点时一定要保持链表的头尾连接,在遍历时不能再以NULL为循环结束判断条件,应该使用头和尾相等来判断整个链表结束。

posted @ 2017-05-22 17:26  SimonKly  阅读(1264)  评论(0编辑  收藏  举报