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

C中单向链表之增删改查

// 链表(Linked List)是一种基础但至关重要的数据结构。它通过动态内存分配实现数据的非连续存储,解决了数组的固定长度和插入/删除低效的问题。无论是算法面试还是实际开发,链表都是高频考点和核心技能之一。 #include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; // 单向链表(Singly Linked List) typedef struct Node { int num; char data[20]; struct Node *next; Node(int x) : num(num), next(nullptr) {} Node() {} } STU, Node, *PNode; // 链表创建(头插法) PNode linked_create_head1() { int num, i = 0; cout << "请输入节点数量:" << endl; cin >> num; PNode head = NULL; PNode current = NULL; while (i < num) { PNode temp; PNode node = (PNode)malloc(sizeof(Node)); if (node == NULL) { printf("内存分配失败!\n"); exit(1); } memset(node, 0, sizeof(Node)); cout << "请输入第" << i << "节点data:" << endl; cin >> node->data; // 如果当前头节点为空,则将新节点作为头节点 if (head == NULL) { head = node; } else { node->next = head; // 将新节点的指针域指向head(老节点) head = node; // 将新节点作为头节点 } i++; } // 给节点编号 current = head; for (int i = 0; i < num; i++) { current->num = i; current = current->next; } return head; } // 链表创建(尾插法) PNode linked_create_tail() { int num; cout << "请输入节点数量:" << endl; cin >> num; PNode head = nullptr; PNode tail = nullptr; // 记录尾节点 for (int i = 0; i < num; i++) { // 创建新节点 PNode node = (PNode)malloc(sizeof(Node)); if (node == NULL) { printf("内存分配失败!\n"); exit(1); } memset(node, 0, sizeof(Node)); cout << "请输入第" << i + 1 << "个节点data:" << endl; cin >> node->data; node->num = i; // 直接设置节点编号 // 如果是第一个节点 if (head == nullptr) { head = node; tail = node; node->next = nullptr; } else { // 将新节点连接到链表尾部 tail->next = node; tail = node; // 更新尾节点 node->next = nullptr; } } return head; } // 释放链表 void free_list(PNode head) { // 内存释放函数 // 当前节点指针 if (head == NULL) { cout << "链表为空" << endl; return; } while (head != NULL) { // 遍历链表 PNode temp = head; // 临时保存当前节点 head = head->next; // 移动到下一个节点 free(temp); // 释放原节点内存 } } void print_list(PNode *head) { // 检查链表是否为空 if (*head == NULL) { printf("链表为空\n"); return; } PNode current = *head; // 使用临时变量避免修改传入的 head 指针 // 遍历整个链表,包括最后一个节点 while (current) // 修改为 head 而不是 head->next { printf("当前节点为:%d,节点数据为%s\n", current->num, current->data); current = current->next; } } /* task : 1.从指定位置插入节点 2.创建一个有序链表,根据value值排序 */ void update_data(PNode head, int value) { PNode temp = head; while (temp) { if (value == temp->num) { // 找到指定节点 // 先清空原始数据 memset(temp->data, 0, sizeof(temp->data)); cout << "请输入要更改的节点数据:" << endl; cin >> temp->data; cout << "节点数据更新成功" << endl; printf("更改后的数据为%s\n", temp->data); return; } temp = temp->next; } } void insert_node(PNode head) { PNode temp = head; PNode new_node = (PNode)malloc(sizeof(Node)); if (new_node == NULL) { printf("内存分配失败!\n"); exit(1);

http://www.jsqmd.com/news/1099725/

相关文章:

  • 导入Seata-server所需SQL
  • 四川大学《微积分I-1》期末试卷及答案2016-2025学年PDF
  • OpenHarness源码研究-5-基础设施
  • 什么是配置中心?有哪些常见的配置中心?
  • 【车载 AOSP 16 蓝牙(bluedroid)服务】【qcom 平台双蓝牙】【13.耳机如何协商采样率:从 AVDTP 到 AAC 44100 的一条路】
  • 第六周学习报告
  • 我做了一个基于心理测评和场景记忆的 AI 伴侣产品 CandyAI
  • 爆品之后:新消费品牌如何用数字化穿越增长瓶颈?
  • YOLO目标检测论文实战指南:从模型改进到实验写作全流程
  • 2.1.8 this指针
  • 免费开源NoFences桌面分区管理工具:3步打造高效整洁Windows桌面
  • Day10 | SFT 训练实操——用 QLoRA 微调 Qwen3-8B
  • BetterJoy完整指南:让Switch手柄在PC游戏上完美运行
  • 智谱大模型LLM一面,人麻了!!!
  • 【JAVA毕设源码分享】基于springboot的小区公共收益管理系统 的设计与实现(程序+文档+代码讲解+一条龙定制)
  • 光电经纬仪测量中的坐标系体系及其应用
  • CPT Markets:把外汇用户支持体系做到位——维度复盘与提示整理
  • 抖音内容批量采集与智能管理工具:从零到精通的完整指南
  • OpenAI / Claude API 报错 401、403、429 怎么解决?一文讲清 API Key 失效排查思路
  • 量子虚时演化算法原理与sine-Gordon模型模拟实践
  • FreeCAD源码分析: Property View
  • 我一个人 11 天交付了两个模块——不是会分身,是让两个 AI 打了配合
  • 1115.交替打印FooBar
  • 【课程设计/毕业设计】基于 SpringBoot 的农业设备销售订单管理系统的设计与实现 基于 SpringBoot 的智慧农机综合服务管理系统【附源码、数据库、万字文档】
  • 修改很简单,但网上讲这点的文档不多,因此多记一笔。另外基于out_ptr会临时转移所有权这点来看,共享所有权模型的std::shared_ptr其实并不适合使用out_ptr,虽然标准没有禁止甚至还要
  • playwright-拖拽验证码
  • LeWorldModel:基于JEPA的轻量化世界模型实践指南
  • 为什么要将 RTF 转换为 PDF?
  • 告别泰拉瑞亚原版限制:tModLoader模组开发实战手册
  • Opencv延迟优化