C语言单链表创建 看不懂 求注释
C语言单链表创建看不懂求注释链表的创建:#include“stdlib.h”#include“stdio.h”#defineNULL0#defineLENsizeof(s...
C语言单链表创建 看不懂 求注释链表的创建:
#include “stdlib.h”
#include “stdio.h”
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf(“%ld,%f”,&p1->num,&p1->score);
head=NULL;
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;
return(head);
}
void main()
{
creat();
} 展开
#include “stdlib.h”
#include “stdio.h”
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf(“%ld,%f”,&p1->num,&p1->score);
head=NULL;
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;
return(head);
}
void main()
{
creat();
} 展开
1个回答
展开全部
#include “stdlib.h”
#include “stdio.h”
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head; // 列表头,要返回的
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);//p1 和 p2 都指指向同一块内存
scanf(“%ld,%f”,&p1->num,&p1->score);//键盘输入,赋值给p1->num 和 p1->score
head=NULL;
while(p1->num != 0)//如果p1->num 输入的不是0,循环
{
n=n+1;
if(n == 1)//第一次 n == 1 ,以后都不是 ,第一次 p1 赋值给 head
head = p1;
else //以后都把新创建的结构体,挂在列表的最后。
p2->next = p1;
p2 = p1;//p2 记录下p1 ,也就是列表最后一个节点的地址
p1 = (struct student *)malloc(LEN);//新malloc一个p1
scanf(“%ld,%f”,&p1->num,&p1->score);//再次输入,循环,知道p1->num 为 0
}
p2->next = NULL;//列表最后赋null
return(head);//返回head
}
void main()
{
creat();
}
#include “stdio.h”
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head; // 列表头,要返回的
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);//p1 和 p2 都指指向同一块内存
scanf(“%ld,%f”,&p1->num,&p1->score);//键盘输入,赋值给p1->num 和 p1->score
head=NULL;
while(p1->num != 0)//如果p1->num 输入的不是0,循环
{
n=n+1;
if(n == 1)//第一次 n == 1 ,以后都不是 ,第一次 p1 赋值给 head
head = p1;
else //以后都把新创建的结构体,挂在列表的最后。
p2->next = p1;
p2 = p1;//p2 记录下p1 ,也就是列表最后一个节点的地址
p1 = (struct student *)malloc(LEN);//新malloc一个p1
scanf(“%ld,%f”,&p1->num,&p1->score);//再次输入,循环,知道p1->num 为 0
}
p2->next = NULL;//列表最后赋null
return(head);//返回head
}
void main()
{
creat();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询