• 0531作业 链表


    结果 

     

    整体代码

     主要实现

    1. /*
    2. *实现
    3. * */
    4. #include "./linklist.h"
    5. linklist* create_linklist(datatype param){
    6. linklist* node=(linklist*)malloc(sizeof(linklist));
    7. if(NULL==node){
    8. puts("节点创建失败");
    9. }
    10. node->param=param;
    11. node->pnext=NULL;
    12. puts("结点创建成功");
    13. return node;
    14. }
    15. //头插
    16. void insert_head_linklist(linklist* head,datatype param){
    17. linklist* newNode=(linklist*)create_linklist(param);
    18. newNode->pnext=head->pnext;
    19. head->pnext=newNode;
    20. head->param++;
    21. }
    22. //尾插
    23. void insert_last_linklist(linklist* head,datatype param){
    24. linklist* newNode=(linklist*)create_linklist(param);
    25. linklist* p=head;
    26. while(NULL!=p->pnext){
    27. p=p->pnext;
    28. }
    29. p->pnext=newNode;
    30. newNode->pnext=NULL;
    31. head->param++;
    32. }
    33. //遍历
    34. void foreach_linklist(linklist* head){
    35. linklist* p=head;
    36. puts("-------遍历-------");
    37. printf("链表总长度:");
    38. while(NULL!=p->pnext){
    39. printf("%d \n",p->param);
    40. p=p->pnext;
    41. }
    42. printf("%d \n",p->param);
    43. puts("-----------------");
    44. }
    45. //卸载堆空间
    46. void free_null(linklist** node){
    47. printf("正在卸载%d的堆空间\n",(*node)->param);
    48. free(*node);
    49. *node=NULL;
    50. puts("已卸载堆空间");
    51. }
    52. //尾删
    53. void delete_last_linklist(linklist* head){
    54. if(head == NULL || head->pnext == NULL){
    55. puts("链表为空或只有头结点");
    56. return;
    57. }
    58. linklist* p=head;
    59. while(NULL!=p->pnext->pnext){
    60. p=p->pnext;
    61. }
    62. free_null(&(p->pnext));
    63. head->param--;
    64. puts("尾删成功");
    65. }
    66. //头删
    67. void delete_head_linklist(linklist* head){
    68. if(head == NULL || head->pnext == NULL){
    69. puts("链表为空或只有头结点");
    70. return;
    71. }
    72. linklist* temp=head->pnext;
    73. head->pnext=head->pnext->pnext;
    74. free_null(&temp);
    75. head->param--;
    76. puts("头删成功");
    77. }
    78. void insert_index_linklist(linklist *head, int index, datatype num)
    79. {
    80. if(index > head->param+1 || index <= 0)
    81. {
    82. return;
    83. }
    84. linklist *p = head;
    85. for(int i=0; i<index-1; i++)
    86. {
    87. p = p->pnext;
    88. }
    89. linklist *temp = (linklist*)malloc(sizeof(linklist));
    90. temp->param = num;
    91. temp->pnext = p->pnext;
    92. p->pnext = temp;
    93. head->param++;
    94. puts("按位插入成功");
    95. }
    96. //指定位置删除节点
    97. void delete_index_linklist(linklist *head, int index)
    98. {
    99. if(NULL == head->pnext || NULL == head || index > head->param+1 || index <= 0)
    100. {
    101. return;
    102. }
    103. linklist *p = head;
    104. for(int i=0; i<index-1; i++)
    105. {
    106. p = p->pnext;
    107. }
    108. linklist *temp = (linklist*)malloc(sizeof(linklist));
    109. temp = p->pnext;
    110. p->pnext = temp->pnext;
    111. free_null(&temp);
    112. head->param--;
    113. puts("按位删除成功");
    114. }
  • 相关阅读:
    第08章 索引的创建与设计原则【2.索引及调优篇】【MySQL高级】
    Vue脚手架中的axios的引入,网络事件代理配置
    JVM-类加载子系统
    xss的DOMPurify过滤框架:一个循环问题以及两个循环问题
    Hive的Sort by Order by Distribute by Cluster by
    【html5期末大作业】基于HTML+CSS+JavaScript管理系统页面模板
    玩转KubeEdge
    SpringBoot SpringBoot 开发实用篇 5 整合第三方技术 5.14 SpringBoot 整合 quartz
    谷粒学院16万字笔记+1600张配图(四)——前端技术
    安装配置SPDK
  • 原文地址:https://blog.csdn.net/2401_84984673/article/details/139397463