当前位置:   article > 正文

数据结构---单链表实现

数据结构---单链表实现

单链表是什么

我的理解是“特殊的数组”,通过访问地址来连接起来

1怎么创建链表 ----通过结构体(成员有存入数据的data和指向下一个节点的地址的指针(结构体指针)next

初始架构---DataType 对应存入数据类型,此处的Node==struct node *

  1. //头文件
  2. #include<stdio.h>
  3. //宏定义
  4. #define DataType int
  5. //全局变量
  6. //结构体
  7. typedef struct node
  8. {
  9. DataType data;
  10. struct node* next;
  11. }node, * Node;
  12. int main()
  13. {
  14. return 0;
  15. }

2初始化链表+尾插+打印

  1. Node Init(Node phead)
  2. {
  3. phead = (Node)malloc(sizeof(node));
  4. if (phead == -1)
  5. return -1;
  6. phead->next = NULL;
  7. }
  8. int PushBack(Node phead, DataType num)
  9. {
  10. //创建新节点
  11. Node newnode = malloc(sizeof(node));
  12. newnode->next = NULL;
  13. newnode->data = num;
  14. //创建指针找到链表的尾,然后插入,
  15. Node p = NULL;
  16. //出来for循环就插入不用考虑链表为空因为for循环里面已经考虑判断了
  17. for (p = phead; p->next != NULL; p = p->next);
  18. p->next = newnode;
  19. return 0;
  20. }
  21. void show_list(Node phead)
  22. {
  23. Node cur = NULL;
  24. cur = phead;
  25. //判断是否为空
  26. if (phead == NULL)
  27. {
  28. printf("NULL\n");
  29. }
  30. //遍历头节点没有数据所有从头节点下一个数据开始打印
  31. for (cur = phead->next; cur != NULL; cur = cur->next)
  32. {
  33. printf("%d->", cur->data);
  34. }
  35. printf("NULL\n");
  36. }

现象:

  1. //头文件
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. //宏定义
  5. #define DataType int
  6. //全局变量
  7. //结构体
  8. typedef struct node
  9. {
  10. DataType data;
  11. struct node* next;
  12. }node, * Node;
  13. Node Init(Node phead)
  14. {
  15. phead = (Node)malloc(sizeof(node));
  16. if (phead == -1)
  17. return -1;
  18. phead->next = NULL;
  19. }
  20. int PushBack(Node phead, DataType num)
  21. {
  22. //创建新节点
  23. Node newnode = malloc(sizeof(node));
  24. newnode->next = NULL;
  25. newnode->data = num;
  26. //创建指针找到链表的尾,然后插入,
  27. Node p = NULL;
  28. //出来for循环就插入不用考虑链表为空因为for循环里面已经考虑判断了
  29. for (p = phead; p->next != NULL; p = p->next);
  30. p->next = newnode;
  31. return 0;
  32. }
  33. void show_list(Node phead)
  34. {
  35. Node cur = NULL;
  36. cur = phead;
  37. //判断是否为空
  38. if (phead == NULL)
  39. {
  40. printf("NULL\n");
  41. }
  42. //遍历头节点没有数据所有从头节点下一个数据开始打印
  43. for (cur = phead->next; cur != NULL; cur = cur->next)
  44. {
  45. printf("%d->", cur->data);
  46. }
  47. printf("NULL\n");
  48. }
  49. int main()
  50. {
  51. Node phead = NULL;
  52. phead = Init(phead);
  53. PushBack(phead, 1);
  54. PushBack(phead, 1);
  55. PushBack(phead, 1);
  56. PushBack(phead, 1);
  57. show_list(phead);
  58. return 0;
  59. }

3添加删除和修改的功能 

删除时利用两个指针一个找一个删,再指向

修改就是在查找的基础上再加一个if判断

  1. #define _CRT_SECURE_NO_WARNINGS
  2. //头文件
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. //宏定义
  6. #define DataType int
  7. //全局变量
  8. //结构体
  9. typedef struct node
  10. {
  11. DataType data;
  12. struct node* next;
  13. }node, * Node;
  14. Node Init(Node phead)
  15. {
  16. phead = (Node)malloc(sizeof(node));
  17. if (phead == NULL)
  18. return -1;
  19. phead->next = NULL;
  20. }
  21. int PushBack(Node phead, DataType num)
  22. {
  23. //创建新节点
  24. Node newnode = malloc(sizeof(node));
  25. newnode->next = NULL;
  26. newnode->data = num;
  27. //创建指针找到链表的尾,然后插入,
  28. Node p = NULL;
  29. //出来for循环就插入不用考虑链表为空因为for循环里面已经考虑判断了
  30. for (p = phead; p->next != NULL; p = p->next);
  31. p->next = newnode;
  32. return 0;
  33. }
  34. void show_list(Node phead)
  35. {
  36. Node cur = NULL;
  37. cur = phead;
  38. //判断是否为空
  39. if (phead == NULL)
  40. {
  41. printf("NULL\n");
  42. }
  43. //遍历头节点没有数据所有从头节点下一个数据开始打印
  44. for (cur = phead->next; cur != NULL; cur = cur->next)
  45. {
  46. printf("%d->", cur->data);
  47. }
  48. printf("NULL\n");
  49. }
  50. int Delect(Node phead,DataType num)
  51. {
  52. if (phead == NULL)
  53. {
  54. printf("peahd is NULL\n");
  55. return -1;
  56. }
  57. Node cur1= NULL;
  58. Node cur2 = NULL;
  59. for (cur1 = phead, cur2 = phead->next; cur1->next != NULL; cur1 = cur2, cur2 = cur2->next)
  60. {
  61. if (cur2->data == num)
  62. {
  63. cur1->next = cur2->next;
  64. free(cur2);
  65. return 0;
  66. }
  67. }
  68. printf("no find num\n");
  69. return -1;
  70. }
  71. int Change(Node phead, int num1, int num2)
  72. {
  73. if (phead == NULL)
  74. {
  75. printf("peahd is NULL\n");
  76. return -1;
  77. }
  78. Node cur = NULL;
  79. for (cur = phead->next;cur != NULL; cur = cur->next)
  80. {
  81. if (cur->data == num1)
  82. {
  83. cur->data = num2;
  84. return 0;
  85. }
  86. }
  87. printf("no find num1\n");
  88. return -1;
  89. }
  90. int main()
  91. {
  92. Node phead = NULL;
  93. phead = Init(phead);
  94. PushBack(phead, 1);
  95. PushBack(phead, 1);
  96. PushBack(phead, 1);
  97. PushBack(phead, 1);
  98. Change(phead, 1, 2);
  99. show_list(phead);
  100. Delect(phead, 1);
  101. show_list(phead);
  102. Delect(phead, 1);
  103. Delect(phead, 1);
  104. Delect(phead, 1);
  105. show_list(phead);
  106. Delect(phead, 1);
  107. show_list(phead);
  108. Change(phead, 1, 2);
  109. show_list(phead);
  110. return 0;
  111. }

4释放---释放后链表为空,不会显示后面功能

  1. void Releas(Node phead)
  2. {
  3. Node cur1 = NULL;
  4. Node cur2 = NULL;
  5. for (cur1 = cur2 = phead; cur1->next != NULL; cur1 = cur2)
  6. {
  7. cur2 = cur1->next;
  8. free(cur1);
  9. }
  10. }
  11. int main()
  12. {
  13. Node phead = NULL;
  14. phead = Init(phead);
  15. PushBack(phead, 1);
  16. PushBack(phead, 1);
  17. PushBack(phead, 1);
  18. PushBack(phead, 1);
  19. Change(phead, 1, 2);
  20. Releas(phead);
  21. show_list(phead);
  22. Delect(phead, 1);
  23. show_list(phead);
  24. Delect(phead, 1);
  25. Delect(phead, 1);
  26. Delect(phead, 1);
  27. show_list(phead);
  28. Delect(phead, 1);
  29. show_list(phead);
  30. Change(phead, 1, 2);
  31. show_list(phead);
  32. return 0;
  33. }

5最终代码---链表实现方法有很多掌握自己熟练的解决问题才是关键

  1. #define _CRT_SECURE_NO_WARNINGS
  2. //头文件
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. //宏定义
  6. #define DataType int
  7. //全局变量
  8. //结构体
  9. typedef struct node
  10. {
  11. DataType data;
  12. struct node* next;
  13. }node, * Node;
  14. Node Init(Node phead)
  15. {
  16. phead = (Node)malloc(sizeof(node));
  17. if (phead == NULL)
  18. return -1;
  19. phead->next = NULL;
  20. }
  21. int PushBack(Node phead, DataType num)
  22. {
  23. //创建新节点
  24. Node newnode = malloc(sizeof(node));
  25. newnode->next = NULL;
  26. newnode->data = num;
  27. //创建指针找到链表的尾,然后插入,
  28. Node p = NULL;
  29. //出来for循环就插入不用考虑链表为空因为for循环里面已经考虑判断了
  30. for (p = phead; p->next != NULL; p = p->next);
  31. p->next = newnode;
  32. return 0;
  33. }
  34. void show_list(Node phead)
  35. {
  36. Node cur = NULL;
  37. cur = phead;
  38. //判断是否为空
  39. if (phead == NULL)
  40. {
  41. printf("NULL\n");
  42. }
  43. //遍历头节点没有数据所有从头节点下一个数据开始打印
  44. for (cur = phead->next; cur != NULL; cur = cur->next)
  45. {
  46. printf("%d->", cur->data);
  47. }
  48. printf("NULL\n");
  49. }
  50. int Delect(Node phead,DataType num)
  51. {
  52. if (phead == NULL)
  53. {
  54. printf("peahd is NULL\n");
  55. return -1;
  56. }
  57. Node cur1= NULL;
  58. Node cur2 = NULL;
  59. for (cur1 = phead, cur2 = phead->next; cur1->next != NULL; cur1 = cur2, cur2 = cur2->next)
  60. {
  61. if (cur2->data == num)
  62. {
  63. cur1->next = cur2->next;
  64. free(cur2);
  65. return 0;
  66. }
  67. }
  68. printf("no find num\n");
  69. return -1;
  70. }
  71. int Change(Node phead, int num1, int num2)
  72. {
  73. if (phead == NULL)
  74. {
  75. printf("peahd is NULL\n");
  76. return -1;
  77. }
  78. Node cur = NULL;
  79. for (cur = phead->next;cur != NULL; cur = cur->next)
  80. {
  81. if (cur->data == num1)
  82. {
  83. cur->data = num2;
  84. return 0;
  85. }
  86. }
  87. printf("no find num1\n");
  88. return -1;
  89. }
  90. void Releas(Node phead)
  91. {
  92. Node cur1 = NULL;
  93. Node cur2 = NULL;
  94. for (cur1 = cur2 = phead; cur1->next != NULL; cur1 = cur2)
  95. {
  96. cur2 = cur1->next;
  97. free(cur1);
  98. }
  99. }
  100. int main()
  101. {
  102. Node phead = NULL;
  103. phead = Init(phead);
  104. PushBack(phead, 1);
  105. PushBack(phead, 1);
  106. PushBack(phead, 1);
  107. PushBack(phead, 1);
  108. Change(phead, 1, 2);
  109. Releas(phead);
  110. show_list(phead);
  111. Delect(phead, 1);
  112. show_list(phead);
  113. Delect(phead, 1);
  114. Delect(phead, 1);
  115. Delect(phead, 1);
  116. show_list(phead);
  117. Delect(phead, 1);
  118. show_list(phead);
  119. Change(phead, 1, 2);
  120. show_list(phead);
  121. return 0;
  122. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/1003858
推荐阅读
相关标签
  

闽ICP备14008679号