| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2084 人关注过本帖
标题:链表循环插入数据遇到问题,请神帮助!
取消只看楼主 加入收藏
弟大勿勃
Rank: 2
等 级:论坛游民
帖 子:186
专家分:59
注 册:2014-4-17
结帖率:81.82%
收藏
已结贴  问题点数:10 回复次数:9 
链表循环插入数据遇到问题,请神帮助!
程序代码:
#include<stdio.h>
#include<stdlib.h>

#define LEN sizeof(struct student)
struct student *creat();
struct student *del(struct student *head,int num);
int print(struct student *head);
//******************************************//
int n;
//******************************************//
struct student
{
    int num;
   
    float score;
    struct student *next;
};

struct student *creat()
{
    struct student *head,*p1,*p2;
    p1=p2=(struct student*) malloc(LEN);
    printf("input the num: ");
    scanf("%d",&p1->num);
    printf("input the score: ");
    scanf("%f",&p1->score);
    head=NULL;
    n=0;
    while(p1->num!=0)
    {
        n++;
        if(n==1)
        {
            head=p1;                  
        }
        else
        {
        p2->next=p1;                       
        }
        p2=p1;                    
        p1=(struct student*) malloc(LEN);
        printf("input the num:");
        scanf("%d",&p1->num);
        printf("input the score: ");
        scanf("%f",&p1->score);
    }
    p2->next=NULL;
    return (head);
}
int print(struct student *head)
{
    struct student *p;
    p=head;
    printf("There are %d records:\n",n);
    printf("\t\tnum\t\tscore\n");
    while(p!=NULL)
    {
        printf("\t\t%d\t\t%f\n",p->num,p->score);
        p=p->next;
    }
}

struct student *del(struct student *head,int y)
{
    struct student *p1,*p2;
   
    if(head==NULL)
    {
        printf("This list is null\n");
        return (NULL);   
    }
   
    p1=head;
   
    while( (p1->num != y )&& (p1->next != NULL))                  
      {
            p2 = p1;
            p1 = p1->next;
      }
      if( y == p1->num )
      {
            if( p1 == head )    
            {
                  head = p1->next;
            }
            else                
            {
                  p2->next = p1->next;
            }

            printf("\nDelete No: %d succeed!\n", y);
            n = n-1;
      }
      else
      {
            printf("%d not been found!\n", y);
      }
   
   
    return (head);
}
struct student *add(struct student*head,struct student *stu1)
{
    struct student *p1,*p0,*p2;
   
    p1=head;
   
    p0=stu1;
   
    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(p1==head)
            {
                head=p0;
                p0->next=p1;
                free(p1);
                p1=p0->next;
                   
            }
            else
            {
                p2->next=p0;
                p0->next=p1;
                free(p1);
                p1=p0->next;
           
       
            }
           
        }
        else
        {
            p1->next=p0;
            p0->next=NULL;
       
        }
        n=n+1;
    }
    return(head);
}

void main()
{
    struct student *q,*stu,stu2;
    int x;
    int a;
    stu=creat();
    q=stu;
    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
            {
           
                printf("\nplease input the num  you want to insert: ");
                scanf("%d",&stu2.num);
                printf("please input the score  you want to insert: ");
                scanf("%f",&stu2.score);
               
                q=add(q,&stu2);
                if(q==NULL)
                 {
           
                    break;
                }
                print(q);
            }while(stu2.num!=0);
           
        }
   
   
            printf("\n\n");
        system("pause");
}




以上是源代码,有循环输出、删除、添加一个结构体(学生成绩和学号)的功能,但是对于
循环插入功能:
struct student *add(struct student*head,struct student *stu1)
{
    struct student *p1,*p0,*p2;
   
    p1=head;
   
    p0=stu1;
   
    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(p1==head)
            {
                head=p0;
                p0->next=p1;
                free(p1);
                p1=p0->next;
                   
            }
            else
            {
                p2->next=p0;
                p0->next=p1;
                free(p1);
                p1=p0->next;
           
       
            }
           
        }
        else
        {
            p1->next=p0;
            p0->next=NULL;
       
        }
        n=n+1;
    }
    return(head);
}

void main()
{
    struct student *q,*stu,stu2;
    int x;
    int a;
    stu=creat();
    q=stu;
    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
            {
           
                printf("\nplease input the num  you want to insert: ");
                scanf("%d",&stu2.num);
                printf("please input the score  you want to insert: ");
                scanf("%f",&stu2.score);
               
                q=add(q,&stu2);
                if(q==NULL)
                 {
           
                    break;
                }
                print(q);
            }while(stu2.num!=0);
           
        }
   
   
            printf("\n\n");
        system("pause");
}
运行后结果会无限循环,并不是得到想要的功能。这是为什么?在线等!
            

2016-09-23 15:00
弟大勿勃
Rank: 2
等 级:论坛游民
帖 子:186
专家分:59
注 册:2014-4-17
收藏
得分:0 
各位版主游侠,大神们,请再次教导!
2016-09-23 15:13
弟大勿勃
Rank: 2
等 级:论坛游民
帖 子:186
专家分:59
注 册:2014-4-17
收藏
得分:0 
回复 3楼 demon90s
不会汇编,看得出来怎么改的请帮助!
2016-09-23 15:42
弟大勿勃
Rank: 2
等 级:论坛游民
帖 子:186
专家分:59
注 册:2014-4-17
收藏
得分:0 
别沉啊,还没有人给我满意答案呢!各位大神都放假了吗???
2016-09-26 08:41
弟大勿勃
Rank: 2
等 级:论坛游民
帖 子:186
专家分:59
注 册:2014-4-17
收藏
得分:0 
怎么没人呢?
2016-09-26 10:16
弟大勿勃
Rank: 2
等 级:论坛游民
帖 子:186
专家分:59
注 册:2014-4-17
收藏
得分:0 
回复 8楼 xzlxzlxzl
就是说对于链表来说,当要生成一个新的结点,得给它分配一个内存空间“(struct student*) malloc(LEN);”?
是这样吗?那我直接在主函数输入新的结点数据时就给struct student stu2这个结构体分配好内存空间,再改用您的add
指针函数(把p0=(struct student*) malloc(LEN);这一句删除)一样可以运行成功,如下:
如果哪里说错了,希望您指正!
程序代码:
void 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(stu2.num!=0);
           
        }
   
   
            printf("\n\n");
        system("pause");
}



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



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

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