带头结点的双向循环链表----------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 *prior, *next; 15 }Node, *Link; 16 17 void is_malloc_ok(Link head) 18 { 19 if (head == NULL) 20 { 21 printf("malloc is error!\n"); 22 exit(-1); 23 } 24 } 25 26 /*创建链表*/ 27 void create_link(Link * head) 28 { 29 *head = (Link)malloc(sizeof(Node)); 30 31 is_malloc_ok(*head); 32 33 (*head)->next = (*head)->prior = *head;//让其指向自己 34 } 35 36 /*创建节点*/ 37 void create_node(Link * new_node) 38 { 39 *new_node = (Link)malloc(sizeof(Node)); 40 is_malloc_ok(*new_node); 41 } 42 43 /*插入节点(头插)*/ 44 void insert_node_head(Link head, Link new_node) 45 { 46 new_node->prior = head; 47 new_node->next = (head)->next; 48 (head)->next->prior = new_node; 49 (head)->next = new_node; 50 } 51 52 53 /*插入节点(尾插)*/ 54 void insert_node(Link * head, Link new_node) 55 { 56 Link p = NULL; 57 58 p = *head; 59 while (p->next != *head) 60 { 61 p = p->next; 62 } 63 64 new_node->prior = p; 65 new_node->next = p->next; 66 p->next->prior = new_node; 67 p->next = new_node; 68 } 69 70 /*输出链表*/ 71 void out_link(Link head) 72 { 73 Link p = NULL; 74 #if 0 75 if (head->next == head) 76 { 77 printf("link is empty\n"); 78 return ; 79 } 80 #endif 81 82 if (head == NULL) 83 { 84 printf("link is empty\n"); 85 return ; 86 } 87 88 p = (head)->next; 89 90 while (p != head) 91 { 92 printf("%d\n", p->data); 93 p = p->next; 94 } 95 putchar(10); 96 } 97 98 /*变成空链*/ 99 void make_empty(Link * head) 100 { 101 Link p = NULL; 102 103 p = (*head)->next; 104 105 while (*head != (*head)->next) 106 { 107 (*head)->next = p->next;//head移动 108 free(p);//释放head之前的节点 109 p = (*head)->next; 110 } 111 } 112 113 /*释放链表*/ 114 void release_link(Link * head) 115 { 116 make_empty(head); 117 118 free(*head); 119 120 *head = NULL; 121 } 122 123 /*删除节点*/ 124 void delete_node(Link * head, int data) 125 { 126 Link p = NULL; 127 128 p = (*head)->next; 129 130 if (p == *head)//空链 131 { 132 printf("link is empty!\n"); 133 return ; 134 } 135 136 /*非空链*/ 137 while (p != *head && p->data != data) 138 { 139 p = p->next;//找那个节点 140 } 141 142 if (p == *head)//找一圈没找到 143 { 144 printf("No such node!\n"); 145 return ; 146 } 147 else//找到并改变指向 148 { 149 p->prior->next = p->next; 150 p->next->prior = p->prior; 151 152 free(p); 153 } 154 } 155 156 void insert_node_seq(Link * head, Link new_node) 157 { 158 Link p = NULL; 159 160 p = (*head); 161 162 while (p->next != *head && new_node->data < p->next->data)//找到该位置的前一个位置 163 { 164 p = p->next; 165 } 166 #if 0 167 if (p == *head)//找到尾部 168 { 169 new_node->prior = p->prior; 170 new_node->next = p; 171 p->prior->next = new_node; 172 p->prior = new_node; 173 return ; 174 } 175 #endif 176 new_node->prior = p; 177 new_node->next = p->next; 178 p->next->prior = new_node; 179 p->next = new_node; 180 181 } 182 183 int main() 184 { 185 int i; 186 Link head = NULL; 187 Link new_node = NULL; 188 create_link(&head); 189 190 out_link(head); 191 192 for (i = 0; i < 10; i++) 193 { 194 create_node(&new_node); 195 new_node->data = i + 1; 196 // insert_node(&head, new_node); 197 insert_node_head(head, new_node); 198 } 199 200 out_link(head); 201 202 #if 1 203 printf("please input i\n"); 204 scanf("%d", &i); 205 putchar(10); 206 create_node(&new_node); 207 new_node->data = i; 208 insert_node_seq(&head, new_node); 209 out_link(head); 210 #endif 211 212 #if 0 213 delete_node(&head, 2); 214 out_link(head); 215 #endif 216 release_link(&head); 217 out_link(head); 218 return 0; 219 }