| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1866 人关注过本帖
标题:链表循环插入数据遇到问题,请神帮助!
只看楼主 加入收藏
弟大勿勃
Rank: 2
等 级:论坛游民
帖 子:186
专家分:59
注 册:2014-4-17
收藏
得分:0 
回复 8楼 xzlxzlxzl
对了,还有个问题。您的add函数写的很巧妙,但我改了我的add函数,为什么还是不对呢?我感觉我的思路应该也能成功才对啊?
下面是我改的add,希望能告诉我到底哪里写得不合适才会出错的?
程序代码:
struct student *add(struct student*head,struct student *stu1)
{
    struct student *p1,*p0,*p2;
    p1=head;
    p0=(struct student*)malloc(LEN);
    p0=stu1;
    p0->next=NULL;
    if(head==NULL)
    {
        head=p0;
       
       
    }
    else
    {
        while ((p1!=NULL)&&(p0->num>p1->num))
    {
        p2=p1;
        p1=p1->next;
    }
                                                                                                        
        if(p0->num<=p1->num )
        {
            if(p1==head)
            {
                head=p0;
                p0->next=p1;       
            }
            else
            {
                p2->next=p0;
                p0->next=p1;
           
            }
           
        }
        else
        {
            p1->next=p0;
           
        }
   
   
    }
         printf("\nDelete No: %d succeed!\n", p0->num);
            n = n+1;
    return(head);
}

2016-09-26 16:16
弟大勿勃
Rank: 2
等 级:论坛游民
帖 子:186
专家分:59
注 册:2014-4-17
收藏
得分:0 
希望还有好人出来帮我看看上面的代码哪里错了?
2016-09-26 16:20
弟大勿勃
Rank: 2
等 级:论坛游民
帖 子:186
专家分:59
注 册:2014-4-17
收藏
得分:0 
谢谢每位私信我的 人儿!
2016-09-27 10:20
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
收藏
得分:0 
struct student *add(struct student*head,struct student *stu1)
{
    struct student *p1,*p0,*p2;
    p1=head;
    //p0=(struct student*)malloc(LEN);
    p0=stu1;
    if(p0->num==0) return head;        //这里我加上一句
    p0->next=NULL;
    if(head==NULL)
    {
        head=p0;
      
      
    }
    else
    {
        while ((p1!=NULL)&&(p0->num>p1->num))
        {
            p2=p1;
            p1=p1->next;
           }   
       /*其实你自己写的不比别人差多少,只是看似考虑全面,实则多虑。下面的你自己仔细思考下*/                                                                                                
       // if(p0->num<=p1->num )
       // {
            if(p1==head)
            {
                head=p0;
                p0->next=p1;      
            }
            else
            {
                p2->next=p0;
                p0->next=p1;
           
            }
           
       // }
       // else
       // {
         //   p1->next=p0;
           
       // }
   
   
    }
         printf("\nDelete No: %d succeed!\n", p0->num);
            n = n+1;
    return(head);
}

int main()
{
    struct student *q,*stu,stu2,*p;
    int x;
    int a;
    stu=creat();
    q=stu;
    //p=&stu2;
    print(q);
    printf("How do you want to do:del='1',add='2'\n");
      
        scanf("%d",&a);
        if(a==1)
        {
        do
        {
      
        printf("please input the num of you want to deleat: ");
        scanf("%d",&x);
        q=del(q,x);
        if(q==NULL)
        {
           
            break;
        }
        print(q);
      
      
        }while(x!=0);
        }
        else
        {
            do
            {
                p=(struct student*)malloc(LEN);
                printf("\nplease input the num  you want to insert: ");
                scanf("%d",&(p->num));
                printf("please input the score  you want to insert: ");
                scanf("%f",&(p->score));
               
                q=add(q,p);
                if(q==NULL)
                 {
           
                    break;
                }
                print(q);
            }while(p->num!=0);
           
        }
   
   
            printf("\n\n");
        system("pause");
}

2016-09-27 13:48
弟大勿勃
Rank: 2
等 级:论坛游民
帖 子:186
专家分:59
注 册:2014-4-17
收藏
得分:0 
回复 14楼 linlulu001
感觉考虑多了反而冗余了,最后结果都错了!真是太感谢您了,其实我看完您改的代码想到
其实这个程序只是用指针来指向链表的各个结点,再插入新节点时,并没有真正的改变原结点
的其它数据,所以只要把插入的结点和相邻的结点之间的关系(及地址)写正确就醒了,其它的
考虑多了都是错!
再次感谢您,以及回复帮助我的所有好人!
2016-09-27 14:21
快速回复:链表循环插入数据遇到问题,请神帮助!
数据加载中...
 
   



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

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