• C语言利用函数创建链表,修改链表(插入,删除,添加),指针函数的返回


    这段代码是一个简单的链表操作程序,包括创建节点、在链表末尾添加节点、在指定位置插入节点和删除指定位置的节点。以下是详细的注释:

    1. #include
    2. #include
    3. // 定义链表节点结构体
    4. struct listnode{
    5. int i; // 节点存储的整数值
    6. struct listnode *next; // 指向下一个节点的指针
    7. }Node;
    8. // 在链表末尾添加节点的函数声明
    9. struct listnode *list_append(struct listnode *node,int num);
    10. // 在指定位置插入节点的函数声明
    11. struct listnode *list_insert(struct listnode *node,int position,int num);
    12. // 删除指定位置节点的函数声明
    13. struct listnode *list_delete(struct listnode *node,int position);
    14. // 创建节点的函数声明
    15. struct listnode* createNode(int data);
    16. // 创建节点的函数实现
    17. struct listnode* createNode(int data) {
    18. struct listnode *newNode = (struct listnode *)malloc(sizeof(struct listnode));
    19. newNode->i = data;
    20. newNode->next = NULL;
    21. return newNode;
    22. }
    23. void main(){
    24. // 创建一个头节点并初始化为0
    25. struct listnode *head = createNode(0);
    26. // 创建一个临时头节点用于遍历
    27. struct listnode *head_t;
    28. // 分配内存给临时头节点
    29. head_t = (struct listnode*)malloc(sizeof(struct listnode));
    30. // 打印头节点的值
    31. printf("%d",head->i);
    32. // 将临时头节点指向头节点
    33. head_t = head;
    34. // 循环创建5个节点并添加到链表中
    35. for(int j = 0;j<5;j++){
    36. struct listnode* node = (struct listnode*)malloc(sizeof(struct listnode));
    37. node->i = j+1;
    38. node->next = NULL;
    39. head->next = node;
    40. head = node;
    41. }
    42. // 打印最后一个节点的值
    43. printf("%d",head->i);
    44. // 遍历链表并打印每个节点的值
    45. struct listnode* curr = head_t;
    46. while(curr){
    47. printf("%d",curr->i);
    48. curr=curr->next;
    49. }
    50. // 在链表末尾添加一个值为7的节点,并遍历打印链表
    51. struct listnode* curr2 = list_append(head_t,7);
    52. while(curr2){
    53. printf("%d",curr2->i);
    54. curr2=curr2->next;
    55. }
    56. // 在链表的第2个位置插入一个值为2的节点,并遍历打印链表
    57. struct listnode* curr3 = list_insert(head_t,2,2);
    58. while(curr3){
    59. printf("%d",curr3->i);
    60. curr3=curr3->next;
    61. }
    62. // 删除链表的第6个位置的节点,并遍历打印链表
    63. struct listnode* curr4 = list_delete(head_t,6);
    64. while(curr4){
    65. printf("%d",curr4->i);
    66. curr4=curr4->next;
    67. }
    68. }
    69. // 在链表末尾添加节点的函数实现
    70. struct listnode *list_append(struct listnode *node,int num){
    71. struct listnode *node_head;
    72. struct listnode *node_head2;
    73. node_head = (struct listnode *)malloc(sizeof(struct listnode));
    74. node_head2 = (struct listnode *)malloc(sizeof(struct listnode));
    75. node_head = node;
    76. node_head2 = node;
    77. while(node_head->next){
    78. node_head = node_head->next;
    79. }
    80. struct listnode *node_end;
    81. node_end = (struct listnode *)malloc(sizeof(struct listnode));
    82. node_end->i = num;
    83. node_end->next = NULL;
    84. node_head->next = node_end;
    85. return node;
    86. }
    87. // 在指定位置插入节点的函数实现
    88. struct listnode *list_insert(struct listnode *node,int position,int num){
    89. struct listnode *node_head;
    90. node_head = (struct listnode *)malloc(sizeof(struct listnode));
    91. int pos_1 = 0;
    92. node_head = node;
    93. while(node_head->next){
    94. node_head = node_head->next;
    95. pos_1++;
    96. if(pos_1==position-1){
    97. break;
    98. }
    99. }
    100. struct listnode *newnode;
    101. newnode = (struct listnode *)malloc(sizeof(struct listnode));
    102. newnode->i=num;
    103. newnode->next=node_head->next;
    104. node_head->next = newnode;
    105. return node;
    106. }
    107. // 删除指定位置节点的函数实现
    108. struct listnode *list_delete(struct listnode *node,int position){
    109. struct listnode *node_head;
    110. node_head = (struct listnode *)malloc(sizeof(struct listnode));
    111. int pos_1 = 0;
    112. node_head = node;
    113. while(node_head->next){
    114. node_head = node_head->next;
    115. pos_1++;
    116. if(pos_1==position){
    117. break;
    118. }
    119. }
    120. if(node->next->next)
    121. node_head->next = node_head->next->next;
    122. else{
    123. node_head->next = NULL;
    124. }
    125. return node;
    126. }

  • 相关阅读:
    mongodb6创建账号
    Matlab|含多微网租赁共享储能的配电网博弈优化调度
    移动端的助农电商系统-计算机毕业设计源码08655
    Swift基础语法 - 枚举
    线性表01- 数组与简易接口设计
    将 mixamo 中的动画重定向到 UE 的小白人中
    C++之结构体以及通讯录管理系统
    git修改提交信息commit
    2022人工智能数学基础1-2(许志钦
    【编程题】【Scratch三级】2021.06 躲球游戏
  • 原文地址:https://blog.csdn.net/SmartGridequation/article/details/136453015