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

#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;
}

  • 26
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单链表是一种常用的数据结构,它由若干个节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。在Linux C语言,可以通过结构体来实现单链表结构体包含一个数据成员和一个指向下一个节点的指针成员。 下面是一个简单的单链表结构体定义: ```c typedef struct node { int data; struct node *next; } Node; ``` 其,data表示数据元素,next表示指向下一个节点的指针。 增加节点操作: ```c void add_node(Node **head, int data) { Node *new_node = (Node*)malloc(sizeof(Node)); new_node->data = data; new_node->next = NULL; if (*head == NULL) { *head = new_node; } else { Node *cur = *head; while (cur->next != NULL) { cur = cur->next; } cur->next = new_node; } } ``` 删除节点操作: ```c void delete_node(Node **head, int data) { Node *cur = *head; Node *prev = NULL; while (cur != NULL) { if (cur->data == data) { if (prev == NULL) { *head = cur->next; } else { prev->next = cur->next; } free(cur); return; } prev = cur; cur = cur->next; } } ``` 修改节点操作: ```c void modify_node(Node *head, int old_data, int new_data) { Node *cur = head; while (cur != NULL) { if (cur->data == old_data) { cur->data = new_data; return; } cur = cur->next; } } ``` 查询节点操作: ```c Node* find_node(Node *head, int data) { Node *cur = head; while (cur != NULL) { if (cur->data == data) { return cur; } cur = cur->next; } return NULL; } ``` 以上就是在Linux C语言实现单链表增删改查操作的示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值