| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2083 人关注过本帖
标题:链表循环插入数据遇到问题,请神帮助!
只看楼主 加入收藏
弟大勿勃
Rank: 2
等 级:论坛游民
帖 子:186
专家分:59
注 册:2014-4-17
结帖率:81.82%
收藏
已结贴  问题点数:10 回复次数:14 
链表循环插入数据遇到问题,请神帮助!
程序代码:
#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
demon90s
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:44
专家分:121
注 册:2016-1-3
收藏
得分:0 
断点调试
2016-09-23 15:21
弟大勿勃
Rank: 2
等 级:论坛游民
帖 子:186
专家分:59
注 册:2014-4-17
收藏
得分:0 
回复 3楼 demon90s
不会汇编,看得出来怎么改的请帮助!
2016-09-23 15:42
demon90s
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:44
专家分:121
注 册:2016-1-3
收藏
得分:0 
回复 4楼 弟大勿勃
学习一下断点调试,和汇编没有关系。
2016-09-23 16:06
弟大勿勃
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
xzlxzlxzl
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖北
等 级:贵宾
威 望:125
帖 子:1091
专家分:5825
注 册:2014-5-3
收藏
得分:10 
抽时间看了下你的插入代码,感觉你变量、堆内存管理等概念混淆不清,对链表节点操作更是混乱,你试下下面的代码(替换你原add函数即可),代码里有注释:
struct student *add(struct student*head,struct student *stu1)
{
    struct student *p1,*p0,*p2;
   
    p1=p2=head;
    p0=(struct student*) malloc(LEN);    //*stu1是指向的是传递过来的局部变量,作为链表节点必须申请内存
    *p0=*stu1;
    p0->next=NULL;
    while (p1!=NULL&&p0->num>p1->num)
    {
        p2=p1;
        p1=p1->next;
    }
    if(p1==p2)
    {//如果两个指针相等说明插入的序号比头指针的序号还小,需调整头指针指向
        p0->next=head;
        head=p0;
    }
    else
    {
        p2->next=p0;
        p0->next=p1;
    }
    return(head);
}
2016-09-26 10:45
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
回复 8楼 xzlxzlxzl
方法很巧,收藏了。
2016-09-26 11:47
弟大勿勃
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
快速回复:链表循环插入数据遇到问题,请神帮助!
数据加载中...
 
   



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

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