1、C+实现循环链表 注意:循环链表含有头结点cpp view plaincopyprint?1. #include 2. using namespace std; 3. template 4. struct Node 5. 6. Type data; 7. Node *next; 8. ; 9. template 10. class Circlist 11. 12. protected: 13. int len;/链表中结点个数 14. Node* Head; /指向头结点 15. public: 16. Circlist();/默认构造函数 17. Circlist(const Circlis
2、t/拷贝构造函数 18. Circlist(); 19. void createListForward();/头插法 20. void createBackward();/尾插法 21. void initList();/生成头结点,尾部设置为 NULL 22. bool isEmptyList(); 23. void printList(); 24. int length(); 25. void destoryList(); 26. void getFirstData(Type 27. void search(const Type searchItem); 28. void insertFi
3、rst(const Type newItem); 29. void insertLast(const Type newItem); 30. void insertBefore(const int pos,const Type newItem); 31. void insertAfter(const int pos,const Type newItem); 32. void deleteNode(const Type deleteItem); 33. void deleteNode(const int pos,Type 34. void reverse(); 35. const Circlist
4、 36. ; 37. 38. template 39. Circlist:Circlist() /初始化时,只有一个头结点,有 head 指向 40. 41. Head = new Node; 42. Head-next = Head; 43. len =0; 44. 45. 46. template 47. Circlist:Circlist(const Circlist 50. Head-next = Head; 51. len =0; 52. Node* current = Head; 53. Node* otherListCurrent = otherList.Head-next;/o
5、therListCurrent 指向第一个元素 54. if (otherList.Head-next != otherList.Head)/被拷贝的表不是空表 55. 56. while(otherListCurrent-next!=otherList.Head)/拷贝的目标不为空 57. 58. Node* newNode = new Node; 59. newNode-data = otherListCurrent-data; 60. newNode-next = current-next; 61. current-next = newNode; 62. current=current-
6、next; 63. otherListCurrent = otherListCurrent-next; 64. len+; 65. 66. if (otherListCurrent-next=otherList.Head) 67. 68. Node* newNode = new Node; 69. newNode-data = otherListCurrent-data; 70. newNode-next = current-next; 71. current-next = newNode; 72. len+; 73. 74. 75. 76. 77. template 78. const Ci
7、rclist/current 总是指向要插的位置 81. Node* otherListCurrent=otherList.Head-next;/otherListCurrent 指向第一个元素 82. if (this!=/自己有结点,先销毁 87. 88. while(otherListCurrent!=otherList.Head) 89. 90. Node* newNode = new Node; 91. newNode-data = otherListCurrent-data; 92. newNode-next = current-next; 93. current-next = n
8、ewNode; 94. current=current-next; 95. otherListCurrent = otherListCurrent-next; 96. len+; 97. 98. 99. 100. return *this;/为了连续赋值 101. 102. 103. template 104. Circlist:Circlist() 105. 106. destoryList(); 107. 108. 109. template 110. void Circlist:createListForward()/头插法 111. 112. Node* newNode; 113. c
9、outlen; 115. for (int i=0;i; 118. coutnewNode-data; 120. newNode-next=Head-next; 121. Head-next = newNode; /每插入一个结点,都是要把它放在第一个结点的位置 122. 123. 124. 125. template 126. void Circlist:createBackward()/尾插法 127. 128. Node* current = Head;/current 指向头结点 129. Node* newNode; 130. coutlen; 132. for (int i=0;i
10、; 135. coutnewNode-data; 137. newNode-next = current-next; 138. current-next = newNode; 139. current=current-next; 140. 141. 142. 143. template 144. void Circlist:initList() /只剩下头结点,和指针设置 145. 146. destoryList();/所有结点都销毁,在重建头结点 147. Head = new Node; 148. Head-next = Head; 149. len =0; 150. 151. 152.
11、 template 153. bool Circlist:isEmptyList() 154. 155. if (Head-next=Head) 156. 157. return true; 158. 159. else 160. 161. return false; 162. 163. 164. 165. template 166. void Circlist:printList() 167. 168. Node*current=Head-next; 169. while (current!=Head) 170. 171. coutdatanext; 173. 174. 175. 176.
12、template 177. int Circlist:length() 178. 179. return len; 180. 181. 182. template 183. void Circlist:destoryList()/销毁包括头结点 184. 185. Node* current; 186. Node* temp; 187. if (Head!=NULL)/析构函数也要调这个函数,因此要判断头是不是为空,为空表示已经释放 188. 189. temp = Head; 190. current = Head-next; 191. while(current!=Head) 192. 1
13、93. delete temp; 194. temp = current; 195. current = current-next; 196. 197. delete temp; 198. len=0; 199. 200. 201. 202. template 203. void Circlist:getFirstData(Type 208. 209. else 210. 211. cout 216. void Circlist:search(Type searchItem) 217. 218. Node* current; 219. if (isEmptyList() 220. 221. c
14、outnext;/越过头结点,指向第一个元素 226. while (current!=Head 229. 230. if (current!=Head) 231. 232. cout 242. void Circlist:insertFirst(const Type newItem) 243. 244. Node *newNode = new Node; 245. newNode-data = newItem; 246. newNode-next = Head-next; 247. Head-next = newNode; 248. len+; 249. 250. 251. template
15、 252. void Circlist:insertLast(const Type newItem) 253. 254. Node *newNode = new Node; 255. newNode-data = newItem; 256. /寻找位置 257. Node* current = Head; 258. while (current-next != Head) 259. 260. current = current -next; 261. 262. /此时 current 指向结点的尾部,就是应该插入的位置 263. newNode-next = current-next; 264
16、. current-next = newNode; 265. len+; 266. 267. 268. template 269. void Circlist:insertBefore(const int pos,const Type newItem) 270. 271. int i=1; 272. Node* current = Head-next; 273. if (poslen) 274. 275. cout* newNode = new Node; 279. newNode-data = newItem; 280. if (1=pos) 281. 282. newNode-next =
17、 Head-next; 283. Head-next = newNode; 284. 285. else 286. 287. while(inext; 290. i+; 291. 292. newNode-next = current-next; 293. current-next = newNode; 294. 295. len+; 296. 297. 298. template 299. void Circlist:insertAfter(const int pos,const Type newItem) 300. 301. int i=1; 302. Node* current = He
18、ad-next;/current 指向第一个位置,和 i 配合,指向第 i 个结点 303. if (poslen) 304. 305. cout* newNode = new Node; 309. newNode-data = newItem; 310. while(inext; 313. i+; 314. 315. newNode-next = current-next; 316. current-next = newNode; 317. len+; 318. 319. 320. 321. 322. template 323. void Circlist:deleteNode(const
19、Type deleteItem) 324. 325. Node* current=Head - next; 326. Node* trailCurrent = Head;/指向 current 前面的结点 327. if (isEmptyList() 328. 329. coutdata!=deleteItem)/删除第一个元素后的元素 334. 335. trailCurrent = current; 336. current=current-next; 337. 338. if (current=NULL) 339. 340. coutnext = current-next; 345. d
20、elete current; 346. cout 353. void Circlist:deleteNode(const int pos,Type 356. Node* current = Head; 357. Node* temp; 358. if (poslen) 359. 360. coutnext; 367. i+; 368. 369. temp = current-next; 370. current-next = temp-next; 371. deleteItem = temp-data; 372. delete temp; 373. len-; 374. 375. 376. t
21、emplate 377. void Circlist:reverse() 378. 379. /先处理头结点 380. Node* current = Head-next; 381. Head-next=Head; 382. if (current=Head) 383. 384. cout* nextCurrent = current-next; 391. current-next = Head-next; 392. Head-next=current; 393. current = nextCurrent; 394. 395. 396. 397. 398. 399. void main() 400. 401. Circlist list1; 402. list1.createBackward(); 403. list1.reverse(); 404. list1.printList(); 405. system(“pause“); 406.