c语言链表头插法学生管理系统
时间: 2023-11-28 20:06:12 浏览: 24
好的,以下是C语言链表头插法学生管理系统的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student {
char name[20];
int age;
struct student *next;
} Student;
Student *create_student(char *name, int age) {
Student *s = (Student *)malloc(sizeof(Student));
strcpy(s->name, name);
s->age = age;
s->next = NULL;
return s;
}
void add_student(Student **head, Student *s) {
s->next = *head;
*head = s;
}
void print_students(Student *head) {
while (head != NULL) {
printf("Name: %s, Age: %d\n", head->name, head->age);
head = head->next;
}
}
int main() {
Student *head = NULL;
add_student(&head, create_student("Alice", 20));
add_student(&head, create_student("Bob", 21));
add_student(&head, create_student("Charlie", 22));
print_students(head);
return 0;
}
```