C语言链表的输入

Python020

C语言链表的输入,第1张

struct

student

*creat(void)

{struct

student

*head

struct

student

*p1,*p2

n=0

p1=p2=(struct

student*)malloc(len)

//这里格式化输入二个数0,0

scanf("%ld,%f",&p1->num,&p1->score)

//head赋值为null

head=null

//刚才输入的是0因此不满足条件一次循环都不走

while(p1->num!=0)

{

n=n+1

if(n==1)head=p1

else

p2->next=p1

p2=p1

p1=(struct

student*)malloc(len)

scanf("%ld,%f",&p1->num,&p1->score)

}

p2->next=null

//所以这时候head还是最初赋的值null自然打不出信息

return(head)

}

敢问楼主,为何要两个链表你的需求一个链表完全搞定了啊,第二个链表是用来做什么的?

#include<stdio.h>                                                               

#include<stdlib.h>                                                              

struct student{                                                                 

    char name[10]                                                                 

    char id[10]                                                                   

    struct student *next                                                          

}                                                                              

int main(void)                                                                  

{                                                                               

   int n                                                                       

   printf("请输入要学生个数,以回车结束!\n")                                  

   scanf("%d",&n)                                                              

   struct student *head=(struct student *)malloc(sizeof(struct student))       

   head->next=NULL                                                             

   struct student *end=head                                                    

   for(int i=0i!=ni++)                                                        

   {                                                                            

           struct student*p=(struct student *)malloc(sizeof(struct student))         

           p->next=NULL                                                              

           printf("第%d个学生信息:\n",i+1)                                          

           printf("  姓名:")                                                        

           scanf("%s",&p->name)                                                      

           printf("  学号:")                                                        

           scanf("%s",&p->id)                                                        

           end->next=p                                                               

           end=p                                                                     

   }                                                                            

   for(struct student *p=head->nextp!=NULLp=p->next)                          

   {                                                                            

           printf("姓名:%s  学号:%s \n",p->name,p->id)                               

   }                                                                            

}

截图如下: