• 【数据结构】双向链表


    一、main函数

    1. #include
    2. #include "./3.doublelinklist.h"
    3. int main(int argc, const char *argv[])
    4. {
    5. doublelinklist* head = creatr_doublelinklist();
    6. insertHead_doublelinklist(head,999);
    7. insertHead_doublelinklist(head,888);
    8. insertHead_doublelinklist(head,777);
    9. insertHead_doublelinklist(head,666);
    10. insertHead_doublelinklist(head,555);
    11. insertHead_doublelinklist(head,444);
    12. showdoublelinklist(head);
    13. insertEnd_doublelinklist(head,123);
    14. insertEnd_doublelinklist(head,456);
    15. insertEnd_doublelinklist(head,789);
    16. showdoublelinklist(head);
    17. delHead_doublelinklist(head);
    18. showdoublelinklist(head);
    19. delEnd_doublelinklist(head);
    20. showdoublelinklist(head);
    21. insertByindex_doublelinklist(head,3,111);
    22. insertByindex_doublelinklist(head,20,99);
    23. showdoublelinklist(head);
    24. delByindex_doublelinklist(head,2);
    25. delByindex_doublelinklist(head,20);
    26. showdoublelinklist(head);
    27. return 0;
    28. }

    二、功能函数

    1. #include
    2. #include
    3. #include "./3.doublelinklist.h"
    4. doublelinklist* creatr_doublelinklist()
    5. {
    6. doublelinklist*head = (doublelinklist*)malloc(sizeof(doublelinklist));
    7. if(NULL == head)
    8. {
    9. printf("结点创建失败,双向链表申请失败\n");
    10. return NULL;
    11. }
    12. head->next = NULL;
    13. head->prev = NULL;
    14. head->text.len=0;
    15. return head;
    16. }
    17. //判空
    18. int isEmpty_doublelinklist(doublelinklist*head)
    19. {
    20. return head->next == NULL?1:0;
    21. }
    22. //遍历
    23. void showdoublelinklist(doublelinklist*head)
    24. {
    25. doublelinklist*p = head;
    26. while(p->next != NULL )
    27. {
    28. p=p->next;
    29. printf("%d ",p->text.data);
    30. }
    31. printf("\n");
    32. return ;
    33. }
    34. //头插法
    35. void insertHead_doublelinklist(doublelinklist*head,dataType num)
    36. {
    37. doublelinklist* temp = (doublelinklist*)malloc(sizeof(doublelinklist));
    38. if(NULL == temp)
    39. {
    40. printf("结点创建失败,头插失败\n");
    41. return ;
    42. }
    43. temp->text.data = num;
    44. temp->next = NULL;
    45. temp->prev = NULL;
    46. if(isEmpty_doublelinklist(head) == 1)
    47. {
    48. temp->next = head->next;
    49. head->next= temp;
    50. temp->prev = head;
    51. return ;
    52. }
    53. else
    54. {
    55. temp->next = head->next;
    56. head->next = temp;
    57. temp->next->prev = temp;
    58. temp->prev = head;
    59. }
    60. head->text.len++;
    61. return ;
    62. }
    63. //尾插法
    64. void insertEnd_doublelinklist(doublelinklist*head,dataType num)
    65. {
    66. doublelinklist* temp = (doublelinklist*)malloc(sizeof(doublelinklist));
    67. if(NULL == temp)
    68. {
    69. printf("结点创建失败,尾插失败\n");
    70. return ;
    71. }
    72. temp->text.data = num;
    73. temp->next = NULL;
    74. temp->prev = NULL;
    75. doublelinklist* p =head;
    76. while(p->next != NULL)
    77. {
    78. p=p->next;
    79. }
    80. temp->next = p->next;
    81. p->next = temp;
    82. temp->prev = p;
    83. head->text.len++;
    84. return ;
    85. }
    86. //头删法
    87. void delHead_doublelinklist(doublelinklist*head)
    88. {
    89. if(isEmpty_doublelinklist(head))
    90. {
    91. printf("双向链表为空,头删失败\n");
    92. return;
    93. }
    94. doublelinklist* temp = head->next;
    95. if(NULL == temp->next)
    96. {
    97. head->next= NULL;
    98. }
    99. else
    100. {
    101. head->next=temp->next;
    102. temp->next->prev=head;
    103. }
    104. free(temp);
    105. head->text.len--;
    106. return;
    107. }
    108. //尾删法
    109. void delEnd_doublelinklist(doublelinklist*head)
    110. {
    111. if(isEmpty_doublelinklist(head))
    112. {
    113. printf("双向链表为空,尾删失败\n");
    114. return;
    115. }
    116. doublelinklist*temp;
    117. doublelinklist*p=head;
    118. while(p->next->next != NULL)
    119. {
    120. p=p->next;
    121. }
    122. temp=p->next;
    123. p->next=NULL;
    124. free(temp);
    125. head->text.len--;
    126. return;
    127. }
    128. //按位置插入
    129. void insertByindex_doublelinklist(doublelinklist*head,int index,dataType num)
    130. {
    131. doublelinklist* temp = (doublelinklist*)malloc(sizeof(doublelinklist));
    132. if(NULL == temp)
    133. {
    134. printf("结点创建失败,插入失败\n");
    135. return ;
    136. }
    137. temp->text.data = num;
    138. temp->next = NULL;
    139. temp->prev = NULL;
    140. if(index<1 || index >head->text.len+1)
    141. {
    142. printf("插入位置非法\n");
    143. return ;
    144. }
    145. doublelinklist* p=head;
    146. int i;
    147. for(i=1;i
    148. {
    149. p=p->next;
    150. }
    151. if(p->next != NULL)
    152. {
    153. temp->next = p->next;
    154. p->next = temp;
    155. temp->next->prev = temp;
    156. temp->prev = p;
    157. }
    158. else
    159. {
    160. temp->next = NULL;
    161. p->next = temp;
    162. temp->prev = p;
    163. }
    164. head->text.len++;
    165. return ;
    166. }
    167. //按位置删除
    168. void delByindex_doublelinklist(doublelinklist*head,int index)
    169. {
    170. if(isEmpty_doublelinklist(head))
    171. {
    172. printf("双向链表为空,删除失败\n");
    173. return;
    174. }
    175. if(index<1 || index >head->text.len+1)
    176. {
    177. printf("选择位置非法\n");
    178. return ;
    179. }
    180. doublelinklist*p=head;
    181. for(int i=1;i
    182. {
    183. p=p->next;
    184. }
    185. doublelinklist*temp = p->next;
    186. p->next = temp->next;
    187. if(NULL != temp->next)
    188. {
    189. temp->next->prev = p;
    190. }
    191. free(temp);
    192. head->text.len--;
    193. return;
    194. }

    三、头文件

    1. #ifndef __doublelink_H__
    2. #define __doublelink_H__
    3. typedef int dataType;
    4. union msg{
    5. dataType data;
    6. int len;
    7. };
    8. typedef struct node{
    9. union msg text;
    10. struct node* next;
    11. struct node* prev;
    12. }doublelinklist;
    13. doublelinklist* creatr_doublelinklist();
    14. void showdoublelinklist(doublelinklist*head);
    15. void insertHead_doublelinklist(doublelinklist*head,dataType num);
    16. void insertEnd_doublelinklist(doublelinklist*head,dataType num);
    17. void insertByindex_doublelinklist(doublelinklist*head,int index,dataType num);
    18. void delHead_doublelinklist(doublelinklist*head);
    19. void delEnd_doublelinklist(doublelinklist*head);
    20. void delByindex_doublelinklist(doublelinklist*head,int index);
    21. #endif

    四、运行结果

  • 相关阅读:
    [附源码]SSM计算机毕业设计大学生心理咨询网站JAVA
    PyTorch开发者福音, OpenVINO整合PyTorch实现推理加速!
    springboot中使用mybatis的类型转换器
    TypeScript 项目 Airbnb 语法风格 ESLint 配置
    扩增子分析全面升级!加量不加价,数据更多新玩法
    FineReport填报设计-填报设置-填报校验
    【LeetCode 每日一题】15. 三数之和
    python 如何处理图片 举例说明
    前端 Git 使用约定
    Unity - 手动创建 dithering tex 3d
  • 原文地址:https://blog.csdn.net/m0_67565143/article/details/136219109