【单链表】简单补充


前言

单链表没有内置的索引,但我们可以通过遍历链表来模拟索引操作。
例如,我们可以通过遍历找到第 n个节点来实现基于索引的操作。

参数校验说明
traverseList()函数:

*在遍历链表之前,检查链表头指针是否为NULL。如果为空,打印一条消息并返回,因为遍历一个空链表是无意义的。

insertAtEnd()函数:

*在插入节点之前,首先检查传入的头指针的引用是否为NULL。如果为NULL,打印一条错误消息并返回。
*确保headRef不为NULL后,再检查链表是否为空。如果为空,将新节点设置为头节点。
*如果链表不为空,则遍历到链表尾部并插入新节点。

下面是一些示例函数,演示如何在单链表中根据索引进行插入、删除和查找操作。


一、示例代码:

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

// 定义链表节点结构体并为其起别名
typedef struct Node {
    int data;
    struct Node* next;
} ListNode;

// 函数用于创建新节点
ListNode* createNode(int data) {
    ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
    if (newNode == NULL) {
        printf("Memory allocation failed\n");
        exit(1);
    }
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// 函数用于释放链表的内存
void freeList(ListNode* head) {
    ListNode* current = head;
    while (current != NULL) {
        ListNode* temp = current;
        current = current->next;
        free(temp);
    }
}

// 函数用于遍历链表并打印节点的值
void traverseList(ListNode* head) {
    if (head == NULL) {
        printf("List is empty\n");
        return;
    }

    ListNode* current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

// 函数用于在链表尾部插入新节点
void insertAtEnd(ListNode** headRef, int data) {
    if (headRef == NULL) {
        printf("Invalid list reference\n");
        return;
    }

    ListNode* newNode = createNode(data);
    if (*headRef == NULL) {
        *headRef = newNode;
        return;
    }

    ListNode* current = *headRef;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = newNode;
}

// 函数用于在链表的指定索引位置插入新节点
void insertAtIndex(ListNode** headRef, int index, int data) {
    if (headRef == NULL) {
        printf("Invalid list reference\n");
        return;
    }

    if (index < 0) {
        printf("Invalid index\n");
        return;
    }

    ListNode* newNode = createNode(data);

    if (index == 0) {
        newNode->next = *headRef;
        *headRef = newNode;
        return;
    }

    ListNode* current = *headRef;
    for (int i = 0; i < index - 1; i++) {
        if (current == NULL) {
            printf("Index out of bounds\n");
            free(newNode);
            return;
        }
        current = current->next;
    }

    newNode->next = current->next;
    current->next = newNode;
}

// 函数用于在链表的指定索引位置删除节点
void deleteAtIndex(ListNode** headRef, int index) {
    if (headRef == NULL || *headRef == NULL) {
        printf("Invalid list reference\n");
        return;
    }

    if (index < 0) {
        printf("Invalid index\n");
        return;
    }

    ListNode* current = *headRef;

    if (index == 0) {
        *headRef = current->next;
        free(current);
        return;
    }

    ListNode* previous = NULL;
    for (int i = 0; i < index; i++) {
        if (current == NULL) {
            printf("Index out of bounds\n");
            return;
        }
        previous = current;
        current = current->next;
    }

    if (current == NULL) {
        printf("Index out of bounds\n");
        return;
    }

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

// 函数用于在链表中查找指定索引的节点
ListNode* getNodeAtIndex(ListNode* head, int index) {
    if (head == NULL) {
        printf("Invalid list reference\n");
        return NULL;
    }

    if (index < 0) {
        printf("Invalid index\n");
        return NULL;
    }

    ListNode* current = head;
    for (int i = 0; i < index; i++) {
        if (current == NULL) {
            printf("Index out of bounds\n");
            return NULL;
        }
        current = current->next;
    }

    return current;
}

int main() {
    // 创建链表节点
    ListNode* head = createNode(1);
    ListNode* second = createNode(2);
    ListNode* third = createNode(3);

    // 连接链表节点
    head->next = second;
    second->next = third;

    // 遍历链表并打印节点的值
    printf("Original list: ");
    traverseList(head);

    // 在链表尾部插入新节点
    insertAtEnd(&head, 4);

    // 遍历链表并打印节点的值
    printf("After inserting at end: ");
    traverseList(head);

    // 在链表的指定索引位置插入新节点
    insertAtIndex(&head, 2, 5);

    // 遍历链表并打印节点的值
    printf("After inserting at index 2: ");
    traverseList(head);

    // 在链表的指定索引位置删除节点
    deleteAtIndex(&head, 3);

    // 遍历链表并打印节点的值
    printf("After deleting at index 3: ");
    traverseList(head);

    // 获取并打印指定索引的节点
    ListNode* node = getNodeAtIndex(head, 2);
    if (node != NULL) {
        printf("Node at index 2: %d\n", node->data);
    }

    // 释放链表内存
    freeList(head);

    return 0;
}

新增功能说明

insertAtIndex()函数:

*根据指定的索引在链表中插入新节点。
*对索引进行校验,防止非法索引值。
*如果索引为0,直接在头部插入新节点。
*否则,遍历链表找到指定位置,插入新节点。

deleteAtIndex()函数:

*根据指定的索引删除链表中的节点。
*对索引和链表进行校验,防止非法操作。
*如果索引为0,直接删除头节点。
*否则,遍历链表找到指定位置,删除节点。

getNodeAtIndex()函数:

*根据指定的索引查找链表中的节点。
*对索引和链表进行校验,防止非法操作。
*遍历链表找到指定位置并返回对应节点。

通过这些新增的函数,链表可以模拟索引操作,支持在特定位置插入、删除和查找节点。这样可以更方便地对链表进行操作,使代码更具可读性和易用性。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值