• C++:将栈类和队列类都实现成模板类


    1.栈的源代码:

    1. #include
    2. using namespace std;
    3. template<typename T>
    4. class Stack
    5. {
    6. private:
    7. T* arr;
    8. int top;
    9. int max;
    10. public:
    11. // 无参构造
    12. Stack() : arr(nullptr), top(-1), max(0) {}
    13. // 有参构造
    14. Stack(int size)
    15. {
    16. if (size > 0)
    17. {
    18. max = size;
    19. arr = new T[max];
    20. top = -1;
    21. cout << "栈容量设置成功" << endl;
    22. }
    23. else
    24. {
    25. cout << "输入有误,栈的容量必须大于0" << endl;
    26. }
    27. }
    28. // 拷贝构造
    29. Stack(const Stack& other) : top(other.max), max(other.max)
    30. {
    31. if (this != &other)
    32. {
    33. arr = new T[max];
    34. for (int i = 0; i < top + 1; i++)
    35. {
    36. arr[i] = other.arr[i];
    37. }
    38. }
    39. }
    40. // 析构函数
    41. ~Stack()
    42. {
    43. delete[] arr;
    44. }
    45. // 入栈
    46. void push(T value)
    47. {
    48. if (!isFull())
    49. {
    50. arr[++top] = value;
    51. cout << value << " 入栈成功" << endl;
    52. }
    53. else
    54. {
    55. cout << "栈满,无法入栈" << endl;
    56. }
    57. }
    58. // 出栈
    59. T pop()
    60. {
    61. if (!isEmpty())
    62. {
    63. return arr[top--];
    64. }
    65. else
    66. {
    67. cout << "栈空,无法出栈" << endl;
    68. return T(); // 返回默认值
    69. }
    70. }
    71. // 清空栈
    72. void clear()
    73. {
    74. top = -1;
    75. }
    76. // 判空
    77. bool isEmpty()
    78. {
    79. return top == -1;
    80. }
    81. // 判满
    82. bool isFull()
    83. {
    84. return top == max - 1;
    85. }
    86. // 获取栈顶元素
    87. T getTop()
    88. {
    89. if (!isEmpty())
    90. {
    91. return arr[top];
    92. }
    93. else
    94. {
    95. cout << "栈空,无法获取栈顶元素" << endl;
    96. return T();
    97. }
    98. }
    99. // 求栈的大小
    100. int getSize()
    101. {
    102. return top + 1;
    103. }
    104. };
    105. int main()
    106. {
    107. Stack stringStack(10);
    108. stringStack.push("hello");
    109. stringStack.push("world");
    110. cout << "栈的大小为: " << stringStack.getSize() << endl;
    111. cout << "栈顶元素为: " << stringStack.getTop() << endl;
    112. cout << stringStack.pop() << " 出栈成功" << endl;
    113. cout << "出栈一次后栈的大小为: " << stringStack.getSize() << endl;
    114. return 0;
    115. }

    2.队列源代码

    1. #include
    2. #define MAX 5
    3. using namespace std;
    4. template <typename T>
    5. class Queue{
    6. public:
    7. //构造
    8. Queue();
    9. //析构
    10. ~Queue();
    11. //拷贝构造
    12. Queue(const Queue &other);
    13. //入队
    14. int push(T e);
    15. //出队
    16. int pop();
    17. //清空队
    18. void clear();
    19. //判空
    20. bool empty();
    21. //判满
    22. bool full();
    23. //求大小
    24. int size();
    25. private:
    26. int front;
    27. int tail;
    28. T *data;
    29. };
    30. //构造函数
    31. template <typename T>
    32. Queue::Queue():front(0),tail(0),data(new T[MAX])
    33. {
    34. cout<<"构造函数"<
    35. }
    36. //析构函数
    37. template <typename T>
    38. Queue::~Queue(){
    39. delete []data;
    40. cout<<"析构"<
    41. }
    42. //拷贝构造函数
    43. template <typename T>
    44. Queue::Queue(const Queue &other):front(other.front),
    45. tail(other.tail),
    46. data(new T[MAX]){
    47. std::copy(other.data,other.data+MAX,data);
    48. cout<<"拷贝构造"<
    49. }
    50. //入队
    51. template <typename T>
    52. int Queue::push(T e){
    53. if(Queue::full()){
    54. cout<<"队满;"<
    55. return 0;
    56. }
    57. data[tail]=e;
    58. cout<"入队"<
    59. tail=(tail+1)%MAX;
    60. return 1;
    61. }
    62. //出队
    63. template <typename T>
    64. int Queue::pop(){
    65. if(Queue::empty()){
    66. cout<<"队空"<
    67. return 0;
    68. }
    69. cout<"出队"<
    70. front=(front+1)%MAX;
    71. return 1;
    72. }
    73. //清空队列
    74. template <typename T>
    75. void Queue::clear(){
    76. cout<<"清空队"<
    77. while(!Queue::empty()){
    78. Queue::pop();
    79. }
    80. }
    81. //判空
    82. template <typename T>
    83. bool Queue::empty(){
    84. return front==tail;
    85. }
    86. //判满
    87. template <typename T>
    88. bool Queue::full(){
    89. return (tail+1)%MAX==front;
    90. }
    91. //求队列大小
    92. template <typename T>
    93. int Queue::size(){
    94. cout<<this<<"队列大小:";
    95. return (tail-front+MAX)%MAX;
    96. }
    97. int main()
    98. {
    99. Queue<int> que;
    100. que.push(1);
    101. que.push(2);
    102. que.push(3);
    103. cout<size()<
    104. Queue<int> q2=que;
    105. cout<size()<
    106. Queue<int> q3=q2;
    107. cout<size()<
    108. q2.clear();
    109. q2.pop();
    110. cout<size()<
    111. cout<size()<
    112. return 0;
    113. }

    3.思维导图

  • 相关阅读:
    yolov5训练可视化指标的含义
    设计一个32*8的sram,长宽比接近1:1,求具体讲一下原理
    前端发送请求,显示超时取消
    typescript56-泛型接口
    安全性归约(构造 1)
    Qt::绘制框架-选择节点-QGraphicsScene-items
    【Embedded System】裸机接口开发
    Java网络开发(Session)—— 从http请求 到 cookie 到 session & 用 session控制 删改数据的权限
    让学前端不再害怕英语单词(三)
    计算机毕业设计java基于springboot的在线动漫平台
  • 原文地址:https://blog.csdn.net/2301_78047404/article/details/132868533