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

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// 学生信息结构体

struct Student {

char name50;

char gender10;

int student_number;

char hobbies100;

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;

}

相关推荐
xbzb9 天前
Swift 对象存储建环实验 + Ceilometer 计量:两个收尾服务
swift·ring·实验·ceilometer·象存储·建环
蛋白界小百灵16 天前
酵母单杂交与EMSA、ChIP技术对比:转录因子-DNA互作研究的互补策略
经验分享·生物技术·实验·卡梅德生物·科研实验
蛋白界小百灵18 天前
犬源scFv噬菌体文库构建要点:轻链基因、5‘RACE引物设计与单B细胞配对策略
生物技术·实验·卡梅德生物·科研实验·实验室日常分享
蛋白界小百灵21 天前
纳米抗体遇上“生物导弹“:小体积药物偶联物如何精准打击肿瘤
生物技术·实验·卡梅德生物·科研实验·实验室日常分享
蛋白界小百灵1 个月前
shRNA慢病毒载体设计实践:启动子、发夹结构与敲低效率的工程化权衡
经验分享·生物技术·实验·卡梅德生物·科研实验·载体设计
蛋白界小百灵1 个月前
AAV载体包装与纯化技术全解析:从三质粒系统到衣壳定向进化
经验分享·生物技术·实验·卡梅德生物·科研实验
蛋白界小百灵1 个月前
Beacon单细胞光流控筛选技术:单细胞水平的抗体筛选与细胞功能分析
经验分享·生物技术·实验·卡梅德生物·科研实验
蛋白界小百灵2 个月前
骆驼乳 IgG2/IgG3 分离与鉴定:脱脂/去酪/亲和层析 + SDS-PAGE/ELISA/SPR 全流程技术拆解
经验分享·生物技术·实验·卡梅德生物·科研实验
蛋白界小百灵2 个月前
单克隆抗体噬菌体展示文库构建与淘选:技术要点与实验设计
生物技术·实验·卡梅德生物·科研实验
蛋白界小百灵3 个月前
卡梅德生物:免疫共沉淀(Co-IP)技术原理及实验流程优化策略
生物技术·实验·卡梅德生物