无头链表增删改查和有头链表增删改查

无头链表的增删改查操作:

无头链表的增删改查操作:

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

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

// 创建新节点
Node* create_node(int data) {
    Node *new_node = (Node*)malloc(sizeof(Node));
    if (new_node == NULL) {
        printf("内存分配失败\n");
        exit(EXIT_FAILURE);
    }
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}

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

// 插入节点到链表头部
void insert_at_head(Node **head, int data) {
    Node *new_node = create_node(data);
    new_node->next = *head;
    *head = new_node;
    printf("插入 %d 到链表头部\n", data);
}

// 尾插法插入节点
void insert_at_tail(Node **head, int data) {
    Node *new_node = create_node(data);
    if (*head == NULL) {
        *head = new_node;
    } else {
        Node *current = *head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = new_node;
    }
    printf("插入 %d 到链表尾部\n", data);
}

// 删除链表中第一个匹配的节点
void delete_node(Node **head, int data) {
    Node *current = *head;
    Node *prev = NULL;

    // 如果头节点就是要删除的节点
    if (current != NULL && current->data == data) {
        *head = current->next;
        free(current);
        printf("删除节点 %d\n", data);
        return;
    }

    // 搜索要删除的节点,保持前一个节点的指针
    while (current != NULL && current->data != data) {
        prev = current;
        current = current->next;
    }

    // 如果没有找到节点
    if (current == NULL) {
        printf("未找到节点 %d\n", data);
        return;
    }

    // 如果要删除的节点是最后一个节点
    if (current->next == NULL) {
        prev->next = NULL;
    } else {
        prev->next = current->next;
    }
    free(current);
    printf("删除节点 %d\n", data);
}

// 删除链表中的所有节点
void delete_all_nodes(Node **head) {
    Node *current = *head;
    Node *next;
    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }
    *head = NULL;
    printf("删除所有节点\n");
}

// 查找链表中的节点
Node* search(Node *head, int data) {
    Node *current = head;
    while (current != NULL) {
        if (current->data == data) {
            printf("找到节点 %d\n", data);
            return current;
        }
        current = current->next;
    }
    printf("未找到节点 %d\n", data);
    return NULL;
}

// 修改链表中的节点数据
void update_node(Node *head, int old_data, int new_data) {
    Node *node_to_update = search(head, old_data);
    if (node_to_update != NULL) {
        node_to_update->data = new_data;
        printf("节点 %d 更新为 %d\n", old_data, new_data);
    }
}

int main() {
    Node *head = NULL;

    // 插入节点
    insert_at_head(&head, 3);
    insert_at_head(&head, 2);
    insert_at_head(&head, 1);

    // 尾插法插入节点
    insert_at_tail(&head, 4);
    insert_at_tail(&head, 5);
    insert_at_tail(&head, 6);

    print_list(head);

    // 查找节点
    search(head, 2);

    // 修改节点
    update_node(head, 2, 7);
    print_list(head);

    // 删除节点
    delete_node(&head, 7);
    print_list(head);

    // 再次删除节点
    delete_node(&head, 1);
    print_list(head);

    // 删除不存在的节点
    delete_node(&head, 9);
    print_list(head);

    // 删除所有节点
    delete_all_nodes(&head);
    print_list(head);

    return 0;
}

有头链表的增删改查:

以下是带头节点的链表的增删改查操作的代码:

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

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

// 创建头节点
Node* create_head() {
    Node *head = (Node*)malloc(sizeof(Node));
    if (head == NULL) {
        printf("内存分配失败\n");
        exit(EXIT_FAILURE);
    }
    head->next = NULL;
    return head;
}

// 创建新节点
Node* create_node(int data) {
    Node *new_node = (Node*)malloc(sizeof(Node));
    if (new_node == NULL) {
        printf("内存分配失败\n");
        exit(EXIT_FAILURE);
    }
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}

// 打印链表
void print_list(Node *head) {
    Node *current = head->next; // 跳过头节点
    printf("链表: ");
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

// 插入节点到链表头部
void insert_at_head(Node *head, int data) {
    Node *new_node = create_node(data);
    new_node->next = head->next;
    head->next = new_node;
    printf("插入 %d 到链表头部\n", data);
}

// 尾插法插入节点
void insert_at_tail(Node *head, int data) {
    Node *new_node = create_node(data);
    Node *current = head;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = new_node;
    printf("插入 %d 到链表尾部\n", data);
}

// 删除链表中第一个匹配的节点
void delete_node(Node *head, int data) {
    Node *current = head->next; // 跳过头节点
    Node *prev = head;

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

    if (current == NULL) {
        printf("未找到节点 %d\n", data);
        return;
    }

    prev->next = current->next;
    free(current);
    printf("删除节点 %d\n", data);
}

// 删除链表中的所有节点
void delete_all_nodes(Node *head) {
    Node *current = head->next; // 跳过头节点
    Node *next;
    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }
    head->next = NULL;
    printf("删除所有节点\n");
}

// 查找链表中的节点
Node* search(Node *head, int data) {
    Node *current = head->next; // 跳过头节点
    while (current != NULL) {
        if (current->data == data) {
            printf("找到节点 %d\n", data);
            return current;
        }
        current = current->next;
    }
    printf("未找到节点 %d\n", data);
    return NULL;
}

// 修改链表中的节点数据
void update_node(Node *head, int old_data, int new_data) {
    Node *node_to_update = search(head, old_data);
    if (node_to_update != NULL) {
        node_to_update->data = new_data;
        printf("节点 %d 更新为 %d\n", old_data, new_data);
    }
}

int main() {
    Node *head = create_head();

    // 插入节点
    insert_at_head(head, 3);
    insert_at_head(head, 2);
    insert_at_head(head, 1);

    // 尾插法插入节点
    insert_at_tail(head, 4);
    insert_at_tail(head, 5);
    insert_at_tail(head, 6);

    print_list(head);

    // 查找节点
    search(head, 2);

    // 修改节点
    update_node(head, 2, 7);
    print_list(head);

    // 删除节点
    delete_node(head, 7);
    print_list(head);

    // 再次删除节点
    delete_node(head, 1);
    print_list(head);

    // 删除不存在的节点
    delete_node(head, 9);
    print_list(head);

    // 删除所有节点
    delete_all_nodes(head);
    print_list(head);

    // 释放头节点
    free(head);

    return 0;
}

这个版本的代码具有头节点,头节点用于简化对链表的操作,并且在遍历和操作链表时,不需要额外的空指针检查。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值