急急急!!!!!!!C语言链表插入排序问题

来源:百度知道 编辑:UC知道 时间:2024/06/23 20:49:01
这是C语言上级的程序填空题,输入一个数然后通过链表让其从大到小排序,高手帮忙做下
#include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
void fun( SLIST *h, int x)
{ SLIST *p, *q, *s;
s=(SLIST *)malloc(sizeof(SLIST));
/**********found**********/
s->data=x;
q=h;
p=h->next;
while(p!=NULL && x>p->data) {
/**********found**********/
q=p;
p=p->next;
}
s->next=p;
/**********found**********/
q->next=0;
}
SLIST *creatlist(int *a)
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; i<N; i++)
{ q=(SLIST *)malloc(sizeof(SLIST));
q->data=a[i]; p->next=q; p=q;
}
p->next=0;
return h;
}
void outlist(SLIST *h)
{ SLIST *p;
p=h->next;
if (

下面的答案测试结果是:x可以插入链表,保持从小到大有序
found1.
if (h==NULL || s==NULL) return;
found2.
s->next = NULL; // 这里可以不填
found3.
q->next = s; while(q->next!=0){q = q->next;} // 为了防止下面有一句错误

如果不仅插入,而且让链表换一个方向(如果原来从小到大)
就有以下答案,经过测试,在有头结点的链表中使用,不正确
found1.
if (h==NULL || s==NULL) return; s->next = h;
found2.
q->next = s->next; s->next = q; // 不知是否可以调换链表的排序方向
found3.
while(p!=NULL){ s->next = q; q = s; s=p; p = p->next; } q = h; h = s;