#include
#include
#include
#include
#include
#define len sizeof(struct student)
#define N 3
struct student
{
char name[20];
int age;
char sex[6];
float score;
struct student *next;
};
void main()
{
int i=1;
struct student *head,*p1,*p2;
head=NULL;
p1=p2=(struct student *)malloc(len);
while(i<=N)
{
gets(p2->name);
scanf("%d %s %f",&p2->age,&p2->sex ,&p2->score );
if(i==1)
head=p1;
else
p1->next=p2;
p1=p2;
p2=(struct student *)malloc(len);
i=i+1;
getchar();
}
p1->next=NULL;
}
生成三个student。
一个链表。
先申请内存,输入学生的name…
然后存链表。
N做次数
head链表头
p1,p2临时的变量
nexe下一个student,最后next=NULL。
创建3个学生记录的链表。根据每个学生姓名,输入年龄,性别,成绩
创建3个学生记录的链表。根据每个学生姓名,输入年龄,性别,成绩
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define len sizeof(struct student)
#define N 3
struct student 定义student结构体
{
char name[20]; 名字
int age; 年纪
char sex[6]; 性别
float score; 分数
struct student *next; 指向下个学生
};
void main()
{
int i=1;
struct student *head,*p1,*p2;
head=NULL;
p1=p2=(struct student *)malloc(len); 为第一个学生分配空间
while(i<=N) N定义为3,3个学生
{
gets(p2->name);输入学生名字
scanf("%d %s %f",&p2->age,&p2->sex ,&p2->score ); 输入年龄 性别 分数
if(i==1)
head=p1; 第一个学生作为表头
else
p1->next=p2; 指向下一个学生
p1=p2;
p2=(struct student *)malloc(len); 为下个学生分配内存
i=i+1;
getchar();
}
p1->next=NULL; 最后一个学生的next指向null
}