(1)无头头插双链表排序
(思路:在原链表中逐个比较大小,将最小的插入新的链表(插入顺序按照尾插的顺序))
#include <stdio.h>
#include <stdlib.h>
typedef struct person
{
int age;
struct person *pre;
struct person *next;
}per;
per *head_list(per *one, int num) //头插
{
per *temp = (per *)malloc(sizeof(per));
temp->age = num;
temp->pre = NULL;
if(one)
{
one->pre = temp;
temp->next = one;
}
return temp;
}
void show(per *head)
{
if(NULL == head)
{
return;
}
per *t = NULL;
while(head)
{
t = head;
printf("age is %d\n",head->age);
head = head->next;
}
printf("=======================\n");
while(t)
{
printf("age is %d\n",t->age);
t = t->pre;
}
}
void *sort(per *head) //排序
{
if(NULL == head)
{
return;
}
if(NULL == head->next)
{
printf("min is head\n");
}
per *min = head;
per *tmp = head;
per *new_list = NULL;
per *tail_sort = NULL;
while(head)
{
min = head;
tmp = head;
while(tmp->next)
{
if(min->age > tmp->next->age)
{
min = tmp->next;
min->pre = tmp;
}
tmp = tmp->next;
}
if(min == head)
{
head = head->next;
}
else
if(NULL == min->next) //如果最小的是最后一个
{
min->pre->next = NULL;
}
else
{
min->pre->next = min->next;
min->next->pre = min->pre;
}
//按尾插顺序组成新链表
min->pre = NULL;
min->next = NULL;
if(NULL == new_list)
{
tail_sort = min;
new_list = tail_sort;
}
else
{
tail_sort->next = min;
min->pre = tail_sort;
tail_sort = min;
}
}
return new_list;
}
int main()
{
int i = 0;
per *head = NULL;
while(i++ < 6)
{
head = head_list(head,rand () % 100); //rand() 伪随机函数,产生100以内的随机数
}
show(head);
printf("=========after sort==========\n");
head = sort(head);
show(head);
return 0;
}
(2)无头尾插双链表排序
(思路:在原链表中逐个比较大小,将最小的插入新的链表(插入顺序按照头插的顺序))
#include <stdio.h>
#include <stdlib.h>
typedef struct person
{
int age;
struct person *pre;
struct person *next;
}per;
per *tail_list(per *one, int num) //尾插
{
per *temp = (per *)malloc(sizeof(per));
temp->age = num;
if(NULL == one)
{
return temp;
}
per *head = one;
per *t = one;
while(one->next)
{
t = one->next;
one = one->next;
}
one->next = temp;
temp->pre = t;
return head;
}
void show(per *head)
{
if(NULL == head)
{
return;
}
per *t = NULL;
while(head)
{
t = head;
printf("age is %d\n",head->age);
head = head->next;
}
printf("==========================\n");
while(t)
{
printf("age is %d\n",t->age);
t = t->pre;
}
}
void *sort(per *head) //排序
{
if(NULL == head)
{
return;
}
if(NULL == head->next)
{
printf("min is head\n");
return;
}
per *min = head;
per *tmp = head;
per *new_list = NULL;
while(head)
{
min = head;
tmp = head;
while(tmp->next)
{
if(min->age > tmp->next->age)
{
min = tmp->next;
min->pre = tmp;
}
tmp = tmp->next;
}
if(min == head)
{
head = head->next;
}
else
if(NULL == min->next)
{
min->pre->next = NULL;
}
else
{
min->pre->next = min->next;
min->next->pre = min->pre;
}
//按头插顺序组成新新链表
min->pre = NULL;
min->next = new_list;
if(NULL != new_list)
{
new_list->pre = min;
}
new_list = min;
}
return new_list;
}
int main()
{
int i = 0;
per *head = NULL;
while(i++ < 6)
{
head = tail_list(head,rand() % 100); //rand() 伪随机数函数,生成100以内的随机数
}
show(head);
printf("=======after sort========\n");
head = sort(head);
show(head);
return 0;
}