C语言链表实现学生系统添加功能:高效录入多名学生信息
要实现学生系统的添加功能,并且使用链表结构存储多名学生的信息,可以按照以下步骤进行:
- 首先定义一个学生结构体,包含学号('id')、姓名('name')、年龄('age')等学生信息。
typedef struct Student {
int id;
char name[20];
int age;
struct Student* next;
} Student;
- 创建一个指向学生链表头结点的指针。
Student* head = NULL;
- 编写一个函数用于添加学生信息。该函数需要从用户输入获取学生的信息,并将其添加到链表中。
void addStudent() {
Student* newStudent = (Student*)malloc(sizeof(Student)); // 创建一个新的学生结点
printf("请输入学生学号:");
scanf("%d", &(newStudent->id));
printf("请输入学生姓名:");
scanf("%s", newStudent->name);
printf("请输入学生年龄:");
scanf("%d", &(newStudent->age));
newStudent->next = NULL;
if (head == NULL) { // 如果链表为空,则将新结点设为头结点
head = newStudent;
} else { // 如果链表不为空,则将新结点插入到链表末尾
Student* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newStudent;
}
}
- 调用
addStudent()函数来添加多名学生的信息。
int main() {
int num;
printf("请输入要添加的学生数量:");
scanf("%d", &num);
for (int i = 0; i < num; i++) {
addStudent();
}
return 0;
}
通过以上步骤,就可以实现在学生系统中录入多名学生的信息,并使用链表结构进行存储。
原文地址: https://www.cveoy.top/t/topic/pdvg 著作权归作者所有。请勿转载和采集!