当前位置:   article > 正文

c++模板类实现线性表顺序存储_模板库线性表

模板库线性表

SeqList.h

  1. #pragma once
  2. template<typename T>
  3. class SeqList
  4. {
  5. public:
  6.     SeqList(void);
  7.     SeqList(int capacity);
  8.     ~SeqList(void);
  9.     int getLength();
  10.     int getCapacity();
  11.     int insert(T &t,int pos);
  12.     int get(int pos,T&);
  13.     int del(int pos,T&);
  14.     int clear();
  15. private:
  16.     int length;
  17.     int capacity;
  18.     T *pArray;//数组
  19. };
SeqList.cpp

  1. #include "SeqList.h"
  2. template<typename T>
  3. SeqList<T>::SeqList(void)
  4. {
  5. }
  6. template<typename T>
  7. SeqList<T>::SeqList(int capacity)
  8. {
  9. //T *pArray;//数组
  10. this->pArray = new T[capacity];
  11. this->capacity = capacity;
  12. this->length = 0;
  13. }
  14. template<typename T>
  15. SeqList<T>::~SeqList(void)
  16. {
  17. delete[] this->pArray;
  18. this->pArray = NULL;
  19. this->length = 0;
  20. this->capacity = 0;
  21. }
  22. template<typename T>
  23. int SeqList<T>::getLength()
  24. {
  25. return this->length;
  26. }
  27. template<typename T>
  28. int SeqList<T>::getCapacity()
  29. {
  30. return this->capacity;
  31. }
  32. template<typename T>
  33. int SeqList<T>::insert(T &t,int pos)
  34. {
  35. int i;
  36. if (pos<0)
  37. {
  38. return -1;
  39. }
  40. //元素后移
  41. for (i = this->length; i>pos;i--)
  42. {
  43. this->pArray[i] = this->pArray[i-1];
  44. }
  45. //在pos位置插入元素
  46. this->pArray[i] = t;//stl元素的保存 时通过赋值的机制实现的 深拷贝浅拷贝
  47. this->length++;
  48. return 0;
  49. }
  50. template<typename T>
  51. int SeqList<T>::get(int pos,T&t)
  52. {
  53. if (pos<0)
  54. {
  55. return -1;
  56. }
  57. t = this->pArray[pos];
  58. return 0;
  59. }
  60. template<typename T>
  61. int SeqList<T>::del(int pos,T&t)
  62. {
  63. t = this->pArray[pos];//保存要删除的节点元素值
  64. //元素前移
  65. for (int i = pos ;i <=this->length;i++)
  66. {
  67. this->pArray[i] = this->pArray[i+1];
  68. }
  69. this->length--;
  70. return 0;
  71. }
  72. template<typename T>
  73. int SeqList<T>::clear()
  74. {
  75.     this->length = 0;
  76.     return 0;
  77. }

test.cpp

  1. #include <iostream>
  2. //#include "SeqList.h"//模板类两次编译 需要包含cpp
  3. #include "SeqList.cpp"
  4. using namespace std;
  5. struct Teacher
  6. {
  7. char name[20];
  8. int age;
  9. };
  10. void display()
  11. {
  12. Teacher t1,t2,t3;
  13. Teacher tmp;
  14. SeqList<Teacher> list(10);
  15. t1.age = 10;
  16. t2.age = 20;
  17. t3.age = 30;
  18. list.insert(t1,0);
  19. list.insert(t2,0);
  20. list.insert(t3,0);
  21. for (int i =0;i<list.getLength();i++)
  22. {
  23. list.get(i,tmp);
  24. cout<<tmp.age<<endl;
  25. }
  26. //链表的删除
  27. while(list.getLength()>0)
  28. {
  29. list.del(0,tmp);
  30. cout<<tmp.age<<endl;
  31. }
  32. }
  33. void display_p()
  34. {
  35. Teacher *p1,*p2,*p3;
  36. Teacher t1,t2,t3;
  37. Teacher *tmp;
  38. SeqList<Teacher*> list(10);
  39. p1 = &t1;
  40. p2 = &t2;
  41. p3 = &t3;
  42. t1.age = 10;
  43. t2.age = 20;
  44. t3.age = 30;
  45. list.insert(p1,0);
  46. list.insert(p2,0);
  47. list.insert(p3,0);
  48. for (int i =0;i<list.getLength();i++)
  49. {
  50. list.get(i,tmp);
  51. cout<<tmp->age<<endl;
  52. }
  53. //链表的删除
  54. while(list.getLength()>0)
  55. {
  56. list.del(0,tmp);
  57. cout<<tmp->age<<endl;
  58. }
  59. }
  60. int main()
  61. {
  62. display_p();
  63. system("pause");
  64. return 0;
  65. }



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

闽ICP备14008679号