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

链表相关知识

一、什么是链表?
链表是一种动态数据结构,由一系列“节点”组成。每个节点包含两个部分:数据域(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

http://www.xdnf.cn/news/12200.html

相关文章:

  • 一键切换不同状态,3D数字孪生场景搭建更便捷!
  • 【iOS】cache_t分析
  • Qt 按钮类控件(Push Button 与 Radio Button)(1)
  • COMSOL学习笔记-静电场仿真
  • 可视化图解算法48:有效括号序列
  • DFORMER: RETHINKING RGBD REPRESENTATION LEARNING FOR SEMANTIC SEGMENTATION 论文浅析
  • 电厂数字孪生:智能优化助力碳中和
  • 【定昌linux开发板】设置用户密码过期时间
  • eNSP实现WDS手拉手业务
  • 如何做好一份技术文档?(上篇)
  • Spring AI(11)——SSE传输的MCP服务端
  • Spring Plugin框架应用实践:医院多租户客户端动态路由方案解析
  • App使用webview套壳引入h5(二)—— app内访问h5,顶部被手机顶部菜单遮挡问题,保留顶部安全距离
  • 两个错误教训记录--java变量作用域问题导致变量值异常
  • calico/node is not ready: BIRD is not ready: BGP not established with xxx
  • sumatraPDF设置深色界面
  • ArcGIS Maps SDK for JavaScript:使用图层过滤器只显示FeatureLayer的部分要素
  • LG P9990 [Ynoi Easy Round 2023] TEST_90 Solution
  • 风机下引线断点检测算法实现
  • 免费wordpress模板下载
  • Astro深度解析:颠覆传统的前端架构革命,打造极致性能的现代Web应用
  • KMP 算法中 next 数组的构建函数 get_next
  • linux-------------------------进程间通信(上)
  • QMetaObject::invokeMethod调用失败
  • 摄像机ISP处理流程
  • 【面经分享】京东
  • springboot实现查询学生
  • Spring @Scheduled vs XXL-JOB vs DolphinScheduler vs Airflow:任务调度框架全景对比
  • 电子电路:什么是扩散电容?
  • PC 360安全浏览器