双链表安插自动排序
双链表插入自动排序
今天开了一上午无聊的会议,利用一些时间想了一下,双列表插入自动排序问题。 这双链表还是花了我很多时间,早上测试结果,总是不对是因为我的list_node输出造成的,让我误以为打印结果不对。 1 /* doublylinkedlist.c */ 2 #include <stdlib.h> 3 #include <stdio.h> 4 #include "doublylinkedlist.h" 5 6 struct node tailsentinel; 7 struct node headsentinel = {0, NULL, &tailsentinel}; 8 struct node tailsentinel = {0, NULL, &headsentinel}; 9 10 static link head = &headsentinel; 11 static link tail = &tailsentinel; 12 56 void insert_sort(link p) 57 { 58 link node = head; 59 for(;node->next != tail && node->next->item < p->item; node=node->next); 60 p->next = node->next; 61 p->pre = node; 62 node->next->pre=p; 63 node->next = p; 64 }
空列表情况:
非空是 插入情况: