当前位置:   article > 正文

C语言 单向链表_c 单向链表 append

c 单向链表 append
  1. /* 1.C语言单向链表
  2. * 2.链表的(增,删,查,遍历)
  3. * Author: Mr.Long
  4. * Date : 2015-12-1 15:24:26
  5. */
  6. #include<stdio.h>
  7. #include<stdlib.h>
  8. /* 结构定义 */
  9. struct node {
  10. int data;
  11. node *next;
  12. };
  13. /* 函数定义 */
  14. void printNode(struct node *head);
  15. struct node* appendNode(struct node *head,int);
  16. struct node* addFirstNode(struct node *head,int);
  17. struct node* findNode(struct node *head,int);
  18. int deletNode(struct node *head,int);
  19. int main() {
  20. /* 定义一个head指针,作为链表的头节点*/
  21. struct node *head = NULL;
  22. //申请内存空间
  23. head = (struct node *)malloc(sizeof(struct node));
  24. //初始化头结点数据
  25. head->data = 0;
  26. head->next = NULL;
  27. printf("*****************添加节点******************\r\n");
  28. /* 在链表尾部追加节点 */
  29. head = appendNode(head,5);
  30. head = appendNode(head,8);
  31. head = appendNode(head,4);
  32. head = appendNode(head,34);
  33. head = appendNode(head,6);
  34. head = appendNode(head,9);
  35. /* 添加为头节点 */
  36. head = addFirstNode(head,12);
  37. head = addFirstNode(head,61);
  38. printf("添加成功... \n");
  39. printf("*****************查找节点******************\r\n");
  40. /* 查找指定节点 */
  41. struct node *a;
  42. a = findNode(head,34);
  43. if(a != NULL) {
  44. printf("查找成功... \n");
  45. printf("data:%d \n",a->data);
  46. printf("next:0x%X \n",a->next);
  47. } else {
  48. printf("没找到该节点 \n");
  49. }
  50. printf("*****************删除节点******************\r\n");
  51. /* 删除指定节点 成功返回1 失败返回0 */
  52. int dest = 0;
  53. bool state = deletNode(head,dest);
  54. if(state)
  55. printf("删除[%d]成功... \n",dest);
  56. else
  57. printf("删除[%d]失败... \n",dest);
  58. printf("*****************所有节点******************\r\n");
  59. /* 遍历节点并输出 */
  60. printNode(head);
  61. return 0;
  62. }
  63. int deletNode(struct node *head,int dest) {
  64. struct node *p = head;
  65. struct node *lp;
  66. while(p->next) {
  67. if(p->data == dest) {
  68. // 删除操作:将上一个节点的next指向被删除节点的next
  69. lp->next = p->next;
  70. //释放被删除节点的内存空间
  71. free(p);
  72. return true;
  73. }
  74. //记录上一个节点
  75. lp = p;
  76. //移动指针
  77. p = p->next;
  78. }
  79. return false;
  80. }
  81. struct node* findNode(struct node *head,int dest) {
  82. struct node *p = head;
  83. struct node *pc = NULL;//临时变量
  84. while(p) { //当p不为NULL时
  85. if(p->data == dest) {//找到了指定节点
  86. pc = p;
  87. break;
  88. }
  89. p = p->next; //移动指针
  90. }
  91. return pc;
  92. }
  93. struct node* appendNode(struct node *head,int data) {
  94. if(data == NULL) return head;
  95. if(head == NULL) { //若头结点为空,则创建一个节点作为头结点
  96. printf("head is NULL \n");
  97. head = (struct node *)malloc(sizeof(struct node));
  98. head->data = data;
  99. head->next = NULL;
  100. return head;
  101. }
  102. /*易错点
  103. * 这里千万注意循环条件是p->next而不是p
  104. * 因为当执行到链表尾节点时p已经为NULL
  105. * 而当p为NULL时56行的空指针操作会导致程序崩溃
  106. */
  107. struct node *p = head;
  108. while(p->next) {
  109. p = p->next;
  110. }
  111. struct node *pNew;
  112. pNew = (struct node *)malloc(sizeof(struct node));
  113. pNew->data = data;
  114. pNew->next = NULL;
  115. p->next = pNew;
  116. /*易错点:
  117. * 如果这里返回 p 则链表只保留了最后两项数据
  118. * 原因在于47行的while循环移动了p的指针
  119. */
  120. return head;
  121. }
  122. struct node* addFirstNode(struct node *head,int data) {
  123. struct node *p;
  124. p = (struct node *)malloc(sizeof(struct node));
  125. p->data = data;
  126. p->next = head;
  127. return p;
  128. }
  129. void printNode(struct node *head) {
  130. struct node *p = head;
  131. while(p) { /* 当p不为NUll时 */
  132. printf("self=0x%X | data=%2d | next=0x%X\n",p,p->data,p->next);
  133. p = p->next;
  134. }
  135. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/945155
推荐阅读
相关标签
  

闽ICP备14008679号