当前位置:   article > 正文

双链表的创建,按需删除等操作(可运行c语言源代码)_双链表的删除与输出

双链表的删除与输出

目录

一、双链表

1.1定义

1.2图解

二、代码

1.1双链表的创建

1.2输出双链表

1.3双链表的删除(进阶)

1.4全部代码(本地直接运行这个)


一、双链表

1.1定义

单链表结点中只有一个指向其后继的指针,使得单链表只能从头结点依次顺序地向后遍历。要访问某个结点的前驱结点(插入、删除操作时),只能从头开始遍历,访问后继结点的时间复杂度o(1),访问前驱结点的时间复杂度为 o(n)。为了克服单链表的上述缺点,引入了双链表,双链表结点中有两个指针 prior 和 next,分别指向其前驱结点和后继结点,

1.2图解

双链表:

双链表的插入:

双链表的删除:

二、代码

1.1双链表的创建

  1. // 创建双链表的函数
  2. struct Node* createLinkedList(int arr[], int size) {
  3. struct Node* head = NULL; // 头指针初始化为 NULL
  4. struct Node* tail = NULL; // 尾指针初始化为 NULL
  5. // 遍历数组并创建节点
  6. for (int i = 0; i < size; i++) {
  7. // 为新节点分配内存空间
  8. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  9. newNode->data = arr[i]; // 设置新节点的数据值
  10. newNode->next = NULL; // 新节点的后继节点指针初始化为 NULL
  11. // 处理链表头尾
  12. if (head == NULL) {
  13. // 如果链表为空,将新节点设置为头节点和尾节点
  14. head = newNode;
  15. tail = newNode;
  16. } else {
  17. // 如果链表不为空,将新节点链接到尾部,并更新尾节点指针
  18. tail->next = newNode; // 当前尾节点的后继节点指向新节点
  19. newNode->prev = tail; // 新节点的前驱节点指向当前尾节点
  20. tail = newNode; // 更新尾节点指针为新节点
  21. }
  22. }
  23. return head; // 返回链表的头指针
  24. }

1.2输出双链表

  1. // 输出节点数值的函数
  2. void printNodeValues(struct Node* head) {
  3. printf("当前链表: ");
  4. while (head != NULL) {
  5. printf("%d ", head->data);
  6. head = head->next;
  7. }
  8. printf("\n");
  9. }

1.3双链表的删除(进阶)

删除值在 m 到 n 之间的节点

  1. // 删除值在 m 到 n 之间的节点的函数
  2. void deleteNodesInRange(struct Node** headRef, int m, int n) {
  3. // 从双链表的头部开始遍历
  4. struct Node* current = *headRef;
  5. while (current != NULL) {
  6. // 如果当前节点的值在 m 和 n 之间
  7. if (current->data >= m && current->data <= n) {
  8. // 如果当前节点有前驱节点,将其指向当前节点的下一个节点
  9. if (current->prev != NULL) {
  10. current->prev->next = current->next;
  11. }
  12. // 如果当前节点有后继节点,将其指向当前节点的前驱节点
  13. if (current->next != NULL) {
  14. current->next->prev = current->prev;
  15. }
  16. // 释放当前节点的内存空间
  17. struct Node* temp = current;
  18. // 将当前节点指针移动到下一个节点
  19. current = current->next;
  20. // 释放当前节点的内存空间
  21. free(temp);
  22. } else {
  23. // 如果当前节点的值不在 m 和 n 之间,继续遍历下一个节点
  24. current = current->next;
  25. }
  26. }
  27. }

1.4全部代码(本地直接运行这个)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // 双链表节点结构
  4. struct Node {
  5. int data;
  6. struct Node* prev;
  7. struct Node* next;
  8. };
  9. // 创建双链表的函数
  10. struct Node* createLinkedList(int arr[], int size) {
  11. struct Node* head = NULL; // 头指针初始化为 NULL
  12. struct Node* tail = NULL; // 尾指针初始化为 NULL
  13. // 遍历数组并创建节点
  14. for (int i = 0; i < size; i++) {
  15. // 为新节点分配内存空间
  16. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  17. newNode->data = arr[i]; // 设置新节点的数据值
  18. newNode->next = NULL; // 新节点的后继节点指针初始化为 NULL
  19. // 处理链表头尾
  20. if (head == NULL) {
  21. // 如果链表为空,将新节点设置为头节点和尾节点
  22. head = newNode;
  23. tail = newNode;
  24. } else {
  25. // 如果链表不为空,将新节点链接到尾部,并更新尾节点指针
  26. tail->next = newNode; // 当前尾节点的后继节点指向新节点
  27. newNode->prev = tail; // 新节点的前驱节点指向当前尾节点
  28. tail = newNode; // 更新尾节点指针为新节点
  29. }
  30. }
  31. return head; // 返回链表的头指针
  32. }
  33. // 删除值在 m 到 n 之间的节点的函数
  34. void deleteNodesInRange(struct Node** headRef, int m, int n) {
  35. // 从双链表的头部开始遍历
  36. struct Node* current = *headRef;
  37. while (current != NULL) {
  38. // 如果当前节点的值在 m 和 n 之间
  39. if (current->data >= m && current->data <= n) {
  40. // 如果当前节点有前驱节点,将其指向当前节点的下一个节点
  41. if (current->prev != NULL) {
  42. current->prev->next = current->next;
  43. }
  44. // 如果当前节点有后继节点,将其指向当前节点的前驱节点
  45. if (current->next != NULL) {
  46. current->next->prev = current->prev;
  47. }
  48. // 释放当前节点的内存空间
  49. struct Node* temp = current;
  50. // 将当前节点指针移动到下一个节点
  51. current = current->next;
  52. // 释放当前节点的内存空间
  53. free(temp);
  54. } else {
  55. // 如果当前节点的值不在 m 和 n 之间,继续遍历下一个节点
  56. current = current->next;
  57. }
  58. }
  59. }
  60. // 输出节点数值的函数
  61. void printNodeValues(struct Node* head) {
  62. printf("当前链表: ");
  63. while (head != NULL) {
  64. printf("%d ", head->data);
  65. head = head->next;
  66. }
  67. printf("\n");
  68. }
  69. int main() {
  70. int arr[] = {1, 2, 3, 4, 5};
  71. int size = sizeof(arr) / sizeof(arr[0]);
  72. printf("正在创建双链表...\n");
  73. struct Node* head = createLinkedList(arr, size);
  74. printNodeValues(head); // 调用新的输出函数
  75. int m = 2; // 设定要删除的范围
  76. int n = 4;
  77. printf("正在删除数值在 %d 到 %d 之间的节点...\n", m, n);
  78. deleteNodesInRange(&head, m, n);
  79. printNodeValues(head); // 调用新的输出函数
  80. printf("\n");
  81. return 0;
  82. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/456489
推荐阅读
相关标签
  

闽ICP备14008679号