单链表
typedef struct node
{
int data;
struct node *next;
}node,*linklist_s;
单链表的冒泡排序
void sort(node *head)
{
linklist_t temp = head;
linklist_t p = head->next;
while(head->next)
{
while(temp->next)
{
if(temp->data>temp->next->data)
{
int tmp = 0;
tmp = temp->data;
temp->data = temp->next->data;
temp->next->data = tmp;
}
temp = temp->next;
}
temp = p;
head = head->next;
}
}
双链表的插入排序
typedef struct node{
T data;//数据域
struct node *prev;//前置指针
struct node *next;//后置指针
}dlist,*dlist_t;
//排序
void sort(dlist_t head)
{
dlist_t tmp = head->next,p = NULL,q = NULL;
//将链表置空
head->next = head;
head->prev = head;
while(tmp!=head){
//保存原链表中tmp的下一个节点
q = tmp->next;
//找到tmp插入的位置
for(p=head->prev;p!=head&&p->data>tmp->data;p=p->prev);
//将tmp插入到p之后
tmp->prev = p;
tmp->next = p->next;
p->next->prev = tmp;
p->next = tmp;
//将tmp指向要插入的下一个节点
tmp = q;
}
}