【数据结构】用C++实现顺序表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等)

前端之家收集整理的这篇文章主要介绍了【数据结构】用C++实现顺序表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. //顺序表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等)
  2. //头文件
  3. #pragma once
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. template<class Type>
  8. class SeqList
  9. {
  10. public:
  11. SeqList(size_t sz = INIT_SIZE);
  12. ~SeqList();
  13. public:
  14. bool isfull() const
  15. {return size >= capacity; }
  16. bool isempty() const
  17. {return (size == 0); }
  18. public:
  19. void show_list();
  20. void push_front(const Type &x);
  21. void push_back(const Type &x);
  22. void insert_pos(int pos,const Type &x);
  23. int find(const Type &key);
  24. void pop_front();
  25. void pop_back();
  26. void sort();
  27. void insert_val(const Type &x);
  28. void delete_pos(int pos);
  29. void delete_val(const Type &x);
  30. int length();
  31. void clear();
  32. void destory();
  33. void reverse();
  34. void quit_system(int &a);
  35. private:
  36. enum{ INIT_SIZE = 8 };
  37. Type *base;
  38. size_t capacity;
  39. size_t size;
  40. };
  41.  
  42. template<class Type> //构造函数
  43. SeqList<Type>::SeqList(size_t sz)
  44. {
  45. capacity = sz > INIT_SIZE ? sz : INIT_SIZE;
  46. base = new Type[capacity];
  47. size = 0;
  48. }
  49.  
  50. template<class Type> //析构函数
  51. SeqList<Type>::~SeqList()
  52. {
  53. delete base;
  54. }
  55.  
  56. template<class Type> //显示
  57. void SeqList<Type>::show_list()
  58. {
  59. if (size == 0)
  60. {
  61. cout << "数据表为空。" << endl;
  62. return;
  63. }
  64. for (int i = 0; i<size; ++i)
  65. {
  66. cout << base[i] << " ";
  67. }
  68. cout << endl;
  69. }
  70. template<class Type> //头插
  71. void SeqList<Type>::push_front(const Type &x)
  72. {
  73. if (isfull())
  74. {
  75. cout << "顺序表已满,不能插入." << endl;
  76. return;
  77. }
  78. for (int i = size; i > 0; --i)
  79. {
  80. base[i] = base[i - 1];
  81. }
  82. base[0] = x;
  83. size++;
  84. }
  85.  
  86. template<class Type> //尾插
  87. void SeqList<Type>::push_back(const Type &x)
  88. {
  89. if (isfull())
  90. {
  91. cout << "顺序表已满,不能插入." << endl;
  92. return;
  93. }
  94. base[size] = x;
  95. size++;
  96. }
  97.  
  98. template<class Type> //按位置插
  99. void SeqList<Type>::insert_pos(int pos,const Type &x)
  100. {
  101. if (pos<0 || pos>size)
  102. {
  103. cout << "插入的位置不正确。" << endl;
  104. return;
  105. }
  106. if ( isfull() )
  107. {
  108. cout << "顺序表已满,不能插入." << endl;
  109. return;
  110. }
  111. for (int i = size; i > pos; --i)
  112. {
  113. base[i] = base[i - 1];
  114. }
  115. base[pos] = x;
  116. size++;
  117. }
  118.  
  119. template<class Type> //查找
  120. int SeqList<Type>::find(const Type &key)
  121. {
  122. for (int i = 0; i<size; ++i)
  123. {
  124. if (base[i] == key)
  125. return i;
  126. }
  127. return -1;
  128. }
  129.  
  130.  
  131. template<class Type> //头删
  132. void SeqList<Type>::pop_front()
  133. {
  134. if (isempty())
  135. {
  136. cout << "顺序表为空,不能删除。" << endl;
  137. return;
  138. }
  139. for (int i = 0; i < size; ++i)
  140. {
  141. base[i] = base[i + 1];
  142. }
  143. size--;
  144. }
  145.  
  146. template<class Type>
  147. void SeqList<Type>::pop_back() //尾删
  148. {
  149. if (isempty())
  150. {
  151. cout << "顺序表为空,不能删除。" << endl;
  152. return;
  153. }
  154. size--;
  155. }
  156.  
  157. template<class Type> //排序
  158. void SeqList<Type>::sort()
  159. {
  160. if (isempty())
  161. {
  162. return;
  163. }
  164. for (int i = 1; i < size; i++)
  165. {
  166. for (int j = 0; j < size - i; j++)
  167. {
  168. int temp;
  169. if (base[j]>base[j + 1])
  170. {
  171. temp = base[j];
  172. base[j] = base[j + 1];
  173. base[j + 1] = temp;
  174. }
  175. }
  176. }
  177. }
  178.  
  179. template<class Type> //按值插入
  180. void SeqList<Type>::insert_val(const Type &x)
  181. {
  182. push_back(x); //尾插
  183. sort(); //排序
  184. }
  185.  
  186. template<class Type> //按位置删除
  187. void SeqList<Type>::delete_pos(int pos)
  188. {
  189. if (isempty())
  190. {
  191. cout << "顺序表为空,不能删除。" << endl;
  192. return;
  193. }
  194. if (pos < 0 || pos >= size)
  195. {
  196. cout << "删除的位置不正确。" << endl;
  197. return;
  198. }
  199. for (int i = pos; i < size; ++i)
  200. {
  201. base[i] = base[i + 1];
  202. }
  203. size--;
  204. }
  205.  
  206. template<class Type> //按值删除
  207. void SeqList<Type>::delete_val(const Type &x)
  208. {
  209. if (isempty())
  210. {
  211. cout << "顺序表为空,不能删除。" << endl;
  212. return;
  213. }
  214. int pos=find(x);
  215. if (pos == -1)
  216. {
  217. cout << "未找到该数。" << endl;
  218. return;
  219. }
  220. delete_pos(pos);
  221. }
  222.  
  223. template<class Type> //求长度
  224. int SeqList<Type>::length()
  225. {
  226. return size;
  227. }
  228.  
  229. template<class Type> //清除表
  230. void SeqList<Type>::clear()
  231. {
  232. size = 0;
  233. }
  234.  
  235. template<class Type> //摧毁表
  236. void SeqList<Type>::destory()
  237. {
  238. base = NULL;
  239. }
  240.  
  241.  
  242. template<class Type> //逆序
  243. void SeqList<Type>::reverse()
  244. {
  245. for (int i = 0,j = size - 1; i < size / 2; i++,j--)
  246. {
  247. int temp = base[i];
  248. base[i] = base[j];
  249. base[j] = temp;
  250. }
  251. }
  252.  
  253. template<class Type> //退出
  254. void SeqList<Type>::quit_system(int &a)
  255. {
  256. a = 0;
  257. }
  258.  
  259.  
  260.  
  261. //主函数
  262. #include"SeqList.h"
  263.  
  264. void main()
  265. {
  266. SeqList<int> mylist;
  267. int select = 1;
  268. int Item;
  269. int pos;
  270. while (select)
  271. {
  272. cout << "************************************************************************" << endl;
  273. cout << "* [1] show_list [2] quit_system *" << endl;
  274. cout << "* [3] push_front [4] push_back *" << endl;
  275. cout << "* [5] pop_front [6] pop_back *" << endl;
  276. cout << "* [7] insert_pos [8] insert_val *" << endl;
  277. cout << "* [9] delete_pos [10] delete_val *" << endl;
  278. cout << "* [11] find [12] length *" << endl;
  279. cout << "* [13] clear [14] destory *" << endl;
  280. cout << "* [15] reverse(逆序) [16] sort(顺序) *" << endl;
  281. cout << "************************************************************************" << endl;
  282. cout << "请选择功能:";
  283. cin >> select;
  284. switch (select)
  285. {
  286. case 1:
  287. mylist.show_list();
  288. break;
  289. case 2:
  290. mylist.quit_system(select);
  291. break;
  292. case 3:
  293. cout << "请输入您要插入的数(以-1结束):";
  294. while (cin >> Item,Item != -1)
  295. {
  296. mylist.push_front(Item);
  297. }
  298. break;
  299. case 4:
  300. cout << "请输入您要插入的数(以-1结束):";
  301. while (cin >> Item,Item !=-1)
  302. {
  303. mylist.push_back(Item);
  304. }
  305. break;
  306. case 5:
  307. mylist.pop_front();
  308. break;
  309. case 6:
  310. mylist.pop_back();
  311. break;
  312. case 7:
  313. cout << "请输入您要插入的位置:";
  314. cin >> pos;
  315. cout << "请输入您要插入的值:";
  316. cin >> Item;
  317. mylist.insert_pos(pos,Item);
  318. break;
  319. case 8:
  320. cout << "请输入要插入的数:";
  321. cin >> Item;
  322. mylist.insert_val(Item);
  323. break;
  324. case 9:
  325. cout << "请输入要删的位置:";
  326. cin >> pos;
  327. mylist.delete_pos(pos);
  328. break;
  329. case 10:
  330. cout << "请输入要删的值:";
  331. cin >> Item;
  332. mylist.delete_val(Item);
  333. break;
  334. case 11:
  335. cout << "请输入要查找的数:";
  336. cin >> Item;
  337. pos = mylist.find(Item);
  338. if (pos != -1)
  339. {
  340. cout << "该数为第" << pos << "个数" << endl;
  341. }
  342. cout << "未找到该数。"<<endl;
  343. break;
  344. case 12:
  345. cout <<"该顺序表长度为:"<< mylist.length() << endl;
  346. break;
  347. case 13:
  348. mylist.clear();
  349. break;
  350. case 14:
  351. mylist.destory();
  352. break;
  353. case 15:
  354. mylist.reverse();
  355. break;
  356. case 16:
  357. mylist.sort();
  358. break;
  359. default:
  360. break;
  361. }
  362.  
  363. }
  364. }
  365.  

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