找回密码
 立即注册
首页 业界区 业界 C中单向链表之增删改查

C中单向链表之增删改查

利怡悦 2025-11-25 10:10:00
C中单向链表之增删改查
  1. // 链表(Linked List)是一种基础但至关重要的数据结构。它通过动态内存分配实现数据的非连续存储,解决了数组的固定长度和插入/删除低效的问题。无论是算法面试还是实际开发,链表都是高频考点和核心技能之一。
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <cstring>
  6. using namespace std;
  7. // 单向链表(Singly Linked List)
  8. typedef struct Node
  9. {
  10.     int num;
  11.     char data[20];
  12.     struct Node *next;
  13.     Node(int x) : num(num), next(nullptr) {}
  14.     Node() {}
  15. } STU, Node, *PNode;
  16. // 链表创建(头插法)
  17. PNode linked_create_head1()
  18. {
  19.     int num, i = 0;
  20.     cout << "请输入节点数量:" << endl;
  21.     cin >> num;
  22.     PNode head = NULL;
  23.     PNode current = NULL;
  24.     while (i < num)
  25.     {
  26.         PNode temp;
  27.         PNode node = (PNode)malloc(sizeof(Node));
  28.         if (node == NULL)
  29.         {
  30.             printf("内存分配失败!\n");
  31.             exit(1);
  32.         }
  33.         memset(node, 0, sizeof(Node));
  34.         cout << "请输入第" << i << "节点data:" << endl;
  35.         cin >> node->data;
  36.         // 如果当前头节点为空,则将新节点作为头节点
  37.         if (head == NULL)
  38.         {
  39.             head = node;
  40.         }
  41.         else
  42.         {
  43.             node->next = head; // 将新节点的指针域指向head(老节点)
  44.             head = node; // 将新节点作为头节点
  45.         }
  46.         i++;
  47.     }
  48.     // 给节点编号
  49.     current = head;
  50.     for (int i = 0; i < num; i++)
  51.     {
  52.         current->num = i;
  53.         current = current->next;
  54.     }
  55.     return head;
  56. }
  57. // 链表创建(尾插法)
  58. PNode linked_create_tail()
  59. {
  60.     int num;
  61.     cout << "请输入节点数量:" << endl;
  62.     cin >> num;
  63.     PNode head = nullptr;
  64.     PNode tail = nullptr; // 记录尾节点
  65.     for (int i = 0; i < num; i++)
  66.     {
  67.         // 创建新节点
  68.         PNode node = (PNode)malloc(sizeof(Node));
  69.         if (node == NULL)
  70.         {
  71.             printf("内存分配失败!\n");
  72.             exit(1);
  73.         }
  74.         memset(node, 0, sizeof(Node));
  75.         cout << "请输入第" << i + 1 << "个节点data:" << endl;
  76.         cin >> node->data;
  77.         node->num = i; // 直接设置节点编号
  78.         // 如果是第一个节点
  79.         if (head == nullptr)
  80.         {
  81.             head = node;
  82.             tail = node;
  83.             node->next = nullptr;
  84.         }
  85.         else
  86.         {
  87.             // 将新节点连接到链表尾部
  88.             tail->next = node;
  89.             tail = node; // 更新尾节点
  90.             node->next = nullptr;
  91.         }
  92.     }
  93.     return head;
  94. }
  95. // 释放链表
  96. void free_list(PNode head)
  97. {
  98.     // 内存释放函数
  99.     // 当前节点指针
  100.     if (head == NULL)
  101.     {
  102.         cout << "链表为空" << endl;
  103.         return;
  104.     }
  105.     while (head != NULL)
  106.     {                      // 遍历链表
  107.         PNode temp = head; // 临时保存当前节点
  108.         head = head->next; // 移动到下一个节点
  109.         free(temp);        // 释放原节点内存
  110.     }
  111. }
  112. void print_list(PNode *head)
  113. {
  114.     // 检查链表是否为空
  115.     if (*head == NULL)
  116.     {
  117.         printf("链表为空\n");
  118.         return;
  119.     }
  120.     PNode current = *head; // 使用临时变量避免修改传入的 head 指针
  121.     // 遍历整个链表,包括最后一个节点
  122.     while (current) // 修改为 head 而不是 head->next
  123.     {
  124.         printf("当前节点为:%d,节点数据为%s\n", current->num, current->data);
  125.         current = current->next;
  126.     }
  127. }
  128. /*
  129. task     : 1.从指定位置插入节点
  130.            2.创建一个有序链表,根据value值排序
  131. */
  132. void update_data(PNode head, int value)
  133. {
  134.     PNode temp = head;
  135.     while (temp)
  136.     {
  137.         if (value == temp->num)
  138.         {
  139.             // 找到指定节点
  140.             // 先清空原始数据
  141.             memset(temp->data, 0, sizeof(temp->data));
  142.             cout << "请输入要更改的节点数据:" << endl;
  143.             cin >> temp->data;
  144.             cout << "节点数据更新成功" << endl;
  145.             printf("更改后的数据为%s\n", temp->data);
  146.             return;
  147.         }
  148.         temp = temp->next;
  149.     }
  150. }
  151. void insert_node(PNode head)
  152. {
  153.     PNode temp = head;
  154.     PNode new_node = (PNode)malloc(sizeof(Node));
  155.     if (new_node == NULL)
  156.     {
  157.         printf("内存分配失败!\n");
  158.         exit(1);
  159.     }
  160.     memset(new_node, 0, sizeof(Node));
  161.     cout << "请输入插入节点的序号" << endl;
  162.     cin >> new_node->num;
  163.     cout << "请输入插入节点的数据" << endl;
  164.     cin >> new_node->data;
  165.     while (temp)
  166.     {
  167.         if (temp->num >= new_node->num)
  168.         {
  169.             if (temp->num == new_node->num)
  170.             {
  171.                 new_node->next = temp; // 调整后续链表
  172.             }
  173.             temp->num = temp->num + 1; // 节点序号递增
  174.         }
  175.         temp = temp->next;
  176.     }
  177.     //设置前一位node的next为new_node
  178.     temp = head;
  179.     while (temp)
  180.     {
  181.         if ((temp->num) + 1 == new_node->num)
  182.         {
  183.             temp->next = new_node;
  184.             break; // 遍历链表找到插入的位置
  185.         }
  186.     }
  187. }
  188. int main()
  189. {
  190.     PNode head;
  191.     head = linked_create_head1();
  192.     print_list(&head);
  193.     insert_node(head);
  194.     print_list(&head);
  195.     update_data(head, 1);
  196.     print_list(&head);
  197.     free_list(head);
  198.     return 0;
  199. }
复制代码
关于insert节点理解:
1.png

我们这里要想插入一个节点就得先找到它的序号前一位和后一位,所以我们将前一位node的next指向new_node,将new_node的next指向后一位node,将后几位node的序号各加一
本篇用于记录学习,如有问题欢迎指出

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册