当前位置:   article > 正文

【数据结构】无头单链表各个接口的实现_无头接口

无头接口

以下是无头单链表的增删查改等接口的实现

SList.h

  1. #pragma once
  2. #include <assert.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <malloc.h>
  6. typedef int SLTDataType;
  7. typedef struct SListNode
  8. {
  9. SLTDataType _data;
  10. struct SListNode* _next;
  11. }SListNode;
  12. typedef struct SList
  13. {
  14. SListNode* _head;
  15. SListNode* _tail;
  16. }SList;
  17. void SListInit(SList* plist);//初始化
  18. void SListDestory(SList* plist);//销毁
  19. void SListPushBack(SList* plist, SLTDataType x);//尾插
  20. void SListPopBack(SList* plist); //尾删
  21. void SListPushFront(SList* plist, SLTDataType x); //头插
  22. void SListPopFront(SList* plist);//头删
  23. SListNode* SListFind(SList* plist, SLTDataType x);
  24. void SListInsertAfter(SList* plist, SListNode* pos, SLTDataType x);//在pos后插入节点
  25. void SListEraseAfter( SListNode* pos);//删除pos后的节点
  26. void SListRemove(SList* plist,SLTDataType x);//删除一个值
  27. void SListPrint(SList* plist);
  28. void TestSList();

SList.c 

  1. #include "SList.h"
  2. void SListInit(SList* plist)//初始化
  3. {
  4. assert(plist);
  5. plist->_head=plist->_tail= NULL;
  6. }
  7. void SListDestory(SList* plist)//销毁
  8. {
  9. SListNode* cur;
  10. SListNode* next;
  11. assert(plist);
  12. cur = plist->_head;
  13. while (cur != NULL)
  14. {
  15. next = cur->_next;
  16. free(cur);
  17. cur = next;
  18. }
  19. plist->_head = plist->_tail = NULL;
  20. }
  21. SListNode* BuySListNode(SLTDataType x)//增加一个节点
  22. {
  23. SListNode* node = malloc(sizeof(SListNode));
  24. assert(node);
  25. node->_data = x;
  26. node->_next = NULL;
  27. return node;
  28. }
  29. void SListPushBack(SList* plist, SLTDataType x)//尾插
  30. {
  31. assert(plist);
  32. if (plist->_tail == NULL)
  33. {
  34. plist->_head = plist->_tail = BuySListNode(x);
  35. }
  36. else
  37. {
  38. SListNode* newnode = BuySListNode(x);
  39. plist->_tail->_next = newnode;
  40. plist->_tail = newnode;
  41. }
  42. }
  43. void SListPrint(SList* plist)
  44. {
  45. SListNode* cur = plist->_head;
  46. while (cur != plist->_tail->_next)
  47. {
  48. printf("%d ", cur->_data);
  49. cur = cur->_next;
  50. }
  51. printf("\n");
  52. }
  53. void SListPopBack(SList* plist)//尾删
  54. {
  55. SListNode*prev, *tail;
  56. assert(plist);
  57. prev = NULL;
  58. tail = plist->_head;
  59. if (tail->_next == NULL)
  60. {
  61. free(tail);
  62. plist->_head = NULL;
  63. }
  64. else
  65. {
  66. while (tail->_next)
  67. {
  68. prev = tail;
  69. tail = tail->_next;
  70. }
  71. free(tail);
  72. prev->_next = NULL;
  73. }
  74. }
  75. void SListPushFront(SList* plist, SLTDataType x)//头插
  76. {
  77. SListNode* newnode;
  78. assert(plist);
  79. newnode = BuySListNode(x);
  80. newnode->_next = plist->_head;
  81. plist->_head = newnode;
  82. }
  83. void SListPopFront(SList* plist)//头删
  84. {
  85. SListNode* next;
  86. assert(plist);
  87. next = plist->_head->_next;
  88. free(plist->_head);
  89. plist->_head = next;
  90. }
  91. SListNode* SListFind(SList* plist, SLTDataType x)
  92. {
  93. assert(plist);
  94. SListNode* cur = plist->_head;
  95. while (cur)
  96. {
  97. if (cur->_data == x)
  98. return cur;
  99. cur = cur->_next;
  100. }
  101. return NULL;
  102. }
  103. void SListInsertAfter(SList* plist, SListNode* pos, SLTDataType x)//在pos后面插入
  104. {
  105. SListNode* next, *newnode;
  106. assert(pos);
  107. next = pos->_next;
  108. newnode = BuySListNode(x);
  109. // pos newnode next
  110. pos->_next = newnode;
  111. newnode->_next = next;
  112. }
  113. void SListEraseAfter( SListNode* pos)//删除pos后面的
  114. {
  115. SListNode* next;
  116. assert(pos);
  117. if (pos->_next == NULL)
  118. return;
  119. next=pos->_next;
  120. pos->_next = next->_next;
  121. free(next);
  122. next = NULL;
  123. }
  124. void SListRemove(SList* plist, SLTDataType x)//删除一个值
  125. {
  126. assert(plist);
  127. if (plist->_head->_data == x)
  128. {
  129. SListPopFront(plist);
  130. return;
  131. }
  132. SListNode* prev=NULL;
  133. SListNode* cur = plist->_head;
  134. while (cur)
  135. {
  136. if (cur->_data == x)
  137. {
  138. prev->_next = cur->_next;
  139. free(cur);
  140. cur = NULL;
  141. break;
  142. }
  143. else
  144. {
  145. prev = cur;
  146. cur = cur->_next;
  147. }
  148. }
  149. }
  150. void TestSList()
  151. {
  152. SList s1;
  153. SListInit(&s1);
  154. SListPushBack(&s1,1);
  155. SListPushBack(&s1,3);
  156. SListPushBack(&s1,2);
  157. SListPushBack(&s1,4);
  158. SListPrint(&s1);
  159. //SListPopBack(&s1);
  160. //SListPrint(&s1);
  161. //SListPushFront(&s1, 6);
  162. //SListPushFront(&s1, 7);
  163. //SListPrint(&s1);
  164. //SListPopFront(&s1);
  165. //SListPrint(&s1);
  166. SListNode* pos = SListFind(&s1, 2);
  167. SListInsertAfter(&s1,pos,5);
  168. SListPrint(&s1);
  169. SListEraseAfter(pos);
  170. SListPrint(&s1);
  171. SListRemove(&s1, 3);
  172. SListPrint(&s1);
  173. //SListDestory(&s1);
  174. //SListPrint(&s1);
  175. }

 Test.c

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include "SList.h"
  3. int main()
  4. {
  5. TestSList();
  6. system("pause");
  7. return 0;
  8. }

 

 

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

闽ICP备14008679号