当前位置: 代码迷 >> C语言 >> 没人帮忙哦,飞燕帮我来看下哪里错了啊
  详细解决方案

没人帮忙哦,飞燕帮我来看下哪里错了啊

热度:346   发布时间:2008-04-19 18:27:13.0
没人帮忙哦,飞燕帮我来看下哪里错了啊
建立两个链表 a, b,每个链表中的节点包括学号、成绩。要求把两个链表合并,按学号升序排列。
#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 *p)
{
    struct student *p1,*p2;
    p1=p2=head;
        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;
    a=creat();
    print(a);
    b=creat();
    print(b);
    p1=a;
    while(p1!=null)
    {
        b=insert(b,p1);
        p1=p1->next;
    }
    printf("the %d data:",n);
    print(b);
   
}
搜索更多相关的解决方案: 飞燕  student  struct  链表  

----------------解决方案--------------------------------------------------------
你在没有排序的情况下就插入是不对的


----------------解决方案--------------------------------------------------------
困扰一个下午了,==到花儿也谢鸟,
燕子快来啊。。。。
----------------解决方案--------------------------------------------------------
我这个程序本身也有错误
比如我先输入两个已排好序的链表:
链表a                  1 11
                       3 33
                       5 55
                       0 0

然后输入b              2 22
                       4 44
                       6 66
                       0 0
然后就是个光标在在哪里,没什么反应了,不知道怎么回事
----------------解决方案--------------------------------------------------------
我知道了,你等一下


----------------解决方案--------------------------------------------------------
#include "stdio.h"
#include "malloc.h"
#define null 0
#define len sizeof(struct student)
struct student
{
    long num;
    long score;
    struct student *next;
};
struct student *creat(void) //此函数重大修改
{
    struct student *p1,*p2,*head;
    long n,s;
    printf("please input the students' data:\n");
    head=p1=p2=(struct student *)malloc(len);
    while(scanf("%ld %ld",&n,&s)!=EOF && n!=0)
    {
        p1=(struct student *)malloc(len);
        p1->num=n;    p1->score=s;
        p2->next=p1;p2=p1;
    }
    p2->next=null;
    return head;
}
void print(struct student *head)
{
    struct student *p;
    for(p=head->next; p!=null; p=p->next)//此处有修改
    {
        printf("%ld %ld\n",p->num,p->score);
    }
}
//此函数几乎重写,无排序的排入,你自己加排序吧
struct student *insert(struct student *head,struct student *p)
{
    struct student *p1,*p2;
    p1=head;
    if(head==null)head=p;
    else if(p1==null)
    {
        p->next=null;
        p1->next=p;
    }
    else
    {
        for(; ; p1=p1->next)
        {
            p2 = p1->next;
            if(p1->next == NULL)
            {
                p->next=null;
                p1->next=p;
            }
            else if(p2->num > p->num)
            {
                p->next = p2;
                p1->next = p;
            }
            else continue;
            break;
        }
    }
    return head;
}
int main()
{
    struct student *a,*b,*p1,*p2;
    a=creat();
    print(a);
    b=creat();
    print(b);
    for(p1=a->next; p1!=null; p1=p2) //此处有修改
    {
        p2=p1->next;
        insert(b,p1);
    }
    printf("the data:\n");
    print(b);
    return 0;
}



----------------解决方案--------------------------------------------------------
链表..........
----------------解决方案--------------------------------------------------------
链表..........是什么啊?作用大吗?
----------------解决方案--------------------------------------------------------
链表,链表。。。
链表就是由很多个结构体数据组成,每个结构体数据的内部成员中有一个指向下一个结构体数据的指针变量,从而使所有的结构体数据一个接一个的关联起来。每个结构体数据就像一个节点,只要知道到其中一个,就可以找到其他的节点。
   兄弟,我刚看完结构体和链表,了解不够深入,反正自己就是这样认为了。有机会自己去看下吧。
----------------解决方案--------------------------------------------------------
链表?C语言实现起来不太容易啊,用C++的类进行封装感觉会更好!
6#的朋友是用了什么软件把代码着色的啊?
----------------解决方案--------------------------------------------------------
  相关解决方案