当前位置:   article > 正文

单链表C语言实现

单链表C语言实现

SList.h文件

  1. #pragma once
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<assert.h>
  5. typedef int SLTDataType;
  6. typedef struct SListNode {
  7. SLTDataType data;
  8. struct SListNode* Next;
  9. }SLTNode;
  10. void SLTPrint(SLTNode* phead);
  11. void SLTPushBack(SLTNode** pphead, SLTDataType x);
  12. void SLTPushFront(SLTNode** pphead, SLTDataType x);
  13. void SLTPopBack(SLTNode** pphead);
  14. void SLTPopFront(SLTNode** pphead);
  15. SLTNode* SLTFind(SLTNode* phead,SLTDataType x);
  16. void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);
  17. void SLTInsertAfter(SLTNode* pos, SLTDataType x);
  18. void SLTErase(SLTNode** pphead,SLTNode*pos);
  19. void SLTEraseAfter(SLTNode** pphead);
  20. void SListDesTroy(SLTNode** pphead);

SList.c文件

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "SList.h"
  3. void SLTPrint(SLTNode* phead)
  4. {
  5. SLTNode * pcur = phead;
  6. while (pcur)
  7. {
  8. printf("%d->", pcur->data);
  9. pcur = pcur->Next;
  10. }
  11. printf("NULL");
  12. }
  13. SLTNode* SLTBuyNode(SLTDataType x)
  14. {
  15. SLTNode* NewNode = (SLTNode*)malloc(sizeof(SLTNode));
  16. if (NewNode == NULL)
  17. {
  18. perror("malloc fail!");
  19. exit(1);
  20. }
  21. NewNode->data = x;
  22. NewNode->Next = NULL;
  23. return NewNode;
  24. }
  25. void SLTPushBack(SLTNode** pphead, SLTDataType x)
  26. {
  27. assert(pphead);
  28. SLTNode* NewNode = SLTBuyNode(x);
  29. //空链表与非空
  30. if (*pphead == NULL)
  31. {
  32. *pphead = NewNode;
  33. }
  34. else
  35. {
  36. //find尾巴
  37. SLTNode* ptail = *pphead;
  38. while (ptail->Next != NULL)
  39. {
  40. ptail = ptail->Next;
  41. }
  42. //此时指向尾
  43. ptail->Next = NewNode;
  44. }
  45. }
  46. void SLTPushFront(SLTNode** pphead, SLTDataType x)
  47. {
  48. assert(pphead);
  49. SLTNode* NewNode = SLTBuyNode(x);
  50. NewNode->Next = *pphead;
  51. *pphead = NewNode;
  52. }
  53. void SLTPopBack(SLTNode** pphead)
  54. {
  55. assert(pphead && *pphead);
  56. if ((*pphead)->Next == NULL)// -> 高于 *
  57. {
  58. free(*pphead);
  59. *pphead = NULL;
  60. }
  61. else
  62. {
  63. SLTNode* prev = *pphead;
  64. SLTNode* ptail = *pphead;
  65. while (ptail->Next)
  66. {
  67. prev = ptail;
  68. ptail = ptail->Next;
  69. }
  70. free(ptail);
  71. ptail = NULL;
  72. prev->Next = NULL;
  73. }
  74. }
  75. void SLTPopFront(SLTNode** pphead)
  76. {
  77. assert(pphead && *pphead);
  78. SLTNode* next = (*pphead)->Next;
  79. free(*pphead);
  80. *pphead = next;
  81. }
  82. SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
  83. {
  84. SLTNode* pcur = phead;
  85. while (pcur)
  86. {
  87. if (pcur->data == x)
  88. {
  89. return pcur;
  90. }
  91. pcur = pcur->Next;
  92. }
  93. return NULL;
  94. }
  95. void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
  96. {
  97. assert(pphead && *pphead && pos);
  98. SLTNode* prev = *pphead;
  99. if (pos == *pphead)
  100. {
  101. SLTPushFront(pphead, x);
  102. }
  103. else
  104. {
  105. SLTNode* NewNode = SLTBuyNode(x);
  106. while (prev->Next != pos)
  107. {
  108. prev = prev->Next;
  109. }
  110. NewNode->Next = pos;
  111. prev->Next = NewNode;
  112. }
  113. }
  114. void SLTInsertAfter(SLTNode* pos, SLTDataType x)
  115. {
  116. assert(pos);
  117. SLTNode* NewNode = SLTBuyNode(x);
  118. NewNode->Next = pos->Next;
  119. pos->Next = NewNode;
  120. }
  121. void SLTErase(SLTNode** pphead, SLTNode* pos)
  122. {
  123. assert(*pphead && pphead && pos);
  124. //pos是头结点
  125. if (pos == *pphead)
  126. {
  127. SLTPopFront(pphead);
  128. }
  129. else {
  130. SLTNode* prev = *pphead;
  131. while (prev->Next != pos)
  132. {
  133. prev = prev->Next;
  134. }
  135. prev->Next = pos->Next;
  136. free(pos);
  137. pos = NULL;
  138. }
  139. }
  140. void SLTEraseAfter(SLTNode* pos)
  141. {
  142. assert(pos && pos->Next);
  143. SLTNode* del = pos->Next;
  144. pos->Next = del->Next;
  145. free(del);
  146. del = NULL;
  147. }
  148. void SListDesTroy(SLTNode** pphead)
  149. {
  150. assert(pphead && *pphead);
  151. SLTNode* pcur = *pphead;
  152. while (pcur)
  153. {
  154. SLTNode* next = pcur->Next;
  155. free(pcur);
  156. pcur = next;
  157. }
  158. *pphead = NULL;
  159. }

测试文件

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "SList.h"
  3. void SListTestA()
  4. {
  5. SLTNode* node1 = (SLTNode*)malloc(sizeof(SLTNode));
  6. node1->data = 1;
  7. SLTNode* node2 = (SLTNode*)malloc(sizeof(SLTNode));
  8. node2->data = 2;
  9. SLTNode* node3 = (SLTNode*)malloc(sizeof(SLTNode));
  10. node3->data = 3;
  11. SLTNode* node4 = (SLTNode*)malloc(sizeof(SLTNode));
  12. node4->data = 4;
  13. //连接
  14. node1->Next = node2;
  15. node2->Next = node3;
  16. node3->Next = node4;
  17. node4->Next = NULL;
  18. //调用打印
  19. SLTNode* plist = node1;
  20. SLTPrint(plist);
  21. }
  22. void SListTestB()
  23. {
  24. SLTNode* plist = NULL;
  25. SLTPushFront(&plist, 1);
  26. SLTPushFront(&plist, 2);
  27. SLTPushFront(&plist, 3);
  28. SLTPushFront(&plist, 4);
  29. SLTPopBack(&plist);
  30. SLTPrint(plist);
  31. printf("\n");
  32. SLTPopBack(&plist);
  33. SLTPrint(plist);
  34. printf("\n");
  35. SLTPopBack(&plist);
  36. SLTPrint(plist);
  37. printf("\n");
  38. SLTPopFront(&plist);
  39. SLTPrint(plist);
  40. while (1);
  41. }
  42. void SListTestC()
  43. {
  44. SLTNode* plist = NULL;
  45. SLTPushFront(&plist, 1);
  46. SLTPushFront(&plist, 2);
  47. SLTPushFront(&plist, 3);
  48. SLTPushFront(&plist, 4);
  49. SLTPrint(plist);
  50. printf("\n");
  51. SLTNode* find= SLTFind(plist, 4);
  52. //SLTInsert(&plist, find, 2);
  53. SLTInsertAfter(find,11);
  54. SLTPrint(plist);
  55. printf("\n");
  56. SLTEraseAfter(find);
  57. SLTPrint(plist);
  58. printf("\n");
  59. SListDesTroy(&plist);
  60. SLTPrint(plist);
  61. }
  62. int main()
  63. {
  64. //SListTestA();
  65. SListTestC();
  66. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/422783
推荐阅读
相关标签
  

闽ICP备14008679号