看看 此题 与题意是否相符
写出构造有序链表的递归函数,输入的数据序列是无序的。。#include "stdio.h"
#include "stdlib.h"
struct node
{int a;
struct node *next;
};
struct node *Creat(int n)
{
int i;
struct node *h,*p,*q;
printf("请输入%d个整数:\n",n);
h=(struct node *)malloc(sizeof(struct node));
h->next=NULL;
q=h;
for(i=1;i<=n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
printf("into create node %d:\n",i);
scanf("%d",&p->a);
h->next=p;
p->next=NULL;
h=h->next;
}
return q;
}
prin(struct node *h)
{
h=h->next;
while(h!=NULL)
{
printf("%d\t",h->a);
h=h->next;
}
}
struct node *pai(struct node *h)
{
struct node *head,*p,*q,*s;
h=h->next;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
s=head;
p=(struct node *)malloc(sizeof(struct node));
p->a=h->a;
s->next=p;
p->next=NULL;
h=h->next;
while(h!=NULL)
{
q=(struct node *)malloc(sizeof(struct node));
q->a=h->a;
h=h->next;//记录下一个节点
head=s;
p=head->next;
while(head!=NULL)
{
if(head->next==NULL)
{
head->next=q;
q->next=NULL;
break;
}
else if(q->a>p->a)
{
head->next=q;
q->next=p;
break;
}
else
{head=head->next;
p=head->next;
}
}
}
return s;
}
main()
{
struct node *head;
head=Creat(5);//这里初始化了节点的个数,最多可以排序5个元素
prin(head);
head=pai(head);
printf("\n********从大到小的顺序如下*************\n");
prin(head);
]
这个题 与提议相符吗 是无序输出的吗