• C++(day6)


    思维导图

    将栈类和队列类都实现成模板类

    1. #include <iostream>
    2. using namespace std;
    3. #define MAX 5
    4. template <typename T>
    5. class Stack{
    6. public:
    7. //构造函数
    8. Stack();
    9. //析构函数
    10. ~Stack();
    11. //拷贝构造函数
    12. Stack(const Stack &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. T topdata();
    25. //求栈的大小
    26. int size();
    27. private:
    28. T *data;
    29. int top;
    30. };
    31. template <typename T>
    32. //构造函数
    33. Stack<T>::Stack():data(new T[MAX]),top(-1){
    34. cout<<"构造函数"<<endl;
    35. }
    36. template <typename T>
    37. //析构函数
    38. Stack<T>::~Stack(){
    39. delete []data; //释放指针空间
    40. cout<<"析构函数"<<endl;
    41. }
    42. template <typename T>
    43. //拷贝构造函数
    44. Stack<T>::Stack(const Stack &other):data(new T[MAX]),top(other.top){
    45. std::copy(other.data,other.data+MAX,data);
    46. cout<<"拷贝函数"<<endl;
    47. }
    48. //入栈
    49. template <typename T>
    50. int Stack<T>::push(T e){
    51. if(Stack<T>::full()){
    52. cout<<"栈满,无法入栈;"<<endl;
    53. return 0;
    54. }
    55. data[++top]=e;
    56. cout<<data[top]<<"已入栈"<<endl;
    57. return 1;
    58. }
    59. //出栈
    60. template <typename T>
    61. int Stack<T>::pop(){
    62. if(Stack<T>::empty()){
    63. cout<<"栈空,无法出栈;"<<endl;
    64. return 0;
    65. }
    66. cout<<data[top--]<<"已出栈"<<endl;
    67. return 1;
    68. }
    69. //清空栈
    70. template <typename T>
    71. void Stack<T>::clear(){
    72. cout<<"****清空栈****"<<endl;
    73. while(!Stack::empty()){
    74. Stack::pop();
    75. }
    76. }
    77. //判空
    78. template <typename T>
    79. bool Stack<T>::empty(){
    80. return -1 == top;
    81. }
    82. //判满
    83. template <typename T>
    84. bool Stack<T>::full(){
    85. return MAX-1 == top;
    86. }
    87. //获取栈顶元素
    88. template <typename T>
    89. T Stack<T>::topdata(){
    90. cout<<"栈顶元素为:";
    91. return data[top];
    92. }
    93. //求栈的大小
    94. template <typename T>
    95. int Stack<T>::size(){
    96. cout<<this<<"栈的大小为:";
    97. return top+1;
    98. }
    99. int main()
    100. {
    101. Stack<string> s;
    102. s.push("ww");
    103. s.push("qq");
    104. s.push("ee");
    105. s.push("rr");
    106. s.push("tt");
    107. s.push("yy");
    108. cout<<s.size()<<endl;
    109. Stack<string> s2=s;
    110. cout<<s2.size()<<endl;
    111. Stack<string> s3=s2;
    112. cout<<s3.size()<<endl;
    113. s3.pop();
    114. s3.pop();
    115. s3.pop();
    116. cout<<s3.topdata()<<endl;
    117. s3.clear();
    118. s3.pop();
    119. cout<<s3.size()<<endl;
    120. cout<<s.size()<<endl;
    121. return 0;
    122. }

    队列

    1. #include <iostream>
    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<T>::Queue():front(0),tail(0),data(new T[MAX])
    33. {
    34. cout<<"构造函数"<<endl;
    35. }
    36. //析构函数
    37. template <typename T>
    38. Queue<T>::~Queue(){
    39. delete []data;
    40. cout<<"析构函数"<<endl;
    41. }
    42. //拷贝构造函数
    43. template <typename T>
    44. Queue<T>::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<<"拷贝构造函数"<<endl;
    49. }
    50. //入队
    51. template <typename T>
    52. int Queue<T>::push(T e){
    53. if(Queue<T>::full()){
    54. cout<<"队满,无法入队;"<<endl;
    55. return 0;
    56. }
    57. data[tail]=e;
    58. cout<<e<<"已入队"<<endl;
    59. tail=(tail+1)%MAX;
    60. return 1;
    61. }
    62. //出队
    63. template <typename T>
    64. int Queue<T>::pop(){
    65. if(Queue<T>::empty()){
    66. cout<<"队空,无法出队;"<<endl;
    67. return 0;
    68. }
    69. cout<<data[front]<<"已出队"<<endl;
    70. front=(front+1)%MAX;
    71. return 1;
    72. }
    73. //清空队列
    74. template <typename T>
    75. void Queue<T>::clear(){
    76. cout<<"****清空队列****"<<endl;
    77. while(!Queue<T>::empty()){
    78. Queue<T>::pop();
    79. }
    80. }
    81. //判空
    82. template <typename T>
    83. bool Queue<T>::empty(){
    84. return front==tail;
    85. }
    86. //判满
    87. template <typename T>
    88. bool Queue<T>::full(){
    89. return (tail+1)%MAX==front;
    90. }
    91. //求队列大小
    92. template <typename T>
    93. int Queue<T>::size(){
    94. cout<<this<<"队列大小为:";
    95. return (tail-front+MAX)%MAX;
    96. }
    97. int main()
    98. {
    99. Queue<int> q;
    100. q.push(1);
    101. q.push(2);
    102. q.push(3);
    103. q.push(4);
    104. q.push(5);
    105. cout<<q.size()<<endl;
    106. Queue<int> q2=q;
    107. cout<<q2.size()<<endl;
    108. Queue<int> q3=q2;
    109. cout<<q2.size()<<endl;
    110. q2.clear();
    111. q2.pop();
    112. cout<<q2.size()<<endl;
    113. cout<<q.size()<<endl;
    114. return 0;
    115. }

  • 相关阅读:
    解决serviceaccount用户认证挂载密文token文件失败导致pod使用anonymous用户问题
    如何将变量用作typescript的类型注解
    MQTT协议详述
    第九章:最新版零基础学习 PYTHON 教程—Python 元组(第三节 -访问Python元组的前后元素)
    我赢助手之爆款内容创作:爆款内容的底层逻辑,检查下自己的内容是否符合呢?
    Kafka如何保证数据高可靠
    1028 List Sorting
    第6章——数据库的安全性
    【无标题】
    《SpringBoot篇》02.SpringBoot程序的打包与运行(jar包的运行原理)
  • 原文地址:https://blog.csdn.net/qq_53268516/article/details/132861664