• 9月8日作业


    思维导图

    stack.h

    1. #ifndef STACK_H
    2. #define STACK_H
    3. #include
    4. #define MAXSIZE 128
    5. using namespace std;
    6. class Stack
    7. {
    8. public:
    9. //构造函数
    10. Stack();
    11. //析构函数
    12. ~Stack();
    13. //拷贝构造函数
    14. Stack(const Stack &other);
    15. //入栈
    16. bool push(int value);
    17. //出栈并返回栈顶元素
    18. int pop();
    19. //清空栈
    20. bool clear();
    21. //判空
    22. bool isEmpty();
    23. //判满
    24. bool isFull();
    25. //获取栈顶元素
    26. int stacktop();
    27. //求栈的大小
    28. int stacksize();
    29. //输出栈元素
    30. bool show();
    31. private:
    32. int *stackData;
    33. int topIndex;
    34. };
    35. #endif // STACK_H

    stack.cpp

    1. #include"stack.h"
    2. //构造函数
    3. Stack::Stack()
    4. {
    5. // 使用动态内存分配来创建堆上的栈
    6. stackData = new int[MAXSIZE];
    7. topIndex = -1; // 初始化栈顶索引为-1,表示栈为空
    8. }
    9. //析构函数
    10. Stack::~Stack()
    11. {
    12. delete [] stackData;
    13. }
    14. //拷贝构造函数
    15. Stack::Stack(const Stack &other)
    16. {
    17. topIndex = other.topIndex;
    18. stackData = new int[MAXSIZE];
    19. for (int i = 0; i <= topIndex; i++) {
    20. stackData[i] = other.stackData[i];
    21. }
    22. }
    23. //入栈
    24. bool Stack::push(int value)
    25. {
    26. if(isFull()){
    27. cout<<"该栈已满,无法插入"<
    28. return false;
    29. }else {
    30. topIndex++;
    31. stackData[topIndex] = value;
    32. return true;
    33. }
    34. }
    35. //出栈并返回栈顶元素
    36. int Stack::pop()
    37. {
    38. if(isEmpty()){
    39. cout<<"该栈为空"<
    40. return -1;
    41. }else {
    42. int topvalue = stackData[topIndex];
    43. topIndex--;
    44. return topvalue;
    45. }
    46. }
    47. //清空栈
    48. bool Stack::clear()
    49. {
    50. if(isEmpty()){
    51. cout<<"该栈为空"<
    52. return false;
    53. }else {
    54. topIndex = -1;
    55. return true;
    56. }
    57. }
    58. //判空
    59. bool Stack::isEmpty()
    60. {
    61. return topIndex == -1;
    62. }
    63. //判满
    64. bool Stack::isFull()
    65. {
    66. return topIndex == MAXSIZE-1;
    67. }
    68. //获取栈顶元素
    69. int Stack::stacktop()
    70. {
    71. return stackData[topIndex];
    72. }
    73. //求栈的大小
    74. int Stack::stacksize()
    75. {
    76. return topIndex+1;
    77. }
    78. //输出栈元素
    79. bool Stack::show()
    80. {
    81. if(isEmpty()){
    82. cout<<"该栈为空"<
    83. return false;
    84. }else {
    85. for(int i=0;i<=topIndex; i++){
    86. cout<" ";
    87. }
    88. cout<
    89. return true;
    90. }
    91. }

    main.cpp

    1. #include"stack.h"
    2. int main()
    3. {
    4. Stack s1;
    5. s1.push(5);
    6. s1.push(5);
    7. s1.pop();
    8. s1.show();
    9. s1.~Stack();
    10. return 0;
    11. }

    循环队列 loopqueue.h

    1. #ifndef LOOPQUEUE_H
    2. #define LOOPQUEUE_H
    3. #include
    4. #define MAXSIZE 16
    5. using namespace std;
    6. class LoopQueue
    7. {
    8. public:
    9. //构造函数
    10. LoopQueue();
    11. //析构函数
    12. ~LoopQueue();
    13. //拷贝构造函数
    14. LoopQueue(const LoopQueue &other);
    15. //入队列
    16. bool push(int value);
    17. //出队列并返回队列顶元素
    18. int pop();
    19. //清空队列
    20. bool clear();
    21. //判空
    22. bool isEmpty();
    23. //判满
    24. bool isFull();
    25. //求队列的大小
    26. int LoopQueuesize();
    27. //输出队列元素
    28. bool show();
    29. private:
    30. int *LoopQueueData;
    31. int FrontIndex;
    32. int RearIndex;
    33. };
    34. #endif // LOOPQUEUE_H

    loopqueue.cpp

    1. #include"LoopQueue.h"
    2. //构造函数
    3. LoopQueue::LoopQueue()
    4. {
    5. LoopQueueData = new int[MAXSIZE];
    6. FrontIndex = 0;
    7. RearIndex = 0;
    8. }
    9. //析构函数
    10. LoopQueue::~LoopQueue()
    11. {
    12. delete [] LoopQueueData;
    13. }
    14. //拷贝构造函数
    15. LoopQueue::LoopQueue(const LoopQueue &other)
    16. {
    17. LoopQueueData = new int[MAXSIZE];
    18. int i=other.FrontIndex;
    19. while(1)
    20. {
    21. LoopQueueData[i] = other.LoopQueueData[i];
    22. i++;
    23. if(MAXSIZE==i)
    24. {
    25. i=0;
    26. }
    27. if(i==other.RearIndex)
    28. {
    29. break;
    30. }
    31. }
    32. FrontIndex = other.FrontIndex;
    33. RearIndex = other.RearIndex;
    34. }
    35. //入队列
    36. bool LoopQueue::push(int value)
    37. {
    38. if(isFull()){
    39. cout<<"该队列已满"<
    40. return false;
    41. }else{
    42. LoopQueueData[RearIndex] = value;
    43. RearIndex++;
    44. return true;
    45. }
    46. }
    47. //出队列并返回队列顶元素
    48. int LoopQueue::pop()
    49. {
    50. if(isEmpty()){
    51. cout<<"该队列为空"<
    52. return -1;
    53. }else {
    54. int Frontvalue = LoopQueueData[RearIndex];
    55. FrontIndex++;
    56. return Frontvalue;
    57. }
    58. }
    59. //清空队列
    60. bool LoopQueue::clear()
    61. {
    62. if(isEmpty()){
    63. cout<<"该队列为空"<
    64. return false;
    65. }else {
    66. FrontIndex = -1;
    67. RearIndex = -1;
    68. return true;
    69. }
    70. }
    71. //判空
    72. bool LoopQueue::isEmpty()
    73. {
    74. return FrontIndex==RearIndex;
    75. }
    76. //判满
    77. bool LoopQueue::isFull()
    78. {
    79. return (RearIndex+1)%MAXSIZE==FrontIndex;
    80. }
    81. //求队列的大小
    82. int LoopQueue::LoopQueuesize()
    83. {
    84. return (RearIndex-FrontIndex+MAXSIZE)%MAXSIZE;
    85. }
    86. //输出队列元素
    87. bool LoopQueue::show()
    88. {
    89. if(isEmpty()){
    90. cout<<"该队列为空"<
    91. return false;
    92. }else {
    93. int i=FrontIndex;
    94. cout<<"该队列为>>";
    95. while(1)
    96. {
    97. cout<" ";
    98. i++;
    99. if(MAXSIZE==i)
    100. {
    101. i=0;
    102. }
    103. if(i==RearIndex)
    104. {
    105. break;
    106. }
    107. }
    108. cout<
    109. return true;
    110. }
    111. }

    main.cpp

    1. #include"LoopQueue.h"
    2. using namespace std;
    3. int main()
    4. {
    5. LoopQueue l1;
    6. /*int value = -1;
    7. while(1){
    8. cout<<"请输入要插入的值>>> "<
    9. cin>>value;
    10. if(-1==value){
    11. break;
    12. }else{
    13. l1.push(value);
    14. }
    15. }*/
    16. l1.push(23);
    17. l1.push(44);
    18. l1.push(39);
    19. l1.pop();
    20. l1.show();
    21. cout<<"队列大小为>>"<LoopQueuesize()<
    22. return 0;
    23. }

  • 相关阅读:
    ESP8266 WiFi物联网智能插座—上位机和下位机通信协议
    如何对用户输入进行校验
    前端,CSS,背景颜色跟随轮播图片改变而改变(附源码)
    提供CY系列菁染料CY3、CY5、CY5.5、CY7、CY7.5,ICG,荧光素FITC,Bodipy系列染料标记海藻酸钠Alginate
    练习8:多重子查询
    2022年大一学生实训作业【基于HTML+CSS制作中华传统文化传统美德网站 (6页面)】
    Windows 安装 chromedriver 和 Python 调试
    Java 7 生命周期结束
    91. 存钱罐
    【Hive SQL】统计同名路径下目录数量(基于reverse、split和substr函数)
  • 原文地址:https://blog.csdn.net/mcslll/article/details/132799537