c++简单链表实现

以下为LinkList.h文件代码

  1. #ifndef LINKLIST_H_INCLUDED    
  2. #define LINKLIST_H_INCLUDED    
  3. typedef struct LNode    
  4. {    
  5. int data;    
  6. struct LNode *next;    
  7. } LNode, *pLinkList;    
  8. class LinkList    
  9. {    
  10. private:    
  11. pLinkList pList;    
  12. int listLength;    
  13. public:    
  14. LinkList();    
  15. ~LinkList();    
  16. bool InitList();    
  17. bool DestroyList();    
  18. bool ClearList();    
  19. bool IsEmpty();    
  20. int GetLength();    
  21. bool GetNode(int position, LNode** node);    
  22. int LocateElem(int elem);    
  23. bool SetNodeData(int position, int newData);    
  24. bool GetNodeData(int position, int &data);    
  25. bool InsertNode(int beforeWhich, int data);    
  26. /*bool DeleteNode(int position);*/    
  27. bool DeleteNode(int data);    
  28. };    
  29. #endif // LINKLIST_H_INCLUDED    
  30.   
  31. 以下为LinkList.cpp文件代码    
  32. #include <iostream>    
  33. #include "LinkList.h"    
  34. LinkList::LinkList()    
  35. {    
  36. pList = NULL;    
  37. listLength = 0;    
  38. InitList();    
  39. }    
  40. LinkList::~LinkList()    
  41. {    
  42. if (!DestroyList())    
  43. {    
  44.   DestroyList();    
  45. }    
  46. }    
  47. //初始化,分配一个头节点。    
  48. bool LinkList::InitList()    
  49. {    
  50. if (!(pList = new LNode))    
  51. {    
  52.   return false;    
  53. }    
  54. pList->next = NULL;    
  55. return true;    
  56. }    
  57. //销毁链表。    
  58. bool LinkList::DestroyList()    
  59. {    
  60. if (!ClearList()) {    
  61.   return false;    
  62. }    
  63. delete pList;    
  64. return true;    
  65. }    
  66. //判断链表是否为空。若为空,返回true,否则返回false。    
  67. bool LinkList::IsEmpty() {    
  68. if (pList->next == NULL)    
  69. {    
  70.   return true;    
  71. }    
  72. return false;    
  73. }    
  74. //返回链表的中当前节点数。    
  75. int LinkList::GetLength()    
  76. {    
  77. return listLength;    
  78. }    
  79. //将链表清空,释放当前所有节点。    
  80. bool LinkList::ClearList()    
  81. {    
  82. if (pList == NULL)    
  83. {    
  84.   return false;    
  85. }    
  86. LNode *pTemp = NULL;    
  87. while (pList->next != NULL)    
  88. {    
  89.   pTemp = pList->next;    
  90.   pList->next = pTemp->next;    
  91.   delete pTemp;    
  92. }    
  93. listLength = 0;    
  94. return true;    
  95. }    
  96. //将position指定的节点内的数据设置为newData。    
  97. //第一个有效节点的position为1。    
  98. bool LinkList::SetNodeData(int position, int newData)    
  99. {    
  100. LNode *pTemp = NULL;    
  101. if (!(GetNode(position, &pTemp)))    
  102. {    
  103.   return false;    
  104. }    
  105. pTemp->data = newData;    
  106. return true;    
  107. }    
  108. //得到指定位置节点的数据。    
  109. //节点索引从1到listLength。    
  110. bool LinkList::GetNodeData(int position, int &data)    
  111. {    
  112. LNode *pTemp = NULL;    
  113. if (!(GetNode(position, &pTemp)))    
  114. {    
  115.   return false;    
  116. }    
  117. data = pTemp->data;    
  118. return true;    
  119. }    
  120. //在链表中插入一个节点。    
  121. //插入的位置由beforeWhich指定,新节点插入在beforeWhich之前。    
  122. //beforeWhich的取值在1到ListLength+1之间。    
  123. bool LinkList::InsertNode(int beforeWhich, int data)    
  124. {    
  125. LNode *pTemp = NULL;    
  126. if (beforeWhich < 1 || beforeWhich > (listLength + 1))    
  127. {    
  128.   return false;    
  129. }    
  130. if (!(GetNode(beforeWhich - 1, &pTemp)))    
  131. {    
  132.   return false;    
  133. }    
  134. LNode *newNode = new LNode;    
  135. newNode->data = data;    
  136. newNode->next = pTemp->next;    
  137. pTemp->next = newNode;    
  138. listLength++;    
  139. return true;    
  140. }    
  141. //删除一个指定的节点。    
  142. //节点位置由position指定。    
  143. //positon的值从1到listLength。    
  144. //若链表为空或指定的节点不存在则返回false。    
  145. /*   
  146. bool LinkList::DeleteNode(int position)   
  147.  
  148. if (position < 1 || position > listLength)   
  149.  
  150.   return false;   
  151.  
  152. LNode *pTemp = NULL;   
  153. if (!(GetNode(position - 1, &pTemp)))   
  154.  
  155.   return false;   
  156.  
  157. LNode *pDel = NULL;   
  158. pDel = pTemp->next;   
  159. pTemp->next = pDel->next;   
  160. delete pDel;   
  161. listLength--;   
  162. return true;   
  163. }*/    
  164. //通过指定data删除节点    
  165. bool LinkList::DeleteNode(int data)    
  166. {    
  167.     LNode *pTemp = NULL;    
  168. pTemp = pList;    
  169. while (1)    
  170. {    
  171.   if (pTemp->next->data == data)    
  172.   {    
  173.    break;    
  174.   }    
  175.   pTemp = pTemp->next;    
  176. }    
  177. LNode *pDel = NULL;    
  178. pDel = pTemp->next;    
  179. pTemp->next = pDel->next;    
  180. delete pDel;    
  181. listLength--;    
  182. return true;    
  183. }    
  184. //得到指定位置节点的指针。    
  185. bool LinkList::GetNode(int position, LNode **node)    
  186. {    
  187. LNode *pTemp = NULL;    
  188. int curPos = -1;    
  189. pTemp = pList;    
  190. while (pTemp != NULL)    
  191. {    
  192.   curPos++;    
  193.   if (curPos == position)    
  194.    break;    
  195.   pTemp = pTemp->next;    
  196. }    
  197. if (curPos != position)    
  198. {    
  199.   return false;    
  200. }    
  201. *node = pTemp;    
  202. return true;    
  203. }    
  204. //定位与指定数据相等的数据节点。    
  205. //如果在当前链表中已经存在该数据则返回该数据节点的索引号。    
  206. //若不存在这样的节点则返回0。    
  207. //节点索引从0开始到listLength。    
  208. int LinkList::LocateElem(int elem)    
  209. {    
  210. LNode *pTemp = NULL;    
  211. int curIndex = 1;    
  212. pTemp = pList->next;    
  213. while ((pTemp != NULL) && (pTemp->data != elem))    
  214. {    
  215.   pTemp = pTemp->next;    
  216.   curIndex++;    
  217. }    
  218. if (pTemp == NULL)    
  219. {    
  220.   return 0;    
  221. }    
  222. return curIndex;    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值