当前位置: 首页 > news >正文

链表相关知识

一、什么是链表?
链表是一种动态数据结构,由一系列“节点”组成。每个节点包含两个部分:数据域(data):用于存储数据。
指针域(next):指向下一个节点。
链表不像数组那样在内存中连续存放,而是通过指针将零散的内存块串联起来使用。

二.单链表的结构定义

#include <stdio.h>
#include <stdlib.h>// 定义链表节点结构体
typedef struct Node {int data;            // 数据域struct Node* next;   // 指针域,指向下一个节点
} Node;

三.链表的基本操作及实现

1. 创建一个新节点

Node* createNode(int data) {Node* newNode = (Node*)malloc(sizeof(Node));if (!newNode) {printf("Memory allocation failed.\n");exit(1);}newNode->data = data;newNode->next = NULL;return newNode;
}

2. 头插法插入节点

void insertAtHead(Node** head, int data) {Node* newNode = createNode(data);newNode->next = *head;*head = newNode;
}

3. 尾插法插入节点

void insertAtTail(Node** head, int data) {Node* newNode = createNode(data);if (*head == NULL) {*head = newNode;return;}Node* temp = *head;while (temp->next != NULL) {temp = temp->next;}temp->next = newNode;
}

4. 在指定位置插入节点

void insertAtPosition(Node** head, int position, int data) {if (position < 0) {printf("Invalid position.\n");return;}if (position == 0) {insertAtHead(head, data);return;}Node* newNode = createNode(data);Node* temp = *head;for (int i = 0; temp != NULL && i < position - 1; i++) {temp = temp->next;}if (temp == NULL) {printf("Position out of bounds.\n");free(newNode);return;}newNode->next = temp->next;temp->next = newNode;
}

5. 删除节点(按值删除第一个匹配项)

void deleteNodeByValue(Node** head, int key) {Node* temp = *head;Node* prev = NULL;// 如果头节点就是要删除的节点if (temp != NULL && temp->data == key) {*head = temp->next;free(temp);return;}// 寻找要删除的节点while (temp != NULL && temp->data != key) {prev = temp;temp = temp->next;}if (temp == NULL) {printf("Value not found in list.\n");return;}prev->next = temp->next;free(temp);
}

6. 查找节点

Node* searchNode(Node* head, int key) {Node* temp = head;while (temp != NULL) {if (temp->data == key)return temp;temp = temp->next;}return NULL;
}

7. 打印链表

void printList(Node* head) {Node* temp = head;while (temp != NULL) {printf("%d -> ", temp->data);temp = temp->next;}printf("NULL\n");
}

8. 销毁链表

void destroyList(Node** head) {Node* current = *head;Node* next;while (current != NULL) {next = current->next;free(current);current = next;}*head = NULL;
}

完整测试代码

int main() {Node* head = NULL;insertAtTail(&head, 10);insertAtTail(&head, 20);insertAtHead(&head, 5);insertAtPosition(&head, 2, 15); // 在位置2插入15printf("链表内容:\n");printList(head);printf("\n删除值为20的节点:\n");deleteNodeByValue(&head, 20);printList(head);printf("\n查找值为15的节点:\n");Node* found = searchNode(head, 15);if (found)printf("找到值 %d\n", found->data);elseprintf("未找到\n");destroyList(&head);return 0;
}

输出结果

链表内容:
5 -> 10 -> 15 -> 20 -> NULL删除值为20的节点:
5 -> 10 -> 15 -> NULL查找值为15的节点:
找到值 15

相关文章:

  • python技巧:pyvisa打开hislip设备;IEEE 488.2
  • 一日总结0605
  • Spring MVC 之 异常处理
  • 腾讯云服务器端口怎么全部打开?CVM和轻量端口开通教程
  • w10激活方法
  • Java中线程创建的三种方式
  • Java 9 新特性全整理
  • 快递电子面单便宜吗?怎样申请呢
  • 短视频矩阵系统技术saas源头6年开发构架
  • Python使用总结之Mac安装docker并配置wechaty
  • 鸿蒙Next开发真机调试签名申请流程
  • 基于eclipse进行Birt报表开发
  • 轨道交通可视化,赋能智慧车站运维
  • NumPy 比较、掩码与布尔逻辑
  • UDP:简洁高效的报文结构解析与关键注意事项
  • 45、web实验-抽取公共页面
  • 电商实践 基于token防止订单重复创建
  • Python基于方差-协方差方法实现投资组合风险管理的VaR与ES模型项目实战
  • Spring Boot 项目集成 Redis 问题:RedisTemplate 多余空格问题
  • 论文笔记——相干体技术在裂缝预测中的应用研究
  • 做论坛网站需要多少钱/广州网络公司
  • 做笔记的网站/百度信息流广告位置
  • 商城手机网站怎么做/快速推广
  • 网站如何做会员登录页面/seo网上培训多少钱
  • 漳州建网站/seo推广收费标准
  • 重庆网站建设入门培训/优化设计