几个链表常见操作的经典案例---------C语言,单向链表

1. 创建链表

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

// 定义链表节点结构体
struct ListNode {
    int val;
    struct ListNode *next;
};

// 函数用于创建链表,从标准输入读取数据,以 -1 结束输入
struct ListNode *createList() {
    struct ListNode *head = NULL, *tail = NULL;
    int num;
    scanf("%d", &num);
    while (num!= -1) {
        struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode));
        newNode->val = num;
        newNode->next = NULL;
        if (head == NULL) {
            head = newNode;
            tail = newNode;
        } else {
            tail->next = newNode;
            tail = newNode;
        }
        scanf("%d", &num);
    }
    return head;
}

2. 遍历链表

// 函数用于遍历链表并输出每个节点的值
void printList(struct ListNode *head) {
    struct ListNode *cur = head;
    while (cur!= NULL) {
        printf("%d ", cur->val);
        cur = cur->next;
    }
    printf("\n");
}

3. 查找节点(查找值为特定值的节点)

// 函数用于在链表中查找值为 target 的节点,返回该节点指针,如果没找到返回 NULL
struct ListNode *findNode(struct ListNode *head, int target) {
    struct ListNode *cur = head;
    while (cur!= NULL) {
        if (cur->val == target) {
            return cur;
        }
        cur = cur->next;
    }
    return NULL;
}

4. 插入节点(在指定节点后插入新节点)

// 函数用于在链表中指定节点之后插入一个新节点
void insertNodeAfter(struct ListNode *prevNode, int newVal) {
    if (prevNode == NULL) {
        return;
    }
    struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode));
    newNode->val = newVal;
    newNode->next = prevNode->next;
    prevNode->next = newNode;
}

5. 删除节点(删除值为特定值的节点)

// 函数用于删除链表中值为 target 的节点
struct ListNode *deleteNode(struct ListNode *head, int target) {
    struct ListNode *prev = NULL;
    struct ListNode *cur = head;
    while (cur!= NULL && cur->val!= target) {
        prev = cur;
        cur = cur->next;
    }
    if (cur == NULL) {
        return head;
    }
    if (prev == NULL) {
        head = cur->next;
    } else {
        prev->next = cur->next;
    }
    free(cur);
    return head;
}

主函数(用来测试函数):

int main() {
    // 创建链表
    struct ListNode *head = createList();

    // 遍历链表
    printf("原始链表: ");
    printList(head);

    // 查找节点
    struct ListNode *foundNode = findNode(head, 3);
    if (foundNode!= NULL) {
        printf("找到值为 3 的节点\n");
    } else {
        printf("未找到值为 3 的节点\n");
    }

    // 在找到的节点(假设值为 3 的节点存在)后插入新节点
    if (foundNode!= NULL) {
        insertNodeAfter(foundNode, 5);
        printf("插入节点后链表: ");
        printList(head);
    }

    // 删除节点(删除值为 3 的节点)
    head = deleteNode(head, 3);
    printf("删除节点后链表: ");
    printList(head);

    // 释放链表内存
    struct ListNode *cur = head;
    while (cur!= NULL) {
        struct ListNode *next = cur->next;
        free(cur);
        cur = next;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值