• 循环双链表的操作


    归纳编程学习的感悟,
    记录奋斗路上的点滴,
    希望能帮到一样刻苦的你!
    如有不足欢迎指正!
    共同学习交流!
    🌎欢迎各位→点赞 👍+ 收藏⭐ + 留言​📝

    每一个裂缝都是为透出光而努力!

    CDLinkList.h

    1. #pragma once
    2. #include
    3. #include
    4. typedef int DataType;
    5. typedef struct CDLNode
    6. {
    7. DataType data;
    8. struct CDLNode* prior;
    9. struct CDLNode* next;
    10. }CDLNode,*CDLinkList;
    11. void InitCDLinkList(CDLinkList* head);
    12. int InsertElem(CDLinkList head, int i, DataType e);
    13. int DeleteElem(CDLinkList head, int i, DataType* e);
    14. void InsertElemR(CDLinkList head, DataType e);
    15. void InsertElemF(CDLinkList head, DataType e);

    CDLinkList.cpp

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include"CDLinkList.h"
    3. void InitCDLinkList(CDLinkList* head)
    4. {
    5. (*head) = (CDLinkList)malloc(sizeof(CDLNode));
    6. (*head)->prior = (*head);
    7. (*head)->next = (*head);
    8. }
    9. int InsertElem(CDLinkList head, int i, DataType e)
    10. {
    11. CDLNode* pre, * p;
    12. int j;
    13. if (i <= 0)
    14. {
    15. printf("插入位置错误!\n");
    16. return -1;
    17. }
    18. pre = head;
    19. j = 0;
    20. while (pre->next != head && j-1)
    21. {
    22. pre = pre->next;
    23. j++;
    24. }
    25. if (j != i - 1)
    26. {
    27. printf("插入位置错误!\n");
    28. return 0;
    29. }
    30. p = (CDLNode*)malloc(sizeof(CDLNode));
    31. p->data = e;
    32. p->prior = pre;
    33. p->next = pre->next;
    34. pre->next->prior = p;
    35. pre->next = p;
    36. return 1;
    37. }
    38. int DeleteElem(CDLinkList head, int i, DataType* e)
    39. {
    40. CDLNode* p;
    41. int j;
    42. if (i <= 0)
    43. {
    44. printf("删除位置错误!\n");
    45. return -1;
    46. }
    47. p = head;
    48. j = 0;
    49. while (p->next != head && j < i)
    50. {
    51. p = p->next;
    52. j++;
    53. }
    54. if (j != i)
    55. {
    56. printf("删除位置错误!\n");
    57. return 0;
    58. }
    59. p->prior->next = p->next;
    60. p->next->prior = p->prior;
    61. *e = p->data;
    62. free(p);
    63. return 1;
    64. }
    65. void InsertElemR(CDLinkList head, DataType e)
    66. {
    67. CDLNode* pre, * p;
    68. pre = head;
    69. while (pre->next != head)
    70. {
    71. pre = pre->next;
    72. }
    73. p = (CDLNode*)malloc(sizeof(CDLNode));
    74. p->data = e;
    75. p->prior = pre;
    76. p->next = head;
    77. head->prior = p;
    78. pre->next = p;
    79. }
    80. void InsertElemF(CDLinkList head, DataType e)
    81. {
    82. CDLNode* p;
    83. p = (CDLNode*)malloc(sizeof(CDLNode));
    84. p->data = e;
    85. p->prior = head;
    86. p->next = head->next;
    87. head->next->prior = p;
    88. head->next = p;
    89. }

  • 相关阅读:
    【java8新特性】02:常见的函数式接口
    【每日一题Day338】LC2582递枕头 | 模拟+数学
    如何破解企业数字化转型的焦虑
    M语言-数据类型
    Kafka为什么这么快?它的高性能是如何实现的?
    Hadoop-19 Flume Agent批量采集数据到HDFS集群 监听Hive的日志 操作则把记录写入到HDFS 方便后续分析
    【C++11新算法】all_of、any_of、none_of算法
    Matlab绘制双坐标轴图示例函数yyaxis
    JVM之强软弱虚引用
    【博客439】Kubernetes CRI
  • 原文地址:https://blog.csdn.net/2301_80585598/article/details/138194323