学生信息管理系统(双向链表)(C语言)
时间: 2023-05-31 16:03:59 浏览: 117
很抱歉,我是一名语言模型AI,无法提供代码,但我可以给出一个基本的双向链表框架,供您参考:
```c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//定义学生信息结构体
typedef struct Student{
char name[20];
int age;
char gender[10];
char id[20];
struct Student *prev; //指向前一个节点的指针
struct Student *next; //指向后一个节点的指针
}Student;
//定义链表头结构体
typedef struct List{
int count; //链表中节点数量
Student *head; //指向链表头节点的指针
Student *tail; //指向链表尾节点的指针
}List;
//初始化链表
void init(List *list){
list->count = 0;
list->head = NULL;
list->tail = NULL;
}
//创建节点
Student *create_student(char *name, int age, char *gender, char *id){
Student *new_student = (Student*)malloc(sizeof(Student));
strcpy(new_student->name, name);
new_student->age = age;
strcpy(new_student->gender, gender);
strcpy(new_student->id, id);
new_student->prev = NULL;
new_student->next = NULL;
return new_student;
}
//插入节点
void insert(List *list, Student *student){
if(list->count == 0){
list->head = student;
list->tail = student;
}
else{
student->prev = list->tail;
list->tail->next = student;
list->tail = student;
}
list->count++;
}
//删除节点
void delete(List *list, char *id){
Student *current = list->head;
while(current != NULL){
if(strcmp(current->id, id) == 0){
if(current == list->head){
list->head = current->next;
list->head->prev = NULL;
}
else if(current == list->tail){
list->tail = current->prev;
list->tail->next = NULL;
}
else{
current->prev->next = current->next;
current->next->prev = current->prev;
}
free(current);
list->count--;
return;
}
current = current->next;
}
printf("该学生不存在!\n");
}
//查找节点
Student *search(List *list, char *id){
Student *current = list->head;
while(current != NULL){
if(strcmp(current->id, id) == 0){
return current;
}
current = current->next;
}
return NULL;
}
//打印链表
void print(List *list){
printf("链表中共有%d个节点:\n", list->count);
Student *current = list->head;
while(current != NULL){
printf("姓名:%s\t年龄:%d\t性别:%s\t学号:%s\n", current->name, current->age, current->gender, current->id);
current = current->next;
}
}
int main(){
List list;
init(&list);
insert(&list, create_student("张三", 18, "男", "001"));
insert(&list, create_student("李四", 19, "女", "002"));
insert(&list, create_student("王五", 20, "男", "003"));
print(&list);
delete(&list, "002");
print(&list);
Student *student = search(&list, "001");
if(student != NULL){
printf("姓名:%s\t年龄:%d\t性别:%s\t学号:%s\n", student->name, student->age, student->gender, student->id);
}
return 0;
}
```