• 数据结构(C语言)——双链表


    有了单链表的编写经验,双链表变得格外容易。点击看前一篇-单链表

    下面是代码:

    1. #include
    2. #include
    3. #define E int
    4. typedef struct node {
    5. E element;
    6. struct node* pre;
    7. struct node* next;
    8. }node;
    9. void initialise(node* head) {//初始化头节点
    10. head->element = 0;
    11. head->next = NULL;
    12. head->pre = NULL;
    13. }
    14. int add_node(node* head,int index,E ele) {
    15. node* tem = head->next;//先将下一节点指针给一个临时变量
    16. if (index < 1)return -1;
    17. while (--index && tem != NULL) {
    18. head= tem;
    19. tem = tem->next;//移动指向下一节点
    20. }
    21. if (index == 0 && tem == NULL) {
    22. node* ptr = (node*)malloc(sizeof(node));
    23. if (ptr == NULL)return -1;
    24. ptr->element = ele;
    25. ptr->pre = head;
    26. head->next = ptr;
    27. ptr->next = NULL;
    28. return 1;
    29. }
    30. else if (tem != NULL) {//在中间添加的情况
    31. node* ptr = (node*)malloc(sizeof(node));
    32. ptr->element = ele;
    33. ptr->pre = head;
    34. head->next = ptr;
    35. ptr->next = tem;
    36. tem->pre = ptr;
    37. return 1;
    38. }
    39. return -1;//没有这个位置的节点
    40. }
    41. print_node(node* head) {
    42. while (head->next != NULL) {
    43. head = head->next;//节点指针移动
    44. printf("%d\n", head->element);
    45. }
    46. }
    47. int del_node(node* head, E ele) {
    48. node* tem = head->next;
    49. int count = 0;
    50. while (tem != NULL) {
    51. if (tem->element == ele) {
    52. head->next = tem->next;
    53. if (tem->next != NULL)tem->next->pre = head;//如果是删的最后一个元素,这句话不用执行
    54. count++;
    55. }
    56. head = tem;
    57. tem = tem->next;//节点指针移动
    58. }
    59. if(!count)return -1;
    60. return 1;
    61. }
    62. node* find_node(node* head, E ele) {
    63. head = head->next;//先移动头节点指针
    64. while (head != NULL) {
    65. if (head->element == ele) {
    66. return head;
    67. }
    68. head = head->next;//移动到下一节点
    69. }
    70. }
    71. node* modif_node(node* head, int index,E ele) {
    72. head = head->next;//先移动头节点指针
    73. if (index < 1)return NULL;
    74. while (--index && head != NULL) {
    75. head = head->next;//继续移动到需要位置
    76. }
    77. if (head == NULL)return NULL;
    78. head->element = ele;
    79. return head;
    80. }
    81. int main() {
    82. node head;
    83. initialise(&head);
    84. for (int i = 1; i <= 8; i++) {
    85. add_node(&head, 1, 20*i);
    86. }
    87. add_node(&head, 9, 520);
    88. print_node(&head);
    89. printf("----------\n");
    90. del_node(&head, 160);
    91. print_node(&head);
    92. printf("----------\n");
    93. node* find = find_node(&head, 520);
    94. //找这个数据的前一个数据
    95. printf("%d\n", find->pre->element);
    96. printf("%d\n", find->element);
    97. printf("----------\n");
    98. node* mod = modif_node(&head,9 ,999);
    99. if(mod!=NULL)printf("%d\n", mod->element);
    100. printf("----------\n");
    101. print_node(&head);
    102. return 0;
    103. }

  • 相关阅读:
    Spring Boot跨域处理
    Vue2 基础四前后端交互
    计算机视觉项目实战-目标检测与识别
    通过低代码平台实现移动办公,轻量快捷方便
    Lambda 架构 vs Kappa 架构
    consul持久化失败
    【2023】springboot通过阿里云oss进行文件单个批量文件上传下载
    基于51单片机出租车计费设计(proteus仿真+程序+原理图+设计说明书)
    generated-requests.http
    PID控制算法
  • 原文地址:https://blog.csdn.net/weixin_56821642/article/details/132837454