• 用模板完成顺序栈和顺序队列


    顺序栈 

    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. this->top=-1;
    60. }
    61. template<typename T>
    62. bool Stack::stack_empty()
    63. {
    64. return -1==this->top;
    65. }
    66. template<typename T>
    67. bool Stack::stack_full()
    68. {
    69. return MAX-1==this->top;
    70. }
    71. template<typename T>
    72. T Stack::stack_gettop()
    73. {
    74. return this->data[top];
    75. }
    76. template<typename T>
    77. int Stack::stack_len()
    78. {
    79. return this->top+1;
    80. }
    81. template<typename T>
    82. void Stack::stack_show()
    83. {
    84. for(int i=top;i>=0;i--)
    85. {
    86. cout<<this->data[i]<<" ";
    87. }
    88. cout<
    89. }

    顺序队列

    1. #include
    2. #define MAX 50
    3. using namespace std;
    4. template<typename T>
    5. class Queue
    6. {
    7. private:
    8. T *data;
    9. T head;
    10. T tail;
    11. public:
    12. Queue():data(new T[MAX]),head((T)0),tail((T)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. this->head=other.head;
    29. this->tail=other.tail;
    30. cout<<"拷贝构造函数"<
    31. }
    32. void queue_push(T &&val);
    33. void queue_pop();
    34. void queue_clear();
    35. bool queue_empty();
    36. bool queue_full();
    37. int queue_len();
    38. void queue_show();
    39. };
    40. template<typename T>
    41. void Queue::queue_push(T &&val)
    42. {
    43. data[tail]=val;
    44. tail=(tail+1)%MAX;
    45. return;
    46. }
    47. template<typename T>
    48. void Queue::queue_pop()
    49. {
    50. cout<"出队成功"<
    51. head=(head+1)%MAX;
    52. return;
    53. }
    54. template<typename T>
    55. void Queue::queue_clear()
    56. {
    57. while(!queue_empty())
    58. {
    59. queue_pop();
    60. }
    61. }
    62. template<typename T>
    63. bool Queue::queue_empty()
    64. {
    65. return head==tail;
    66. }
    67. template<typename T>
    68. bool Queue::queue_full()
    69. {
    70. return head==(tail+MAX)%MAX;
    71. }
    72. template<typename T>
    73. int Queue::queue_len()
    74. {
    75. return (tail+MAX-head)%MAX;
    76. }
    77. template<typename T>
    78. void Queue::queue_show()
    79. {
    80. for(int i=head;i!=tail;i=(i+1)%MAX)
    81. {
    82. cout<" ";
    83. }
    84. cout<
    85. }

  • 相关阅读:
    Unity入门02——Unity工作原理
    maven篇6:maven打包
    node(coderwhy)
    JAVA基础(JAVA SE)学习笔记(八)面向对象编程(高级)
    JupyterLab | 这几款插件推荐给天天使用JupyterLab的你!~
    SpringBoot缓存之Ehcache详解
    Cortex-M系列处理器偶发死机定位方法
    海思芯片pcie启动——pcie_mcc驱动框架的booter程序分析
    数据挖掘技术-转换字符串时间为标准时间
    网页轮播图
  • 原文地址:https://blog.csdn.net/qq_64682865/article/details/132864903