| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 485 人关注过本帖
标题:急求高人指点!!!!!!
只看楼主 加入收藏
悠悠我心
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-6-24
收藏
 问题点数:0 回复次数:2 
急求高人指点!!!!!!
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{int num;
 struct student *next;
};
int n;
int i;

struct student *creat(void)  //建立链表函数
{struct student *head;
 struct student *p1,*p2;
 n=0;
 p1=p2=( struct student* )malloc(LEN);
 scanf("%d",&p1->num);
 head=NULL;
 while(p1->num)
   {n=n+1;
    if(n==1)head=p1;
    else p2->next=p1;
    p2=p1;
    p1=( struct student* )malloc(LEN);
    scanf("%d",&p1->num);
    }
  p2->next=NULL;
  return(head);
 }

 void print(struct student *head)//打印出数据函数
   {struct student* p;
    printf("\nNow,These %d numbers are:\n",n);
    p=head;
    if(head!=NULL)
      do
       {printf("%d ",p->num);
        p=p->next;
       }
       while(p!=NULL);
    }

  struct student *paixv(struct student *head)//给输入数据排序函数
  {struct student *p1,*p2;
   if(head==NULL) {printf("\nlist null!\n");}
   p1=head;
   for(i=0;i<=n;i++)
   {
     while(p1->next!=NULL)
       {if(p1->num>p1->next->num)
         {n=p1->num;p1->num=p1->next->num;p1->next->num=n;
         }    
         p1=p1->next;
       }
     if(p1->next==NULL)
       p1=head;
   }
   return(head);
  }

  struct student *complex(struct student *head,struct student *stud)//两个链表合并
    {struct student *p0,*p1,*p2;
     p1=head;
     p0=stud;
     if(head==NULL)
       {head=p0;p0->next=NULL;}
     else
       {while((p0->num>p1->num)&&(p1->next!=NULL))
          {p2=p1;
           p1=p1->next;
          }
        if(p0->num<=p1->num)
          {if(head==p1){head=p0;p0=p0->next;}
           else {p2->next=p0;p0=p0->next;p0->next=p1;}
          }
        else
          {p1->next=p0;p0->next=NULL;}
       }
         n=n+1;
         return(head);
    }
void main()
    {   struct student *head,*stu;
        printf("input numbers:\n");
        head=creat(); //第一次调用建表函数
        printf("\ninput another numbers:");
        stu=creat();  //第二次调用
        print(head);
        paixv(head);
        printf("\n输入数据非降序排列后为:");
        print(head);
        print(stu);
        paixv(stu);
        printf("\n输入数据非降序排列后为:");
        print(stu);
        complex(head,stu);
        print(head);
    }

 急求高人指点,该程序目的在于将两个自由创建的链表合并到一个链表中,其可以编译成功,但第一次调用见表时可以弹出提示输入数据,而第二次却不能弹出,即是只能有一次数据输入,第一次输入完毕后,第二次直接跳过,默认链表为空了!
搜索更多相关主题的帖子: c语言 链表 
2008-06-24 07:11
himpo
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:192
专家分:123
注 册:2008-5-16
收藏
得分:0 
你在第一次建表后加入一个print函数输出再调用creat()

我不知道是不是缓冲区的问题,第二次调用时默认你输入的为0而直接结束了。

我手边没有编译器,瞎猜的。。
2008-06-24 17:33
chinaxy11
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-11-14
收藏
得分:0 
楼上是正解。。你重新调用create()时要释放掉内存中原来的数据,用print()很好。
还有,,你那合并有问题。
2008-06-24 22:19
快速回复:急求高人指点!!!!!!
数据加载中...
 
   



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

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