c语言简单链表输出学生学号,姓名,性别,年龄,成绩
时间: 2024-06-14 17:06:04 浏览: 15
以下是一个简单的C语言链表示例,用于输出学生的学号、姓名、性别、年龄和成绩:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义学生结构体
struct Student {
int id;
char name[50];
char gender;
int age;
float score;
struct Student* next;
};
// 创建链表节点
struct Student* createNode(int id, char name[], char gender, int age, float score) {
struct Student* newNode = (struct Student*)malloc(sizeof(struct Student));
newNode->id = id;
strcpy(newNode->name, name);
newNode->gender = gender;
newNode->age = age;
newNode->score = score;
newNode->next = NULL;
return newNode;
}
// 插入节点到链表尾部
struct Student* insertNode(struct Student* head, int id, char name[], char gender, int age, float score) {
struct Student* newNode = createNode(id, name, gender, age, score);
if (head == NULL) {
head = newNode;
} else {
struct Student* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
return head;
}
// 输出链表中的学生信息
void printStudents(struct Student* head) {
struct Student* temp = head;
while (temp != NULL) {
printf("学号:%d\n", temp->id);
printf("姓名:%s\n", temp->name);
printf("性别:%c\n", temp->gender);
printf("年龄:%d\n", temp->age);
printf("成绩:%.2f\n", temp->score);
printf("\n");
temp = temp->next;
}
}
int main() {
struct Student* head = NULL;
int id;
char name[50];
char gender;
int age;
float score;
// 插入学生信息
printf("请输入学生信息(输入-1结束):\n");
while (1) {
printf("学号:");
scanf("%d", &id);
if (id == -1) {
break;
}
printf("姓名:");
scanf("%s", name);
printf("性别:");
scanf(" %c", &gender);
printf("年龄:");
scanf("%d", &age);
printf("成绩:");
scanf("%f", &score);
head = insertNode(head, id, name, gender, age, score);
}
// 输出学生信息
printf("\n学生信息如下:\n");
printStudents(head);
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)