链表的插入排序

来源:互联网 发布:淘宝德国铁元是真的吗 编辑:程序博客网 时间:2024/05/12 01:20


示例程序:

#include <stdio.h>#include <stdlib.h>typedef struct node* link;struct node{int item;link next;};int main(){//创建两个哑节点  不存放数据struct node heada;struct node headb;headb.next = NULL;int N;int i = 0;link tmp = &heada;;scanf("%d",&N);for(i = 0; i < N; i++){link t = (link)malloc(sizeof(*t));t->next = NULL;t->item = rand()%1000;tmp->next = t;tmp = t;}link tmpb = &headb;link t;for(tmp = heada.next;tmp != NULL;){t = tmp->next;for(tmpb = &headb;tmpb->next != NULL;tmpb=tmpb->next){if(tmpb->next->item > tmp->item)break;}tmp->next = tmpb->next;tmpb->next = tmp;tmp = t;}for(tmpb = headb.next;tmpb != NULL;tmpb= tmpb->next){printf("%d\n",tmpb->item);}}


0 0
原创粉丝点击