| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 213 人关注过本帖
标题:看看 此题 与题意是否相符
只看楼主 加入收藏
cl542454299
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-6-12
结帖率:100%
收藏
 问题点数:0 回复次数:0 
看看 此题 与题意是否相符
写出构造有序链表的递归函数,输入的数据序列是无序的。。
#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);
]
这个题 与提议相符吗  是无序输出的吗
搜索更多相关主题的帖子: include return create 
2011-07-04 12:31
快速回复:看看 此题 与题意是否相符
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017346 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved