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

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

#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条回答 默认 最新

  • qq423399099
    小灸舞 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;
    }
    
    点赞 评论

相关推荐