• 带头节点的单链表练习(写加注释花了5小时,已废)


    目录

    1.LinList.h

    2.test.c

    3.LinList.c


    练习:建立一个单链表,首先依次输入元素1,2,...,10,然后删除元素5,最后依次显示当前表中元素

    1.LinList.h

    引入头文件,#deifne定义,typedef定义,函数声明

    1. #pragma once
    2. #include
    3. #include
    4. #include
    5. #include
    6. typedef int DataType;
    7. typedef struct Node
    8. {
    9. DataType data;
    10. struct Node* next;
    11. }SLNode;
    12. void ListInitiate(SLNode** head);//初始化链表
    13. bool ListInsert(SLNode* head, int i, DataType x);//在第i个节点前插入x,i>=1
    14. DataType ListDelete(SLNode* head, int i, DataType* x);//删除第i个节点
    15. int ListLength(SLNode* head);//ListLength(head)当前元素个数
    16. bool ListGet(SLNode* head, int i, DataType* x);//取元素
    17. bool Destroy(SLNode** head);//撤销单链表

    2.test.c

    单链表的操作

    1. #include "LinList.h"
    2. //head->a0(头节点)->a1->...->ai->...->an
    3. int main()
    4. {
    5. SLNode* head;
    6. int i;
    7. DataType x, y;
    8. ListInitiate(&head);//初始化链表
    9. for (i = 1; i < 11; i++)
    10. {
    11. ListInsert(head, i, (DataType)i);//在第i个节点前插入i,i>=1
    12. }
    13. ListDelete(head, 4, &x);//删除第4个节点
    14. for (i = 1; i <= ListLength(head); i++)//ListLength(head)当前元素个数
    15. {
    16. ListGet(head, i, &y);//取元素
    17. printf("%d ", y);
    18. }
    19. Destroy(&head);//撤销单链表
    20. return 0;
    21. }

    3.LinList.c

    单链表函数具体实现

    1. #include "LinList.h"
    2. void ListInitiate(SLNode** head)//初始化链表,head的值改变了,所以要传head地址
    3. {
    4. *head = (SLNode*)malloc(sizeof(SLNode));
    5. assert(*head);
    6. (*head)->next = NULL;
    7. }
    8. bool ListInsert(SLNode* head, int i, DataType x)//在第i个节点前插入x,i>=1
    9. {
    10. SLNode* L = head;
    11. int j = 0;//当前L指向的第j个节点
    12. while (L->next != NULL&&j-1)//循环结束时L指向第i-1个节点
    13. {//当L->next = NULL时,L已是最后一个节点,
    14. //若j = i-1,在NULL前插入节点,若j != i-1,第i个节点不存在
    15. L = L->next;
    16. j++;
    17. }
    18. if (j != i - 1)
    19. {
    20. printf("插入元素的位置参数出错!\n");
    21. return false;
    22. }
    23. SLNode* s = (SLNode*)malloc(sizeof(SLNode));
    24. assert(s);
    25. s->data = x;
    26. s->next = L->next;
    27. L->next = s;
    28. return true;
    29. }
    30. DataType ListDelete(SLNode* head, int i, DataType* x)//删除第i个节点
    31. {
    32. SLNode* L = head;
    33. int j = 0;//当前L指向的第j个节点
    34. while (L->next != NULL&&L->next->next != NULL && j < i-1)//循环结束时L指向第i-1个节点
    35. {//L->next->next = NULL时,
    36. //若j = i-1,删除最后一个节点,若j != i-1,要删除的节点不存在
    37. L = L->next;
    38. j++;
    39. }
    40. if ((j != i - 1)||(L->next == NULL))//当空列表时,return false;
    41. {
    42. printf("要删除的第%d个节点不存在\n",i);
    43. return false;
    44. }
    45. *x = L->next->data;
    46. SLNode* s = L->next;
    47. L->next = L->next->next;
    48. free(s);
    49. s = NULL;
    50. return *x;
    51. }
    52. int ListLength(SLNode* head)//ListLength(head)当前元素个数
    53. {
    54. if (head == NULL)
    55. {
    56. return 0;
    57. }
    58. SLNode* p = head;//习惯,防止后面找不到头节点
    59. int count = 0;
    60. while (p->next != NULL)
    61. {
    62. p = p->next;
    63. count++;
    64. }
    65. return count;
    66. }
    67. bool ListGet(SLNode* head, int i, DataType* x)//取元素
    68. {
    69. SLNode* p = head;//习惯,防止后面找不到头节点
    70. if (i<1 || i>ListLength(head))
    71. {
    72. printf("取元素的位置参数错误!\n");
    73. return false;
    74. }
    75. while (i--)
    76. {
    77. p = p->next;
    78. }
    79. *x = p->data;
    80. return true;
    81. }
    82. bool Destroy(SLNode** head)//撤销单链表,head的值改变了,所以要传head地址
    83. {
    84. int i = ListLength(*head) + 1;//头节点也要撤销
    85. SLNode* p = *head;
    86. SLNode* p1 = NULL;
    87. while (i--)
    88. {
    89. p1 = p->next;
    90. free(p);
    91. p = p1;
    92. }
    93. *head = NULL;
    94. return true;
    95. }

  • 相关阅读:
    【Vue脚手架项目的结构】
    C/C++ vector模拟实现
    Git的下载与安装
    MogaFX外汇收益帮助First Gen实现10亿英镑的经常性净收入
    windows端口号占用
    tf.nest
    Wireshark - tshark支持iptables提供数据包
    掌握速卖通、亚马逊测评养号补单的条件与优势,提升店铺销量及排名
    【创建Unity工程】给美工的备忘录
    这几个截图文字识别软件可以自动识别文字
  • 原文地址:https://blog.csdn.net/2302_80310672/article/details/137937963