单链表排序急!!!!!!!!!!!!!!

来源:百度知道 编辑:UC知道 时间:2024/05/28 13:13:50
/******************************************

用任何方法实现带头结点单链表的排序
初始化和现实部分已给出
要求添加并编写排序函数!!!!!

******************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define LEN sizeof(struct NODE)
#define MAX 10

typedef struct NODE
{
int x;
struct NODE *next;
}node;

node* initial();
void show(node*);
//在此声明并定义排序函数!!!!!!!!!!!!!!!!

void main()
{

node *head = initial();

printf("origin:\t");
show(head);

//添加排序函数!!!!!!!!!!!!!!!!!

printf("sort:\t");
show(head);

}

node* initial()
{
int i;
node *head,*p,*q;
head = p = q = (node*)malloc(LEN);
head ->next = NULL;

head ->x = -1;

srand(time(NULL));
for(i=0; i<MAX; i++)
{
q

//你要升序还是逆序排列啊,我这个是升序的,逆序你自己改,我这vc6坏了,你自己调试下,方法是对的
void sort(node *h)
{
node *p,*q,*hs= (node*)malloc(LEN);
//建立空表hs
hs->next=NULL;
hs->x=-1;
while(h->next)
{
//获得表h的第一个节点
p=h->next;
h->next=p->next;
q=hs;
//排序并入表
while(q->next)
{
//插入在中间
if(p->x<=q->next->x){p->next=q->next;q->next=p;break;}
q=q->next;
}
//插入在末尾
if(!q->next){q->next=p;p->next=NULL;}
}
h->next=hs->next;
}

//---------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define LEN sizeof(struct NODE)
#define MAX 10

typedef struct NODE
{
int x;
struct NODE *next;
}node;

node* initial();
void show(node*);
node * nsort(node