041. 实现链表的基本操作(创建、插入、删除)

在C语言中,链表是一种常见的数据结构,通过指针将节点连接起来。以下是实现链表的基本操作(创建、插入、删除)的代码示例:

1. 定义链表节点结构

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

// 定义链表节点结构
typedef struct Node {
    int data;             // 数据域
    struct Node* next;    // 指针域,指向下一个节点
} Node;

2. 创建链表

这里提供一个简单的链表创建函数,通过动态输入数据创建链表。

// 创建链表
Node* createLinkedList() {
    Node* head = NULL;    // 头指针
    Node* tail = NULL;    // 尾指针
    int data;

    printf("请输入链表数据(输入-1结束):\n");
    while (1) {
        scanf("%d", &data);
        if (data == -1) {
            break;
        }

        Node* newNode = (Node*)malloc(sizeof(Node)); // 创建新节点
        newNode->data = data;
        newNode->next = NULL;

        if (head == NULL) { // 如果链表为空,头尾指针都指向新节点
            head = newNode;
            tail = newNode;
        } else { // 否则将新节点添加到链表尾部
            tail->next = newNode;
            tail = newNode;
        }
    }
    return head;
}

3. 插入节点

提供两种插入方式:按位置插入和按值插入。

按位置插入

// 按位置插入节点
void insertByPosition(Node** head, int position, int value) {
    if (position < 0) {
        printf("位置无效!\n");
        return;
    }

    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;

    if (position == 0) { // 插入到头部
        newNode->next = *head;
        *head = newNode;
    } else {
        Node* temp = *head;
        int count = 0;

        while (temp != NULL && count < position - 1) { // 找到插入位置的前一个节点
            temp = temp->next;
            count++;
        }

        if (temp == NULL) {
            printf("位置超出链表范围!\n");
            free(newNode);
            return;
        }

        newNode->next = temp->next; // 插入新节点
        temp->next = newNode;
    }
}

按值插入(插入到指定值的节点之后)

// 按值插入节点
void insertByValue(Node* head, int targetValue, int value) {
    Node* temp = head;
    while (temp != NULL && temp->data != targetValue) {
        temp = temp->next;
    }

    if (temp == NULL) {
        printf("未找到目标值:%d\n", targetValue);
        return;
    }

    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = temp->next;
    temp->next = newNode;
}

4. 删除节点

提供两种删除方式:按位置删除和按值删除。

按位置删除

// 按位置删除节点
void deleteByPosition(Node** head, int position) {
    if (*head == NULL || position < 0) {
        printf("链表为空或位置无效!\n");
        return;
    }

    Node* temp = *head;
    if (position == 0) { // 删除头部节点
        *head = temp->next;
        free(temp);
        return;
    }

    int count = 0;
    while (temp != NULL && count < position - 1) { // 找到删除位置的前一个节点
        temp = temp->next;
        count++;
    }

    if (temp == NULL || temp->next == NULL) {
        printf("位置超出链表范围!\n");
        return;
    }

    Node* nodeToDelete = temp->next;
    temp->next = nodeToDelete->next;
    free(nodeToDelete);
}

按值删除

// 按值删除节点
void deleteByValue(Node** head, int value) {
    if (*head == NULL) {
        printf("链表为空!\n");
        return;
    }

    Node* temp = *head;
    Node* prev = NULL;

    while (temp != NULL && temp->data != value) {
        prev = temp;
        temp = temp->next;
    }

    if (temp == NULL) {
        printf("未找到目标值:%d\n", value);
        return;
    }

    if (prev == NULL) { // 删除头部节点
        *head = temp->next;
    } else {
        prev->next = temp->next;
    }

    free(temp);
}

5. 打印链表

// 打印链表
void printLinkedList(Node* head) {
    Node* temp = head;
    printf("链表内容:");
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

6. 主函数

int main() {
    Node* head = createLinkedList(); // 创建链表
    printLinkedList(head);           // 打印链表

    // 插入操作
    printf("插入操作:\n");
    insertByPosition(&head, 2, 99); // 在位置2插入99
    printLinkedList(head);

    // 删除操作
    printf("删除操作:\n");
    deleteByValue(&head, 99);       // 删除值为99的节点
    printLinkedList(head);

    return 0;
}

视频讲解

BiliBili: 视睿网络-哔哩哔哩视频 (bilibili.com)