C语言,指针链表详解解说及代码示例

C语言,指针链表详解解说及代码示例
指针链表是一种常用的数据结构,用于存储和组织数据。它由一系列节点组成,每个节点包含数据和一个指向下一个节点的指针。通过这种方式,可以动态地添加、删除和访问节点,实现灵活的数据操作。
下面是一个简单的指针链表的代码示例,以便更好地理解:

#include <stdio.h>
#include <stdlib.h>
 // 定义链表节点结构体
struct Node {
    int data;           // 节点数据
    struct Node* next;  // 指向下一个节点的指针
};
 // 创建链表节点
struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("内存分配失败!\n");
        exit(1);
    }
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}
 // 在链表末尾插入节点
void insertAtEnd(struct Node** head, int data) {
    struct Node* newNode = createNode(data);
     if (*head == NULL) {
        *head = newNode;
    } else {
        struct Node* temp = *head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}
 // 打印链表
void printList(struct Node* head) {
    struct Node* temp = head;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}
 // 主函数
int main() {
    struct Node* head = NULL;
     // 在链表末尾插入节点
    insertAtEnd(&head, 10);
    insertAtEnd(&head, 20);
    insertAtEnd(&head, 30);
     // 打印链表
    printf("链表内容: ");
    printList(head);
     return 0;
}

在以上示例中,我们首先定义了一个链表节点的结构体,包含数据和指向下一个节点的指针。然后,我们实现了创建节点的函数 createNode ,用于动态分配内存并初始化节点的数据和指针。接下来,我们定义了插入节点的函数 insertAtEnd ,它将新节点插入到链表的末尾。最后,我们实现了打印链表的函数 printList ,用于遍历链表并打印节点的数据。
在主函数中,我们创建一个指向链表头节点的指针 head ,然后通过调用 insertAtEnd 函数插入三个节点。最后,我们调用 printList 函数打印链表的内容。
这只是一个简单的指针链表示例,你可以根据需要扩展和修改代码来实现更复杂的链表操作,如插入节点到指定位置、删除节点等。指针链表是C语言中常用的数据结构,对于存储和操作动态数据非常有用。

在上面的代码基础上,我们可以添加修改和删除节点的功能。下面是修改和删除节点的代码示例:

// 修改指定位置节点的数据
void modifyNode(struct Node* head, int position, int newData) {
    struct Node* temp = head;
    int count = 0;
    while (temp != NULL && count < position) {
        temp = temp->next;
        count++;
    }
    if (temp != NULL) {
        temp->data = newData;
        printf("节点 %d 的数据已修改为 %d\n", position, newData);
    } else {
        printf("位置 %d 无效\n", position);
    }
}
 // 删除指定位置的节点
void deleteNode(struct Node** head, int position) {
    if (*head == NULL) {
        printf("链表为空,无法删除节点\n");
        return;
    }
    struct Node* temp = *head;
    if (position == 0) {
        *head = temp->next;
        free(temp);
        printf("节点 %d 已被删除\n", position);
        return;
    }
    int count = 0;
    while (temp != NULL && count < position - 1) {
        temp = temp->next;
        count++;
    }
    if (temp == NULL || temp->next == NULL) {
        printf("位置 %d 无效\n", position);
        return;
    }
    struct Node* nextNode = temp->next->next;
    free(temp->next);
    temp->next = nextNode;
    printf("节点 %d 已被删除\n", position);
}

在上述代码中,我们添加了两个新的函数。 modifyNode 函数用于修改指定位置节点的数据,它接受链表头节点和目标位置作为参数,并在找到目标位置后修改节点的数据。如果目标位置无效,则会输出相应的错误信息。
  deleteNode 函数用于删除指定位置的节点,它接受链表头节点和目标位置作为参数。如果链表为空,则会输出错误信息。如果目标位置为0,则直接删除头节点。否则,我们遍历链表找到目标位置的前一个节点,然后修改其 next 指针,跳过目标位置的节点,并释放内存。如果目标位置无效,则会输出相应的错误信息。
你可以在主函数中调用这两个新函数来测试修改和删除节点的功能。
请注意,这只是一个简单的示例,你可以根据需要扩展和修改代码来实现更复杂的链表操作。

相关推荐

  1. C语言指针详解解说代码示例

    2023-12-30 00:12:04       42 阅读
  2. 经典C语言程序之 编程】详细解析示例代码

    2023-12-30 00:12:04       39 阅读
  3. C语言尾递归知识代码示例

    2023-12-30 00:12:04       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-30 00:12:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-30 00:12:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-30 00:12:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-30 00:12:04       20 阅读

热门阅读

  1. 面试题:BIO、NIO、AIO 的区别是什么?

    2023-12-30 00:12:04       43 阅读
  2. Top100 C++编程面试问题

    2023-12-30 00:12:04       31 阅读
  3. 网络安全面试题目

    2023-12-30 00:12:04       33 阅读
  4. Linux 系统参数和变量配置

    2023-12-30 00:12:04       25 阅读
  5. 篇章二 | Python 入门指南:深入理解基础数据类型

    2023-12-30 00:12:04       35 阅读
  6. Impala中kudu基础理论详解(超详细)

    2023-12-30 00:12:04       37 阅读
  7. centos 编译安装 cmake

    2023-12-30 00:12:04       47 阅读
  8. 【生成和为20的四个随机数】

    2023-12-30 00:12:04       33 阅读
  9. C#编程简单应用程序批量修改文件名3.0

    2023-12-30 00:12:04       33 阅读