实验:数据结构(结构体在单链表中的增删改查)

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// 学生信息结构体

struct Student {

char name[50];

char gender[10];

int student_number;

char hobbies[100];

struct Student* next;

};

// 初始化一个空表

struct Student* initialize() {

return NULL;

}

// 后插法插入学生信息

struct Student* insert(struct Student* head, char name[], char gender[], int student_number, char hobbies[]) {

struct Student* new_student = (struct Student*)malloc(sizeof(struct Student));

strcpy(new_student->name, name);

strcpy(new_student->gender, gender);

new_student->student_number = student_number;

strcpy(new_student->hobbies, hobbies);

new_student->next = NULL;

if (head == NULL) {

return new_student;

}

struct Student* current = head;

while (current->next != NULL) {

current = current->next;

}

current->next = new_student;

return head;

}

// 根据学号对链表节点进行排序

struct Student* sort_by_student_number(struct Student* head) {

// 使用冒泡排序

int swapped;

struct Student* ptr1;

struct Student* lptr = NULL;

if (head == NULL) {

return NULL;

}

do {

swapped = 0;

ptr1 = head;

while (ptr1->next != lptr) {

if (ptr1->student_number > ptr1->next->student_number) {

// 交换节点

struct Student* temp = ptr1;

ptr1 = ptr1->next;

ptr1->next = temp;

swapped = 1;

}

ptr1 = ptr1->next;

}

lptr = ptr1;

} while (swapped);

return head;

}

// 根据姓名或学号顺序插入新创建的学生信息

struct Student* insert_sorted(struct Student* head, char name[], char gender[], int student_number, char hobbies[]) {

struct Student* new_student = (struct Student*)malloc(sizeof(struct Student));

strcpy(new_student->name, name);

strcpy(new_student->gender, gender);

new_student->student_number = student_number;

strcpy(new_student->hobbies, hobbies);

new_student->next = NULL;

if (head == NULL || student_number < head->student_number) {

new_student->next = head;

return new_student;

}

struct Student* current = head;

while (current->next != NULL && current->next->student_number < student_number) {

current = current->next;

}

new_student->next = current->next;

current->next = new_student;

return head;

}

// 根据学号删除学生信息

struct Student* delete_by_student_number(struct Student* head, int student_number) {

if (head == NULL) {

return NULL;

}

if (head->student_number == student_number) {

struct Student* temp = head;

head = head->next;

free(temp);

return head;

}

struct Student* current = head;

while (current->next != NULL && current->next->student_number != student_number) {

current = current->next;

}

if (current->next != NULL) {

struct Student* temp = current->next;

current->next = current->next->next;

free(temp);

}

return head;

}

// 打印所有学生信息

void print_students(struct Student* head) {

struct Student* current = head;

while (current != NULL) {

printf("姓名:%s,性别:%s,学号:%d,兴趣爱好:%s\n", current->name, current->gender, current->student_number, current->hobbies);

current = current->next;

}

}

int main() {

struct Student* head = initialize();

// 示例:插入学生信息

head = insert(head, "张三", "男", 1001, "篮球");

head = insert(head, "李四", "女", 1002, "音乐");

// ... 插入更多学生信息

// 排序

head = sort_by_student_number(head);

// 插入新学生信息

head = insert_sorted(head, "王五", "男", 1003, "游泳");

// 打印所有学生信息

print_students(head);

// 其他操作...

return 0;

}

相关推荐
Yunni_root5 天前
Ubuntu终端跑colmap实验记录——生成sparse和poses_bounds.npy
经验分享·ubuntu·计算机视觉·nerf·colmap·实验·bad-nerf
Tassel_YUE2 个月前
vSAN01:vSAN简介、安装、磁盘组、内部架构与调用关系
架构·vmware·虚拟化·vsan·实验
Tassel_YUE3 个月前
存储实验:基于华为存储实现存储双活(HyperMetro特性)
运维·服务器·数据库·经验分享·华为·存储·实验
Tassel_YUE3 个月前
存储实验:华为异构存储在线接管与在线数据迁移(Smart Virtualization & Smart Migration 特性)
华为·存储·技术分享·实验·在线迁移
宇寒风暖3 个月前
高级路由实验
网络·学习·计算机网络·实验
Tassel_YUE4 个月前
存储实验:Linux挂载iscsi硬盘与华为OceanStor创建LUN全流程
linux·运维·服务器·存储·实验
赵药师5 个月前
Matlab信号与系统实验-实验三 连续时间信号的频域分析
开发语言·matlab·信号与系统·实验·函数的绘制matlab
HackerKevn5 个月前
【上海大学计算机组成原理实验报告】七、程序转移机制
计算机组成原理·实验报告·实验
宇寒风暖6 个月前
计算机网络(网络原理及应用)之路由器的基本配置(详细~)
学习·计算机网络·实验
꧁༺❀氯ྀൢ躅ྀൢ❀༻꧂6 个月前
数据结构 实验 1
c语言·数据结构·c++·线性表·实验