• day46:C++ day6 继承过程中的特殊成员函数、多重继承、虚继承、多态、泛型编程模板


    一、栈的模板类

    1. #include
    2. using namespace std;
    3. #define MAX 50
    4. template<typename T>
    5. class Stack
    6. {
    7. private:
    8. T *data;
    9. int top;
    10. public:
    11. Stack():data(new T[MAX]),top(-1)
    12. {
    13. cout<<"Stack::无参构造函数"<
    14. }
    15. ~Stack()
    16. {
    17. delete []data;
    18. cout<<"Stack::构析函数"<
    19. }
    20. Stack(const Stack &other)
    21. {
    22. if(data!=NULL)
    23. {
    24. delete[]data;
    25. }
    26. this->data=new T[MAX];
    27. memcpy(this->data,other.data,sizeof(T)*MAX);
    28. this->top=other.top;
    29. cout<<"Stack::拷贝构造函数"<
    30. }
    31. void stack_push(T &&val);
    32. void stack_pop();
    33. void stack_clear();
    34. bool stack_empty();
    35. bool stack_full();
    36. T stack_gettop();
    37. int stack_len();
    38. void stack_show();
    39. };
    40. template<typename T>
    41. void Stack::stack_push(T &&val)
    42. {
    43. this->top++;
    44. this->data[top]=val;
    45. }
    46. template<typename T>
    47. void Stack::stack_pop()
    48. {
    49. if(NULL==this->data || stack_empty())
    50. {
    51. cout<<"所给顺序栈不合法"<
    52. }
    53. cout<<this->data[top]<<"出栈成功"<
    54. this->top--;
    55. }
    56. template<typename T>
    57. void Stack::stack_clear()
    58. {
    59. // while(!stack_empty(S))
    60. // {
    61. // stack_pop(S);
    62. // }
    63. this->top=-1;
    64. }
    65. template<typename T>
    66. bool Stack::stack_empty()
    67. {
    68. return -1==this->top;
    69. }
    70. template<typename T>
    71. bool Stack::stack_full()
    72. {
    73. return MAX-1==this->top;
    74. }
    75. template<typename T>
    76. T Stack::stack_gettop()
    77. {
    78. return this->data[top];
    79. }
    80. template<typename T>
    81. int Stack::stack_len()
    82. {
    83. return this->top+1;
    84. }
    85. template<typename T>
    86. void Stack::stack_show()
    87. {
    88. for(int i=top;i>=0;i--)
    89. {
    90. cout<<this->data[i]<<" ";
    91. }
    92. cout<
    93. }
    94. int main()
    95. {
    96. Stack<int> S;
    97. cout<stack_empty()<
    98. S.stack_push(5);
    99. S.stack_push(8);
    100. S.stack_push(6);
    101. S.stack_push(7);
    102. S.stack_push(3);
    103. S.stack_push(9);
    104. cout<stack_full()<
    105. cout<<"栈顶元素为"<stack_gettop()<
    106. cout<<"栈的长度为"<stack_len()<
    107. S.stack_show();
    108. S.stack_clear();
    109. S.stack_push(520);
    110. S.stack_show();
    111. return 0;
    112. return 0;
    113. }

    二、队列的模板类

    1. #include
    2. #define MAX 50
    3. using namespace std;
    4. template<typename T>
    5. class Queue
    6. {
    7. private:
    8. T *data;
    9. int head;
    10. int tail;
    11. public:
    12. Queue():data(new T[MAX]),head(0),tail(0)
    13. {
    14. cout<<"queue::无参构造函数"<
    15. }
    16. ~Queue()
    17. {
    18. delete []data;
    19. cout<<"queue::析构函数"<
    20. }
    21. Queue(const Queue &other):
    22. data(new T(*other.data)),
    23. head(other.head),
    24. tail(other.tail)
    25. {
    26. this->data=new T[MAX];
    27. memcpy(this->data,other.data,sizeof(T)*MAX);
    28. /*
    29. for(int i=other.head;i!=other.tail;i=(i+1)%MAX)
    30. {
    31. data[i]=other.data[i];
    32. }
    33. */
    34. this->head=other.head;
    35. this->tail=other.tail;
    36. cout<<"拷贝构造函数"<
    37. }
    38. void queue_push(T &&val);
    39. void queue_pop();
    40. void queue_clear();
    41. bool queue_empty();
    42. bool queue_full();
    43. int queue_len();
    44. void queue_show();
    45. };
    46. template<typename T>
    47. void Queue::queue_push(T &&val)
    48. {
    49. data[tail]=val;
    50. tail=(tail+1)%MAX;
    51. return;
    52. }
    53. template<typename T>
    54. void Queue::queue_pop()
    55. {
    56. cout<"出队成功"<
    57. head=(head+1)%MAX;
    58. return;
    59. }
    60. template<typename T>
    61. void Queue::queue_clear()
    62. {
    63. while(!queue_empty())
    64. {
    65. queue_pop();
    66. }
    67. }
    68. template<typename T>
    69. bool Queue::queue_empty()
    70. {
    71. return head==tail;
    72. }
    73. template<typename T>
    74. bool Queue::queue_full()
    75. {
    76. return head==(tail+MAX)%MAX;
    77. }
    78. template<typename T>
    79. int Queue::queue_len()
    80. {
    81. return (tail+MAX-head)%MAX;
    82. }
    83. template<typename T>
    84. void Queue::queue_show()
    85. {
    86. for(int i=head;i!=tail;i=(i+1)%MAX)
    87. {
    88. cout<" ";
    89. }
    90. cout<
    91. }
    92. int main()
    93. {
    94. Queue<int> Q;
    95. Q.queue_empty();
    96. Q.queue_push(5);
    97. Q.queue_push(8);
    98. Q.queue_push(5);
    99. Q.queue_push(8);
    100. Queue<int> L;
    101. L=Q;
    102. Q.queue_full();
    103. Q.queue_pop();
    104. Q.queue_show();
    105. Q.queue_clear();
    106. L.queue_show();
    107. return 0;
    108. }

    三、思维导图有道云笔记

  • 相关阅读:
    周五(政治日)(第一次)
    Linux常用指令--文件目录常用指令
    CC攻击演示
    HIVE内置函数hash() -- 源码解析
    JavaWeb之jQuery
    linux安装idea
    Java架构师的底气,是从哪里来的?
    Oracle事务
    WPF——样式与模板
    笔记-Elasticsearch搜索引擎构建入门与实战
  • 原文地址:https://blog.csdn.net/wxmchong/article/details/132863737