• 9月13日作业


    思维导图

    用模板实现栈

    1. #include
    2. using namespace std;
    3. template <typename T, int MaxSize>
    4. class Stack {
    5. public:
    6. //构造函数
    7. Stack() : top(-1) {}
    8. //入栈
    9. void push(const T& value);
    10. //出栈
    11. T pop();
    12. //判空
    13. bool empty() const;
    14. //判满
    15. bool full()const;
    16. //清空栈
    17. bool clear();
    18. //获取栈顶元素
    19. T stacktop();
    20. //求栈的大小
    21. int stacksize();
    22. //输出栈元素
    23. bool show();
    24. private:
    25. T data[MaxSize];
    26. int top;
    27. };
    28. int main() {
    29. Stack<int, 3> intStack;
    30. intStack.push(1);
    31. intStack.push(2);
    32. intStack.push(3);
    33. intStack.pop();
    34. intStack.show();
    35. Stack<double, 5> doubleStack;
    36. doubleStack.push(1.1);
    37. doubleStack.push(2.2);
    38. doubleStack.push(3.3);
    39. doubleStack.pop();
    40. doubleStack.show();
    41. return 0;
    42. }
    43. //入栈
    44. template <typename T, int MaxSize>
    45. void Stack::push(const T& value) {
    46. if (top == MaxSize - 1) {
    47. throw std::overflow_error("该栈已满");
    48. }
    49. data[++top] = value;
    50. }
    51. //出栈
    52. template <typename T, int MaxSize>
    53. T Stack::pop() {
    54. if (empty()) {
    55. throw std::underflow_error("该栈为空");
    56. }
    57. return data[top--];
    58. }
    59. //判空
    60. template <typename T, int MaxSize>
    61. bool Stack::empty() const {
    62. return top == -1;
    63. }
    64. //判满
    65. template <typename T, int MaxSize>
    66. bool Stack::full()const
    67. {
    68. return top = MaxSize-1;
    69. }
    70. //清空栈
    71. template <typename T, int MaxSize>
    72. bool Stack::clear()
    73. {
    74. if(empty()){
    75. cout<<"该栈为空"<
    76. return false;
    77. }else {
    78. top = -1;
    79. return true;
    80. }
    81. }
    82. //获取栈顶元素
    83. template <typename T, int MaxSize>
    84. T Stack::stacktop()
    85. {
    86. return data[top];
    87. }
    88. //求栈的大小
    89. template <typename T, int MaxSize>
    90. int Stack::stacksize()
    91. {
    92. return top+1;
    93. }
    94. //输出栈元素
    95. template <typename T, int MaxSize>
    96. bool Stack::show()
    97. {
    98. if(empty()){
    99. cout<<"该栈为空"<
    100. return false;
    101. }else {
    102. for(int i=0;i<=top; i++){
    103. cout<" ";
    104. }
    105. cout<
    106. return true;
    107. }
    108. }

    用模板实现队列

    1. #include
    2. #define MAXSIZE 16
    3. using namespace std;
    4. template <typename T, int MaxSize>
    5. class LoopQueue
    6. {
    7. public:
    8. //构造函数
    9. LoopQueue():FrontIndex(0), RearIndex(0){};
    10. //入队列
    11. bool push(T value);
    12. //出队列并返回队列顶元素
    13. T pop();
    14. //清空队列
    15. bool clear();
    16. //判空
    17. bool isEmpty();
    18. //判满
    19. bool isFull();
    20. //求队列的大小
    21. int LoopQueuesize();
    22. //输出队列元素
    23. bool show();
    24. private:
    25. T Data[MaxSize];
    26. int FrontIndex;
    27. int RearIndex;
    28. };
    29. int main()
    30. {
    31. LoopQueue<int, 5> intL;
    32. intL.push(5);
    33. intL.push(7);
    34. intL.push(10);
    35. intL.pop();
    36. intL.show();
    37. LoopQueue<double, 5> doubleL;
    38. doubleL.push(5.5);
    39. doubleL.push(7.4);
    40. doubleL.push(10.3);
    41. doubleL.pop();
    42. doubleL.show();
    43. return 0;
    44. }
    45. //入队列
    46. template <typename T, int MaxSize>
    47. bool LoopQueue::push(T value)
    48. {
    49. if(isFull()){
    50. cout<<"该队列已满"<
    51. return false;
    52. }else{
    53. Data[RearIndex] = value;
    54. RearIndex++;
    55. return true;
    56. }
    57. }
    58. //出队列并返回队列顶元素
    59. template <typename T, int MaxSize>
    60. T LoopQueue::pop()
    61. {
    62. if(isEmpty()){
    63. throw std::underflow_error("该队列为空");
    64. }else {
    65. int Frontvalue = Data[RearIndex];
    66. FrontIndex++;
    67. return Frontvalue;
    68. }
    69. }
    70. //清空队列
    71. template <typename T, int MaxSize>
    72. bool LoopQueue::clear()
    73. {
    74. if(isEmpty()){
    75. cout<<"该队列为空"<
    76. return false;
    77. }else {
    78. FrontIndex = -1;
    79. RearIndex = -1;
    80. return true;
    81. }
    82. }
    83. //判空
    84. template <typename T, int MaxSize>
    85. bool LoopQueue::isEmpty()
    86. {
    87. return FrontIndex==RearIndex;
    88. }
    89. //判满
    90. template <typename T, int MaxSize>
    91. bool LoopQueue::isFull()
    92. {
    93. return (RearIndex+1)%MAXSIZE==FrontIndex;
    94. }
    95. //求队列的大小
    96. template <typename T, int MaxSize>
    97. int LoopQueue::LoopQueuesize()
    98. {
    99. return (RearIndex-FrontIndex+MAXSIZE)%MAXSIZE;
    100. }
    101. //输出队列元素
    102. template <typename T, int MaxSize>
    103. bool LoopQueue::show()
    104. {
    105. if(isEmpty()){
    106. cout<<"该队列为空"<
    107. return false;
    108. }else {
    109. int i=FrontIndex;
    110. cout<<"该队列为>>";
    111. while(1)
    112. {
    113. cout<" ";
    114. i++;
    115. if(MAXSIZE==i)
    116. {
    117. i=0;
    118. }
    119. if(i==RearIndex)
    120. {
    121. break;
    122. }
    123. }
    124. cout<
    125. return true;
    126. }
    127. }

  • 相关阅读:
    Git乱码
    Asp .Net Core 系列:Asp .Net Core 配置 System.Text.Json
    【Python】第十课 魔法方法
    基于低代码平台的OA系统,更灵活高效!
    linux USB摄像头 V4L2工具调试摄像头
    音乐转录(AMT)库Omnizart论文笔记及实践
    吴恩达机器学习笔记十四 多输出的分类 多类和多标签的区别 梯度下降优化 卷积层
    Google开源依赖注入框架-Guice指南
    【摆脱他人眼光的束缚】如何重视自我感受,做好自己
    Go实现日志2——支持结构化和hook
  • 原文地址:https://blog.csdn.net/mcslll/article/details/132863957