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

简介: 实验:数据结构(结构体在单链表中的增删改查)
#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;
}
相关文章
|
2月前
|
存储 算法 编译器
数据结构实验之矩阵的运算器(二维数组)
本实验旨在通过团队合作,掌握数组和矩阵相关运算的代码实现,包括矩阵的加减、数乘、转置、乘法、n次方及行列式的计算。实验过程中,成员们需分工协作,解决编程难题,最终实现一个功能完备的矩阵计算器。通过本实验,不仅锻炼了编程能力,还加深了对数学概念的理解,同时培养了团队合作精神。
75 4
|
2月前
数据结构实验之串模式匹配问题
本实验旨在掌握串模式匹配技术,通过创建文本文件、实现单词计数与定位功能,最终构建一个包含文件建立、单词统计与定位、程序退出等选项的主菜单,以增强对字符串处理的理解与应用能力。
68 4
|
2月前
|
算法
数据结构实验之最长公共子序列
本实验旨在通过编程实践帮助学生理解串的基本概念及求解最长公共子序列的算法。实验内容包括使用动态规划方法设计并实现算法,以找出给定两序列的最大公共子序列。示例代码展示了如何通过构建状态矩阵和回溯路径来找到解决方案。实验总结指出,`memset()`函数用于内存初始化,且对于特定输入,程序能正确输出最长公共子序列之一。
64 4
|
2月前
|
算法
数据结构实验之操作系统打印机管理器问题
本实验旨在通过实现操作系统中的打印机管理器问题,掌握队列的基本操作如入队、出队等,利用队列的先进先出特性解决先申请先打印的问题。实验包括队列的初始化、入队、出队、打印队列内容等功能,并通过菜单式界面进行交互。实验结果显示基本功能可正常执行,但在连续操作时存在执行失败的情况,需进一步优化。
55 4
|
2月前
|
存储 算法 Perl
数据结构实验之链表
本实验旨在掌握线性表中元素的前驱、后续概念及链表的建立、插入、删除等算法,并分析时间复杂度,理解链表特点。实验内容包括循环链表应用(约瑟夫回环问题)、删除单链表中重复节点及双向循环链表的设计与实现。通过编程实践,加深对链表数据结构的理解和应用能力。
67 4
|
5天前
|
机器学习/深度学习 存储 C++
【C++数据结构——线性表】单链表的基本运算(头歌实践教学平台习题)【合集】
本内容介绍了单链表的基本运算任务,涵盖线性表的基本概念、初始化、销毁、判定是否为空表、求长度、输出、求元素值、按元素值查找、插入和删除数据元素等操作。通过C++代码示例详细解释了顺序表和链表的实现方法,并提供了测试说明、通 - **任务描述**:实现单链表的基本运算。 - **相关知识**:包括线性表的概念、初始化、销毁、判断空表、求长度、输出、求元素值、查找、插入和删除等操作。 - **测试说明**:平台会对你编写的代码进行测试,提供测试输入和预期输出。 - **通关代码**:给出了完整的C++代码实现。 - **测试结果**:展示了测试通过后的预期输出结果。 开始你的任务吧,祝你成功!
24 5
|
19天前
|
数据库
数据结构中二叉树,哈希表,顺序表,链表的比较补充
二叉搜索树,哈希表,顺序表,链表的特点的比较
数据结构中二叉树,哈希表,顺序表,链表的比较补充
|
2月前
|
存储 缓存 算法
在C语言中,数据结构是构建高效程序的基石。本文探讨了数组、链表、栈、队列、树和图等常见数据结构的特点、应用及实现方式
在C语言中,数据结构是构建高效程序的基石。本文探讨了数组、链表、栈、队列、树和图等常见数据结构的特点、应用及实现方式,强调了合理选择数据结构的重要性,并通过案例分析展示了其在实际项目中的应用,旨在帮助读者提升编程能力。
81 5
|
2月前
|
机器学习/深度学习 存储 算法
数据结构实验之二叉树实验基础
本实验旨在掌握二叉树的基本特性和遍历算法,包括先序、中序、后序的递归与非递归遍历方法。通过编程实践,加深对二叉树结构的理解,学习如何计算二叉树的深度、叶子节点数等属性。实验内容涉及创建二叉树、实现各种遍历算法及求解特定节点数量。
108 4
|
2月前
|
存储 人工智能 算法
数据结构实验之C 语言的函数数组指针结构体知识
本实验旨在复习C语言中的函数、数组、指针、结构体与共用体等核心概念,并通过具体编程任务加深理解。任务包括输出100以内所有素数、逆序排列一维数组、查找二维数组中的鞍点、利用指针输出二维数组元素,以及使用结构体和共用体处理教师与学生信息。每个任务不仅强化了基本语法的应用,还涉及到了算法逻辑的设计与优化。实验结果显示,学生能够有效掌握并运用这些知识完成指定任务。
66 4