用Dev-c编写程序使用链表输入输出5个学生的信息(学号、姓名、成绩),其中输入输出使用函数实现,在main函数中调用它们。
时间: 2023-06-07 14:07:22 浏览: 116
好的,让我为您解答这个问题。
首先,我们需要定义一个结构体来存储每一个学生的信息,包括学号、姓名和成绩。代码如下:
```c
struct student {
int id;
char name[20];
int score;
};
```
接下来,我们需要定义一个链表结构体,用来存储多个学生的信息。链表结构体包含一个指向下一个节点的指针,以及一个指向节点的结构体指针。
```c
struct node {
struct student data;
struct node *next;
};
```
现在,我们可以编写一个输入学生信息的函数,该函数会创建一个新节点,将学生信息添加到该节点,并将该节点添加到链表中。
```c
void add_student(struct node **head) {
struct node *new_node = (struct node *)malloc(sizeof(struct node));
printf("请输入学生的学号、姓名和成绩:\n");
scanf("%d %s %d", &(new_node->data.id), new_node->data.name, &(new_node->data.score));
new_node->next = *head;
*head = new_node;
}
```
我们还需要一个函数来输出链表中存储的每个学生的信息。
```c
void print_students(struct node *head) {
printf("学号\t姓名\t成绩\n");
while (head != NULL) {
printf("%d\t%s\t%d\n", head->data.id, head->data.name, head->data.score);
head = head->next;
}
}
```
最后,我们需要在 main 函数中调用这些函数来输入输出学生信息。
```c
int main() {
struct node *head = NULL;
int i;
for (i = 0; i < 5; i++) {
add_student(&head);
}
print_students(head);
return 0;
}
```
这个程序使用了一个指向指针的指针作为参数,使得我们可以在 add_student 函数中修改指向链表头节点的指针。然后,我们在 main 函数中调用 add_student 函数五次,输入五个学生的信息,最后打印输出所有学生的信息。
希望这个解答对您有所帮助。如果您有其他问题,请随时问我。
阅读全文
相关推荐



















