| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 473 人关注过本帖
标题:看下合并链表哪里出问题了
只看楼主 加入收藏
zyhfbim1a9
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2008-3-15
收藏
 问题点数:0 回复次数:1 
看下合并链表哪里出问题了
#include "stdio.h"
#include "malloc.h"
#define null 0
#define len sizeof(struct student)
int n;
struct student
{
    long num;
    long score;
    struct student *next;
};
struct student *creat(void)
{   
    struct student *p1,*p2,*head;
    head=null;
    printf("please input the students' data:\n");
    p1=p2=(struct student *)malloc(len);
    scanf("%ld %ld",&p1->num,&p1->score);
    n=0;
    while(p1->num!=0)
    {
        n++;
        if(n==1)head=p1;
        else
        {
            p2->next=p1;
            p2=p1;
        }
            p1=(struct student *)malloc(len);
            scanf("%ld %ld",&p1->num,&p1->score);
    }    
    p2->next=null;
    return(head);
}
void print(struct student *head)
{
    struct student *p;
    p=head;
    while(p!=null)
    {
        printf("%ld %ld\n",p->num,p->score);
         p=p->next;
    }
    
   
}
struct student *insert(struct student *head,struct student *stu)
{
    struct student *p1,*p2,*p;
    p1=p2=head;p=stu;
        if(head==null)head=p;
        else
        {
            while(p->num>p1->num&&p1->next!=null)
            {
                p2=p1;p1=p1->next;
            }
            if(p->num<=p1->num)
            {
                if(p==head)
                {
                    head=p;p1->next=p;
                }
                else
                {
                    p2->next=p;p->next=p1;
                }
            }
            else
            {
                p1->next=p;p->next=null;
            }

        n++;
        
    }
    
    return(head);
}
    
    
main()
{
    struct student *a,*b,*p1,*t1;
    a=creat();
    print(a);
    b=creat();
    print(b);
    p1=a;t1=b;
    while(p1!=null)
    {
        t1=insert(t1,p1);
        p1=p1->next;
    }
    printf("the %d data:",n);
    print(t1);
   
}
搜索更多相关主题的帖子: 链表 
2008-04-19 17:54
zyhfbim1a9
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2008-3-15
收藏
得分:0 
忘写题题目了,补充下:
建立两个链表 a, b,每个链表中的节点包括学号、成绩。要求把两个链表合并,按学号升序排列。
2008-04-19 18:07
快速回复:看下合并链表哪里出问题了
数据加载中...
 
   



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

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