当前位置:   article > 正文

内存泄漏检测、单向链表的操作

内存泄漏检测、单向链表的操作

我要成为嵌入式高手之3月19日数据结构第二天!!
————————————————————————————

valgrind内存测试工具

虚拟机上网、在虚拟机上下载软件,参考笔记:

我要成为嵌入式高手之2月3日Linux高编第一天!!-CSDN博客

上图表示申请了10个空间,释放了10个空间,没有内存泄漏

上图申请了11个空间,释放了10个空间,存在内存泄漏(leaked memory) 

 单向链表逆序

  1. #include "head.h"
  2. LINK_LIST *CreateLink()
  3. {
  4. LINK_LIST *plist = malloc(sizeof(LINK_LIST));
  5. if (NULL == plist)
  6. {
  7. perror("fail to malloc");
  8. return NULL;
  9. }
  10. plist->phead = NULL;
  11. plist->clen = 0;
  12. return plist;
  13. }
  14. int LinkSearch(LINK_LIST *plist)
  15. {
  16. LINK_NODE *ptmp = plist->phead;
  17. while (ptmp != NULL)
  18. {
  19. printf("%d\n", ptmp->data);
  20. ptmp = ptmp->pnext;
  21. }
  22. printf("len = %d\n", plist->clen);
  23. return 0;
  24. }
  25. int PushTailLink(LINK_LIST *plist, DATA_TYPE data)
  26. {
  27. LINK_NODE *ptmp = plist->phead;
  28. LINK_NODE *pnode = malloc(sizeof(LINK_NODE));
  29. if (NULL == pnode)
  30. {
  31. perror("fail to malloc pnode");
  32. return -1;
  33. }
  34. pnode->data = data;
  35. pnode->pnext = NULL;
  36. if (plist->phead == NULL)
  37. {
  38. plist->phead = pnode;
  39. }
  40. else
  41. {
  42. while (ptmp->pnext != NULL)
  43. {
  44. ptmp = ptmp->pnext;
  45. }
  46. ptmp->pnext = pnode;
  47. plist->clen++;
  48. }
  49. return 0;
  50. }
  51. int ReverseLink(LINK_LIST *plist)
  52. {
  53. if (plist->phead == NULL)
  54. {
  55. return -1;
  56. }
  57. LINK_NODE *ptmp = plist->phead;
  58. plist->phead = NULL;
  59. LINK_NODE *pinsert = NULL;
  60. while (ptmp != NULL)
  61. {
  62. pinsert = ptmp;
  63. ptmp = ptmp->pnext;
  64. pinsert->pnext = plist->phead;
  65. plist->phead = pinsert;
  66. }
  67. return 0;
  68. }
  69. int main()
  70. {
  71. LINK_LIST *plist = NULL;
  72. plist = CreateLink();
  73. if (NULL == plist)
  74. {
  75. return -1;
  76. }
  77. PushTailLink(plist, 1);
  78. PushTailLink(plist, 2);
  79. PushTailLink(plist, 3);
  80. PushTailLink(plist, 4);
  81. PushTailLink(plist, 5);
  82. LinkSearch(plist);
  83. ReverseLink(plist);
  84. printf("=========================\n");
  85. LinkSearch(plist);
  86. return 0;
  87. }

找单向链表的中间节点

 

  1. LINK_NODE *SearchMidNode(LINK_LIST *plist)
  2. {
  3. LINK_NODE *pfast = plist->phead;
  4. LINK_NODE *pslow = plist->phead;
  5. while (pfast != NULL)
  6. {
  7. pfast = pfast->pnext;
  8. if (NULL == pfast)
  9. {
  10. break;
  11. }
  12. pfast = pfast->pnext;
  13. pslow = pslow->pnext;
  14. }
  15. return pslow;
  16. }

寻找链表倒数第K个结点

  1. LINK_NODE *SearchReNoKNode(LINK_LIST *plist, int k)
  2. {
  3. LINK_NODE *pfast = plist->phead;
  4. LINK_NODE *pslow = pfast;
  5. int i = 0;
  6. for (i = 0; i < k; i++)
  7. {
  8. if (NULL == pfast)
  9. {
  10. return NULL;
  11. }
  12. pfast = pfast->pnext;
  13. }
  14. while (pfast != NULL)
  15. {
  16. pfast = pfast->pnext;
  17. pslow = pslow->pnext;
  18. }
  19. return pslow;
  20. }

删除指定数据的结点

  1. int DeleteKNode(LINK_LIST *plist, int k)
  2. {
  3. LINK_NODE *pfree = NULL;
  4. LINK_NODE *ptmp = NULL;
  5. pfree = plist->phead;
  6. ptmp = pfree;
  7. while (pfree != NULL)
  8. {
  9. if (pfree->data == k)
  10. {
  11. if (plist->phead == pfree)
  12. {
  13. plist->phead = pfree->pnext;
  14. free(pfree);
  15. plist->clen--;
  16. break;
  17. }
  18. else
  19. {
  20. ptmp->pnext = pfree->pnext;
  21. free(pfree);
  22. plist->clen--;
  23. break;
  24. }
  25. }
  26. else
  27. {
  28. ptmp = pfree;
  29. pfree = pfree->pnext;
  30. }
  31. }
  32. return 0;
  33. }

 链表的插入排序

  1. int InsertSort(LINK_LIST *plist)
  2. {
  3. LINK_NODE *ptmp = NULL;
  4. LINK_NODE *pinsert = NULL;
  5. LINK_NODE *p = NULL;
  6. if (p == NULL || p->pnext == NULL)
  7. {
  8. return -1;
  9. }
  10. ptmp = plist->phead->pnext;
  11. plist->phead->pnext = NULL;
  12. pinsert = ptmp;
  13. while (ptmp != NULL)
  14. {
  15. pinsert = ptmp;
  16. ptmp = ptmp->pnext;
  17. if (pinsert->data <= plist->phead->data)
  18. {
  19. pinsert->pnext = plist->phead;
  20. plist->phead = pinsert;
  21. }
  22. else
  23. {
  24. LINK_NODE *p = plist->phead;
  25. while (p->pnext != NULL && p->pnext->data < pinsert->data)
  26. {
  27. p = p->pnext;
  28. }
  29. pinsert->pnext = p->pnext;
  30. p->pnext = pinsert;
  31. }
  32. }
  33. return 0;
  34. }

 

                                                                                                                                                                                      

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