C语言的链表,单链表,以及单链表的建立、输出操作详解

一、链表

链表(Linked List)是一种常见的数据结构,它由一系列节点(Node)组成,每个节点包含数据域指向下一个节点的指针域。链表的优点是插入和删除操作在已知位置的情况下时间复杂度为O(1),而不像数组需要O(n)的时间复杂度来移动元素。链表的缺点是随机访问元素时需要O(n)的时间。

二、单链表

在单链表中,每个节点包含数据域和一个指向下一个节点的指针。单链表的基本结构如下:

struct Node {
    int data;
    struct Node* next;
};

三、单链表的建立

 建立单链表通常包括创建头节点(head)并逐个添加新节点

// 创建一个新节点

struct Node* createNode(int data)
{
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

代码详解: 

函数定义:

struct Node* createNode(int data)
  • struct Node*:这是函数的返回类型,表示该函数返回一个指向 Node 结构体的指针。
  • createNode:这是函数的名字。
  • (int data):这是函数的参数列表,这里只有一个参数 data,类型为 int,表示新节点中要存储的数据。

 内存分配

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  • malloc(sizeof(struct Node)):使用 malloc 函数动态分配一块内存,大小为 struct Node 结构体的大小。malloc 返回一个指向这块内存的指针,但是它的类型是 void*
  • (struct Node*):将 malloc 返回的 void* 类型指针强制转换struct Node* 类型。
  • struct Node* newNode:定义一个指向 Node 结构体的指针变量 newNode,并将上一步得到的指针赋值给它。

初始化节点 

设置节点的数据域

newNode->data = data;
    • newNode->data:通过指针访问 Node 结构体中的 data 成员。
    • = data:将函数参数 data 的值赋给新节点的 data 成员。

设置节点的指针域

newNode->next = NULL;
  • newNode->next:通过指针访问 Node 结构体中的 next 成员。
  • = NULL:将 next 指针设置为 NULL,表示这个节点暂时没有连接到任何其他节点

返回新节点 

return newNode;

return newNode:返回指向新创建的 Node 结构体的指针。 

总结:该函数的作用是创建一个新的节点并初始化其数据域和指针域,然后返回这个节点的指针。

//初始化链表 

struct Node* createLinkedList(int arr[], int n)
 {
    if (n == 0) return NULL;
    struct Node* head = createNode(arr[0]);
    struct Node* current = head;
    for (int i = 1; i < n; i++) 
    {
        current->next = createNode(arr[i]);
        current = current->next;
    }
    return head;
}

 代码详解: 

函数定义:

struct Node* createLinkedList(int arr[], int n)
  • struct Node*:这是函数的返回类型,表示该函数返回一个指向 Node 结构体的指针。
  • createLinkedList:这是函数的名字。
  • (int arr[], int n):这是函数的参数列表,arr 是一个整数数组,表示要存储在链表中的数据,n 是数组的长度。

处理空数组: 

if (n == 0) return NULL;

如果数组长度为 0,直接返回 NULL,表示创建一个空链表。

创建头节点: 

struct Node* head = createNode(arr[0]);

调用 createNode 函数,使用数组的第一个元素 arr[0] 创建一个新节点,并将其指针赋给 headhead 是链表的头节点。 

创建其他节点:

初始化当前节点指针

struct Node* current = head;

定义一个指针 current,并将其指向链表的头节点 headcurrent 将用于遍历和构建链表。

创建链表的其余节点

for (int i = 1; i < n; i++) {
    current->next = createNode(arr[i]);
    current = current->next;
}
  • 使用 for 循环从 1 开始遍历数组,直到 n-1
  • 在每次循环中:
    • current->next = createNode(arr[i]):调用 createNode 函数,使用数组的当前元素 arr[i] 创建一个新节点,并将新节点的指针赋给当前节点的 next 指针。
    • current = current->next:将 current 更新为新创建的节点,以便在下一次循环中继续构建链表。

返回链表的头节点: 

return head;

函数最后返回链表的头节点 head,这是整个链表的入口点。

总结:该函数的作用是根据给定的数组创建一个单链表。它首先检查数组是否为空,如果是,则返回 NULL。否则,它创建链表的头节点,然后遍历数组,逐个创建新节点并将它们连接起来。最后,返回链表的头节点。这段代码展示了如何动态地构建链表并将数组中的数据转换成链表结构。

单链表建立完整代码

#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));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// 初始化链表
struct Node* createLinkedList(int arr[], int n) {
    if (n == 0) return NULL;
    struct Node* head = createNode(arr[0]);
    struct Node* current = head;
    for (int i = 1; i < n; i++) {
        current->next = createNode(arr[i]);
        current = current->next;
    }
    return head;
}

四、单链表的输出

void printLinkedList(struct Node* head)
 {
    struct Node* current = head;
    while (current != NULL) 
    {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}
  • 从头节点开始,用一个指针 current 指向链表的头节点
  • 进入 while 循环,只要 current 不为 NULL,就打印当前节点的数据。
  • current 更新为下一个节点,继续遍历。
  • 循环结束后,打印 NULL,表示链表的结束。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值