C语言有序链表操作代码示例:插入、删除、查找

本代码示例演示了如何使用C语言实现有序链表的操作,包括插入元素、删除元素、查找元素和打印链表。代码包含详细的注释,方便理解和学习。

#include <stdio.h>
#include <stdlib.h>

// 定义链表节点结构
struct Node {
    int data;
    struct Node* next;
};

// 初始化链表
void initialize(struct Node** head) {
    *head = NULL;
}

// 向有序链表插入元素
void insert(struct Node** head, int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = NULL;

    // 空链表或插入元素值小于头节点
    if (*head == NULL || value < (*head)->data) {
        newNode->next = *head;
        *head = newNode;
    }
    else {
        struct Node* current = *head;
        // 找到插入位置
        while (current->next != NULL && value >= current->next->data) {
            current = current->next;
        }
        newNode->next = current->next;
        current->next = newNode;
    }
}

// 从有序链表删除元素
void delete(struct Node** head, int value) {
    if (*head == NULL) {
        printf('链表为空,无法删除元素。\n');
        return;
    }

    struct Node* current = *head;
    struct Node* prev = NULL;

    // 头节点即为要删除的元素
    if (current != NULL && current->data == value) {
        *head = current->next;
        free(current);
        return;
    }

    // 查找要删除的元素
    while (current != NULL && current->data != value) {
        prev = current;
        current = current->next;
    }

    if (current == NULL) {
        printf('链表中不存在要删除的元素。\n');
        return;
    }

    prev->next = current->next;
    free(current);
}

// 在有序链表中查找元素
void search(struct Node* head, int value) {
    struct Node* current = head;
    int position = 1;
    while (current != NULL && current->data <= value) {
        if (current->data == value) {
            printf('元素 %d 在链表中的位置为 %d。\n', value, position);
            return;
        }
        current = current->next;
        position++;
    }
    printf('元素 %d 在链表中不存在。\n', value);
}

// 打印有序链表中的元素
void display(struct Node* head) {
    struct Node* current = head;
    printf('链表中的元素为: ');
    while (current != NULL) {
        printf('%d ', current->data);
        current = current->next;
    }
    printf('\n');
}

// 主函数
int main() {
    struct Node* head;
    initialize(&head);  // 初始化链表

    insert(&head, 5);   // 插入元素
    insert(&head, 2);
    insert(&head, 8);
    insert(&head, 1);

    display(head);      // 打印链表中的元素

    delete(&head, 2);   // 删除元素
    delete(&head, 10);  // 不存在的元素

    display(head);      // 打印链表中的元素

    search(head, 8);    // 查找元素
    search(head, 3);    // 不存在的元素

    return 0;
}

代码说明:

  • struct Node: 定义了链表节点的结构,包含数据域data和指向下一个节点的指针next
  • initialize(struct Node head): 初始化链表,将头节点设置为NULL。
  • insert(struct Node head, int value): 向有序链表插入元素,保证链表始终有序。
  • delete(struct Node head, int value): 从有序链表中删除元素。
  • search(struct Node head, int value):* 在有序链表中查找元素,并返回其位置。
  • display(struct Node head):* 打印有序链表中的所有元素。

运行结果:

链表中的元素为: 1 2 5 8 
链表中的元素为: 1 5 8 
元素 8 在链表中的位置为 3。
元素 3 在链表中不存在。

使用说明:

  1. 将代码保存为.c文件,例如linked_list.c
  2. 使用C语言编译器编译代码,例如gcc linked_list.c -o linked_list
  3. 运行编译后的可执行文件,例如./linked_list

希望本代码示例能够帮助您理解和学习C语言有序链表的操作。如果您有任何问题,请随时提问。

C语言有序链表操作代码示例:插入、删除、查找

原文地址: https://www.cveoy.top/t/topic/lJF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录