EncoreLaw 2016-05-21 10:02 采纳率: 46.2%
浏览 1293
已采纳

链表的插入程序,我这个哪里有错误啊 - -

#include
#include
#define LEN sizeof(struct student)
struct student
{
int num;
int score;
struct student *next;
};
int n=0;
struct student *creat()
{

struct student *head=NULL,*p1,*p2;
p1=p2=(struct student*)malloc(LEN);
printf("Please enter the numbers and scores of the students(enter 0 maens finishing input):\n");
scanf("%d %d",&p1->num,&p1->score);
while(p1->num!=0)
{ 
    n++;
    if(n==1)head=p1;
    else p2->next=p1;
    p2=p1;
    p1=(struct student*)malloc(LEN);
    scanf("%d %d",&p1->num,&p1->score);

}
p2->next=NULL;
free(p1);
return head;

}
struct student insert(int num,int score,struct student *p)
{

struct student *a,*t=NULL,*h=p;
a=(struct student
)malloc(LEN);
a->num=num,a->score=score;

while(p->next!=NULL)
{
    if(p->num>a->num)
    {
        if(t=NULL){a->next=p,h=a;}
        else{t->next=a,a->next=p;}
        break;
    }
    t=p;
    p=p->next;
}
if(p->next=NULL){p->next=a;a->next=NULL;}
return h;

}

int main()
{
struct student *pt;
int num,score;
pt=creat();
printf("please enter a number and a score to insert into the database:\n");
scanf("%d %d",&num,&score);
pt=insert(num,score,pt);
while(pt!=NULL)
{
printf("num:%d score:%d\n",pt->num,pt->score);
pt=pt->next;
}

return 0;

}

  • 写回答

1条回答 默认 最新

  • 小灸舞 2016-05-21 12:44
    关注

    有两个地方的==写成了=
    有两处的p->next应该改成p

     #include<stdio.h>
    #include<stdlib.h>
    #define LEN sizeof(struct student)
    struct student
    {
        int num;
        int score;
        struct student *next;
    };
    int n=0;
    struct student *creat()
    {
    
        struct student *head=NULL,*p1,*p2;
        p1=p2=(struct student*)malloc(LEN);
        printf("Please enter the numbers and scores of the students(enter 0 maens finishing input):\n");
        scanf("%d %d",&p1->num,&p1->score);
        while(p1->num!=0)
        { 
            n++;
            if(n==1)head=p1;
            else p2->next=p1;
            p2=p1;
            p1=(struct student*)malloc(LEN);
            scanf("%d %d",&p1->num,&p1->score);
    
        }
        p2->next=NULL;
        free(p1);
        return head;
    }
    struct student *insert(int num,int score,struct student *p)
    {   
        struct student *a,*t=NULL,*h=p;
        a=(struct student*)malloc(LEN);
        a->num=num,a->score=score;
    
        while(p!=NULL)
        {
            if(p->num>a->num)
            {
                if(t==NULL){a->next=p,h=a;}
                else{t->next=a,a->next=p;}
                break;
            }
            t=p;
            p=p->next;
        }
        if(p==NULL)
        {
            p->next=a;
            a->next=NULL;
        }
        return h;
    }
    
    int main()
    {
        struct student *pt;
        int num,score;
        pt=creat();
        printf("please enter a number and a score to insert into the database:\n");
        scanf("%d %d",&num,&score);
        pt=insert(num,score,pt);
        while(pt!=NULL)
        {
            printf("num:%d score:%d\n",pt->num,pt->score);
            pt=pt->next;
        }
    
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序