• c++day3


    1> 自行封装一个栈的类,包含私有成员属性:栈的数组、记录栈顶的变量

    成员函数完成:构造函数、析构函数、拷贝构造函数、入栈、出栈、清空栈、判空、判满、获取栈顶元素、求栈的大小

    1. #include
    2. using namespace std;
    3. class Stack
    4. {
    5. private:
    6. int data[50];
    7. int top;//记录栈顶的变量
    8. public:
    9. //构造函数
    10. Stack():top(-1)//栈顶初始化为-1
    11. {
    12. cout<<"构造函数"<
    13. }
    14. //判断栈是否为满
    15. bool S_full()
    16. {
    17. return top==49;
    18. }
    19. //判断栈是否为空
    20. bool S_empty()
    21. {
    22. return top==-1;
    23. }
    24. //入栈
    25. void S_push(const int&item)
    26. {
    27. if(S_full())
    28. {
    29. cout<<"栈已满"<
    30. }
    31. else
    32. {
    33. data[++top]=item;//入栈操作
    34. }
    35. }
    36. //出栈
    37. int S_pop()
    38. {
    39. if(S_empty())
    40. {
    41. cout<<"栈空的"<
    42. return -1;
    43. }
    44. else
    45. {
    46. return data[top--];//出栈操作
    47. }
    48. }
    49. //查看栈顶元素
    50. void S_peek()
    51. {
    52. if(S_empty())
    53. {
    54. cout<<"栈空的"<
    55. }
    56. else
    57. {
    58. cout<<"栈顶元素为:"<
    59. }
    60. }
    61. //求栈的大小
    62. void S_size()
    63. {
    64. cout<<"栈的大小:"<1<
    65. }
    66. //清空栈
    67. void S_free()
    68. {
    69. while(1)
    70. {
    71. if(S_empty())
    72. {
    73. cout<<"栈已清空"<
    74. break;
    75. }
    76. else
    77. {
    78. S_pop();
    79. }
    80. }
    81. }
    82. //析构函数
    83. ~Stack()
    84. {
    85. cout<<"析构函数"<
    86. }
    87. //拷贝构造函数
    88. Stack(const Stack& o) {
    89. cout<<"拷贝构造函数被调用了"<
    90. for(int i=0;i<=top;i++)
    91. {
    92. data[i]=o.data[i];
    93. }
    94. top=o.top;
    95. }
    96. //遍历栈
    97. void S_show()
    98. {
    99. cout<<"栈的元素:";
    100. for(int i=top;i>=0;i--)
    101. {
    102. cout<" ";
    103. }
    104. cout<
    105. }
    106. };
    107. int main()
    108. {
    109. Stack s;
    110. s.S_push(1);
    111. s.S_push(2);
    112. s.S_push(3);
    113. s.S_push(4);
    114. s.S_pop();
    115. s.S_push(5);
    116. s.S_push(6);
    117. s.S_peek();
    118. s.S_size();
    119. s.S_free();
    120. s.S_size();
    121. s.S_push(5);
    122. s.S_push(6);
    123. s.S_size();
    124. s.S_peek();
    125. s.S_show();
    126. return 0;
    127. }

     

    2> 自行封装一个循环顺序队列的类,包含私有成员属性:存放队列的数组、队头位置、队尾位置

    成员函数完成:构造函数、析构函数、拷贝构造函数、入队、出队、清空队列、判空、判满、求队列大小

    1. #include
    2. using namespace std;
    3. class Queue
    4. {
    5. private:
    6. int data[10];
    7. int front;//记录头元素位置
    8. int tail;//记录尾元素位置
    9. public:
    10. //构造函数
    11. Queue():front(0),tail(0)//初始化
    12. {
    13. cout<<"构造函数"<
    14. }
    15. //判断队列是否为满
    16. bool S_full()
    17. {
    18. return (tail+1)%10==front;
    19. }
    20. //判断队列是否为空
    21. bool S_empty()
    22. {
    23. return front==tail;
    24. }
    25. //入队
    26. void S_push(const int&item)
    27. {
    28. if(S_full())
    29. {
    30. cout<<"队列已满"<
    31. }
    32. else
    33. {
    34. data[tail]=item;//入队操作
    35. //队尾后移动
    36. tail=(tail+1)%10;
    37. cout<<"入队成功"<
    38. }
    39. }
    40. //出队
    41. void S_pop()
    42. {
    43. if(S_empty())
    44. {
    45. cout<<"队空的"<
    46. }
    47. else
    48. {
    49. cout<"出队成功"<//出队操作
    50. //队头后移
    51. front=(front+1)%10;
    52. }
    53. }
    54. //求队列的大小
    55. void S_size()
    56. {
    57. cout<<"队列的大小:"<<(tail+10-front)%10<
    58. }
    59. //清空队列
    60. void S_free()
    61. {
    62. while(1)
    63. {
    64. if(S_empty())
    65. {
    66. cout<<"队列已清空"<
    67. break;
    68. }
    69. else
    70. {
    71. S_pop();
    72. }
    73. }
    74. }
    75. //析构函数
    76. ~Queue()
    77. {
    78. cout<<"析构函数"<
    79. }
    80. //拷贝构造函数
    81. Queue(const Queue& o) {
    82. cout<<"拷贝构造函数被调用了"<
    83. for(int i=front;i<=tail;i=(i+1)%10)
    84. {
    85. data[i]=o.data[i];
    86. }
    87. front=o.front;
    88. tail=o.tail;
    89. }
    90. //遍历队列
    91. void S_show()
    92. {
    93. cout<<"队列的元素:";
    94. for(int i=front;i!=tail;i=(i+1)%10)
    95. {
    96. cout<" ";
    97. }
    98. cout<
    99. }
    100. };
    101. int main()
    102. {
    103. Queue s;
    104. s.S_push(1);
    105. s.S_push(2);
    106. s.S_push(3);
    107. s.S_push(4);
    108. s.S_pop();
    109. s.S_push(5);
    110. s.S_push(6);
    111. s.S_size();
    112. s.S_free();
    113. s.S_size();
    114. s.S_push(5);
    115. s.S_push(6);
    116. s.S_push(1);
    117. s.S_push(3);
    118. s.S_push(1);
    119. s.S_push(4);
    120. s.S_size();
    121. s.S_show();
    122. return 0;
    123. }

     思维导图

  • 相关阅读:
    百度面试题:为什么使用接口而不是直接使用具体类?
    Flutter 创建自己的对话框,不使用任何包!
    【ICE】2:基于webrtc的 ice session设计及实现
    BeansTalkd 做消息队列服务
    测试开发必备技能-Jmeter二次开发
    Vue 2 nextTick方法|异步更新|事件循环
    出国参展注意事项
    基于matomo实现业务数据埋点采集上报
    【docker】查看容器日志
    【经验】Word 2021|如何在Word里做出和Markdown中一样漂亮的引用样式(结尾附成品)
  • 原文地址:https://blog.csdn.net/HYL1234511/article/details/132767442