【数据结构】双链表(c++)

前端之家收集整理的这篇文章主要介绍了【数据结构】双链表(c++)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

文件



  1. #pragma once
  2.  
  3. #include <iostream>
  4. #include <assert.h>
  5. using namespace std;
  6.  
  7. template<class Type>
  8. class List;
  9.  
  10. // 结点类
  11. template<class Type>
  12. class NodeList
  13. {
  14. friend class List<Type>;
  15. public:
  16. NodeList();
  17. NodeList(Type d,NodeList<Type> *n = NULL,NodeList<Type> *m = NULL);
  18. private:
  19. Type data;
  20. NodeList<Type>* next;
  21. NodeList<Type>* prio;
  22. };
  23.  
  24. // 链表类
  25. template<class Type>
  26. class List
  27. {
  28. public:
  29. List();
  30. ~List();
  31. public:
  32. void show_list();
  33. void tail_insert(Type const &x);
  34. void head_insert(Type const &x);
  35. void sort();
  36. void head_delete();
  37. void tail_delete();
  38. void val_insert(Type const &x);
  39. NodeList<Type>* find(Type const &x);
  40. void val_delete(Type const &x);
  41. Type length();
  42. void reverse();
  43. void clear();
  44. void destroy();
  45. void quit_system(Type &x);
  46. protected:
  47. NodeList<Type>* BuyNode(Type x = Type())
  48. {
  49. NodeList<Type>* p = new NodeList<Type>(x);
  50. assert(p != NULL);
  51. return p;
  52. }
  53. private:
  54. NodeList<Type>* first;
  55. NodeList<Type>* last;
  56. };
  57.  
  58. // 结点类的构造函数
  59. template<class Type>
  60. NodeList<Type>::NodeList()
  61. {
  62. data = Type();
  63. next = NULL;
  64. prio = NULL;
  65. }
  66.  
  67. template<class Type>
  68. NodeList<Type>::NodeList(Type d,NodeList<Type> *m = NULL)
  69. {
  70. data = d;
  71. next = n;
  72. prio = m;
  73. }
  74.  
  75. // 链表类的构造函数
  76. template<class Type>
  77. List<Type>::List()
  78. {
  79. first = last = BuyNode();
  80. first->prio = NULL;
  81. }
  82.  
  83. // 链表类的析构函数
  84. template<class Type>
  85. List<Type>::~List()
  86. {
  87. destroy();
  88. }
  89.  
  90. // 显示
  91. template<class Type>
  92. void List<Type>::show_list()
  93. {
  94. NodeList<Type> *p = first->next;
  95. while (p != NULL)
  96. {
  97. cout << p->data << "->";
  98. p = p->next;
  99. }
  100. cout << "NULL" << endl;
  101. }
  102.  
  103. // 尾插
  104. template<class Type>
  105. void List<Type>::tail_insert(Type const& x)
  106. {
  107. NodeList<Type> *p = BuyNode(x);
  108. last->next = p;
  109. p->prio = last;
  110. last = p;
  111. first->data++;
  112. }
  113.  
  114. // 头插
  115. template<class Type>
  116. void List<Type>::head_insert(Type const &x)
  117. {
  118. NodeList<Type> *p = BuyNode(x);
  119. if (first->data == 0)
  120. {
  121. first->next = p;
  122. p->prio = first;
  123. last = p;
  124. }
  125. else
  126. {
  127. p->next = first->next;
  128. first->next = p;
  129. p->prio = first;
  130. p->next->prio = p;
  131. last = p;
  132. for (int i = 0; i < first->data; ++i)
  133. {
  134. last = last->next;
  135. }
  136. }
  137. first->data++;
  138. }
  139.  
  140. // 排序
  141. template<class Type>
  142. void List<Type>::sort()
  143. {
  144. if (first->data == 0)
  145. {
  146. return;
  147. }
  148. if (first->data == 1)
  149. {
  150. show_list();
  151. }
  152. NodeList<Type> *t = first->next;
  153. NodeList<Type> *p = t->next;
  154. NodeList<Type> *q = NULL;
  155. t->next = NULL;
  156. p->prio = NULL;
  157. last = t;
  158. first->data = 1;
  159. while (p != NULL)
  160. {
  161. val_insert(p->data);
  162. q = p;
  163. p = p->next;
  164. delete q;
  165. }
  166. }
  167.  
  168. // 头删
  169. template<class Type>
  170. void List<Type>::head_delete()
  171. {
  172. if (first->data == 0)
  173. {
  174. cout << "the list is empty,cannot delete!" << endl;
  175. return;
  176. }
  177. NodeList<Type> *p = first->next;
  178. if (first->data == 1)
  179. {
  180. last = first;
  181. first->next = NULL;
  182. }
  183. else
  184. {
  185. first->next = p->next;
  186. p->next->prio = first;
  187. }
  188. delete p;
  189. first->data--;
  190. }
  191.  
  192. // 尾删
  193. template<class Type>
  194. void List<Type>::tail_delete()
  195. {
  196. if (first->data == 0)
  197. {
  198. cout << "the list is empty,cannot delete!" << endl;
  199. return;
  200. }
  201. NodeList<Type> *s = last;
  202. if (first->data == 1)
  203. {
  204. first->next = NULL;
  205. first->prio = NULL;
  206. last = first;
  207. }
  208. else
  209. {
  210. last = last->prio;
  211. last->next = NULL;
  212. first->prio = NULL;
  213. }
  214. delete s;
  215. first->data--;
  216. }
  217.  
  218. // 按值插入
  219. template<class Type>
  220. void List<Type>::val_insert(Type const &x)
  221. {
  222. if (first->data == 0)
  223. {
  224. tail_insert(x);
  225. return;
  226. }
  227. NodeList<Type> *p = first->next;
  228. NodeList<Type> *s = BuyNode(x);
  229. while (p->data < x && p->next != NULL)
  230. {
  231. p = p->next;
  232. }
  233. if (p->data >= x)
  234. {
  235. p->prio->next = s;
  236. s->prio = p->prio;
  237. s->next = p;
  238. p->prio = s;
  239. first->data++;
  240. }
  241. else
  242. {
  243. tail_insert(x);
  244. }
  245. }
  246.  
  247. // 查找
  248. template<class Type>
  249. NodeList<Type>* List<Type>::find(Type const &x)
  250. {
  251. NodeList<Type> *s = first->next;
  252. while (s != NULL)
  253. {
  254. if (s->data == x)
  255. {
  256. return s;
  257. }
  258. s = s->next;
  259. }
  260. return NULL;
  261. }
  262.  
  263. // 按值删除
  264. template<class Type>
  265. void List<Type>::val_delete(Type const &x)
  266. {
  267. if (first->data == 0)
  268. {
  269. cout << "the list is empty,cannot delete!" << endl;
  270. return;
  271. }
  272. NodeList<Type> *s = find(x);
  273. if (s == NULL)
  274. {
  275. cout << "the number is not exist,can not delete!" << endl;
  276. return;
  277. }
  278. if (s->next != NULL)
  279. {
  280. s->prio->next = s->next;
  281. s->next->prio = s->prio;
  282. first->data--;
  283. delete s;
  284. }
  285. else
  286. {
  287. tail_delete();
  288. }
  289. }
  290.  
  291. // 求长度
  292. template<class Type>
  293. Type List<Type>::length()
  294. {
  295. return first->data;
  296. }
  297.  
  298. // 反转
  299. template<class Type>
  300. void List<Type>::reverse()
  301. {
  302. if (first->data == 0)
  303. {
  304. return;
  305. }
  306. if (first->data == 1)
  307. {
  308. show_list();
  309. }
  310. NodeList<Type> *t = first->next;
  311. NodeList<Type> *p = t->next;
  312. NodeList<Type> *q = NULL;
  313. t->next = NULL;
  314. p->prio = NULL;
  315. last = t;
  316. first->data = 1;
  317. while (p != NULL)
  318. {
  319. head_insert(p->data);
  320. q = p;
  321. p = p->next;
  322. delete q;
  323. }
  324. }
  325.  
  326. // 清空
  327. template<class Type>
  328. void List<Type>::clear()
  329. {
  330. if (first->data == 0)
  331. {
  332. return;
  333. }
  334. while (first->next != NULL)
  335. {
  336. tail_delete();
  337. }
  338. }
  339.  
  340. // 摧毁
  341. template<class Type>
  342. void List<Type>::destroy()
  343. {
  344. clear();
  345. delete first;
  346. }
  347.  
  348. // 退出系统
  349. template<class Type>
  350. void List<Type>::quit_system(Type &x)
  351. {
  352. x = 0;
  353. }



函数

  1. // 用c++实现单链表
  2.  
  3. #include "DList.h"
  4.  
  5. int main()
  6. {
  7. List<int> mylist;
  8. int input = 1;
  9. int insert;
  10. while (input)
  11. {
  12. cout << "*********************************************************************" << endl;
  13. cout << "* [1] show_list [2] tail_insert *" << endl;
  14. cout << "* [3] head_insert [4] sort *" << endl;
  15. cout << "* [5] head_delete [6] tail_delete *" << endl;
  16. cout << "* [7] val_insert           [8] find *" << endl;
  17. cout << "* [9] val_delete [10] length *" << endl;
  18. cout << "* [11] reverse [12] clear *" << endl;
  19. cout << "* [13] destroy [14] quit_system *" << endl;
  20. cout << "*********************************************************************" << endl;
  21. cout << "please choose:";
  22. cin >> input;
  23. switch (input)
  24. {
  25. case 1:
  26. mylist.show_list();
  27. break;
  28. case 2:
  29. cout << "please enter the number want to insert(-1 end):";
  30. while (cin >> insert,insert != -1)
  31. {
  32. mylist.tail_insert(insert);
  33. }
  34. break;
  35. case 3:
  36. cout << "please enter the number want to insert(-1 end):";
  37. while (cin >> insert,insert != -1)
  38. {
  39. mylist.head_insert(insert);
  40. }
  41. break;
  42. case 4:
  43. mylist.sort();
  44. break;
  45. case 5:
  46. mylist.head_delete();
  47. break;
  48. case 6:
  49. mylist.tail_delete();
  50. break;
  51. case 7:
  52. cout << "please enter the number want to insert:";
  53. cin >> insert;
  54. mylist.val_insert(insert);
  55. break;
  56. case 8:
  57. cout << "please enter the number want to find:";
  58. cin >> insert;
  59. if (mylist.find(insert) == NULL)
  60. {
  61. cout << "the number is not exist!" << endl;
  62. }
  63. else
  64. {
  65. cout << "the number at the " << mylist.find(insert) << endl;
  66. }
  67. break;
  68. case 9:
  69. cout << "please enter the number want to delete:";
  70. cin >> insert;
  71. mylist.val_delete(insert);
  72. break;
  73. case 10:
  74. cout << "the length is" << " " << mylist.length() << endl;
  75. break;
  76. case 11:
  77. mylist.reverse();
  78. break;
  79. case 12:
  80. mylist.clear();
  81. break;
  82. case 13:
  83. mylist.destroy();
  84. break;
  85. case 14:
  86. mylist.quit_system(input);
  87. break;
  88. default:
  89. break;
  90. }
  91. }
  92. return 0;
  93. }



清空:








头删:







头插:






反转:






排序:






尾删:






尾插:






按值删除






按值插入:




猜你在找的数据结构相关文章