| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1575 人关注过本帖
标题:新手请教:把一个链表插入到另一个链表中,写好的程序运行不了.谢谢
取消只看楼主 加入收藏
friendbcc008
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2013-10-4
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
新手请教:把一个链表插入到另一个链表中,写好的程序运行不了.谢谢
#include<stdio.h>

struct stu
{
  long num;
  float score;
  struct stu *next;

};

struct stu * insert(struct stu *head1,struct stu *head2)  //两表都不为空,把head2表插入到head1中,按num的大小插入到合适的位置上,最后形成新的链表.
{
  struct stu *p1,*p2,*pnew;      //p1,p2用于head1中,pnew用于head2中
  
    pnew=head2;
    while(pnew!=NULL)
     {  p1=head1;
        while((pnew->num>p1->num)&&(p1->next!=NULL))
        {
            p2=p1;
            p1=p1->next;
        }
        if(pnew->num<=p1->num)
        {
            pnew->next=p1;              
            if(p1==head1) head1=pnew;
            else p2->next=pnew;
        }
        else
        {
            p1->next=pnew;
            pnew->next=NULL;
        }
        
        
        pnew=pnew->next;  
     }
 
  
  return head1;

}

int main()
{
  struct stu *head3,*head4,*p,*p0,*qp=NULL;
  struct stu a,b,c,d,e,f;
  a.num=1002;a.score=80;
  b.num=1004;b.score=76;
  c.num=1005;c.score=92;
  d.num=1001;d.score=88;
  e.num=1003;e.score=70;
  f.num=1006;f.score=69;
 head3=&a;a.next=&b;b.next=&c;c.next=NULL;
 head4=&d;d.next=&e;e.next=&f;f.next=NULL;
 
 p=head3;p0=head4;
 while(p)
 {
   printf("%ld %5.1f\n",p->num,p->score);  //先输出head1表
   p=p->next;
 }
 while(p0)
 {
   printf("%ld %5.1f\n",p0->num,p0->score); //再输出head2表
   p0=p0->next;
 }
 qp=insert(head3,head4);       //这个有问题,请帮忙看下.
 while(qp)
 {
   printf("%ld %5.1f\n",qp->num,qp->score); //再输出两表按num从小到大合并好的表
   qp=qp->next;
 }
 printf("sucsess!\n");
 return 0;
}
搜索更多相关主题的帖子: include insert 
2013-10-04 15:12
friendbcc008
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2013-10-4
收藏
得分:0 
有人吗?版主给看下呀...晕..想了一天都不明白哪里错了...
2013-10-04 17:20
快速回复:新手请教:把一个链表插入到另一个链表中,写好的程序运行不了.谢谢
数据加载中...
 
   



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

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