建立一个5个节点的学生(含4个分量)链表。
时间: 2024-03-17 16:46:07 浏览: 22
好的,我可以帮你建立一个5个节点的学生链表,每个节点包含4个分量,分别是姓名、年龄、性别和成绩。
首先,让我们定义一个学生节点的结构体,包含4个分量:
```
struct student {
char name[20];
int age;
char gender;
float score;
struct student *next;
};
```
接下来,我们可以按照下面的方式建立一个包含5个节点的链表:
```
struct student *head, *p;
head = (struct student *)malloc(sizeof(struct student));
head->next = NULL;
p = (struct student *)malloc(sizeof(struct student));
strcpy(p->name, "Tom");
p->age = 18;
p->gender = 'M';
p->score = 89.5;
p->next = NULL;
head->next = p;
p = (struct student *)malloc(sizeof(struct student));
strcpy(p->name, "Lucy");
p->age = 19;
p->gender = 'F';
p->score = 92.0;
p->next = NULL;
head->next->next = p;
p = (struct student *)malloc(sizeof(struct student));
strcpy(p->name, "John");
p->age = 20;
p->gender = 'M';
p->score = 87.5;
p->next = NULL;
head->next->next->next = p;
p = (struct student *)malloc(sizeof(struct student));
strcpy(p->name, "Mary");
p->age = 18;
p->gender = 'F';
p->score = 94.0;
p->next = NULL;
head->next->next->next->next = p;
p = (struct student *)malloc(sizeof(struct student));
strcpy(p->name, "David");
p->age = 19;
p->gender = 'M';
p->score = 85.5;
p->next = NULL;
head->next->next->next->next->next = p;
```
这样,我们就成功建立了一个包含5个节点的学生链表。