• C++day3


    1> 自行封装一个栈的类

    包含私有成员属性:栈的数组、记录栈顶的变量

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

    1. #include
    2. #define MAX 128
    3. using namespace std;
    4. class Stack
    5. {
    6. private:
    7. int *p = new int[MAX];//栈的数组
    8. int top;//记录栈顶的下标
    9. public:
    10. //构造函数
    11. Stack()
    12. {
    13. top = -1;
    14. cout<<"无参构造函数"<
    15. }
    16. //析构函数
    17. ~Stack()
    18. {
    19. cout<<"Stack::析构函数"<
    20. }
    21. //拷贝构造函数
    22. Stack(const Stack &other):p(other.p),top(other.top)
    23. {
    24. cout<<"拷贝构造函数"<
    25. }
    26. //入栈
    27. bool stack_push(int e)
    28. {
    29. if(stack_full())
    30. {
    31. cout<<"入栈失败"<
    32. return false;
    33. }
    34. top++;
    35. p[top] = e;
    36. cout<" 入栈成功"<
    37. return true;
    38. }
    39. //出栈
    40. bool stack_pop()
    41. {
    42. if(stack_empty())
    43. {
    44. return false;
    45. }
    46. int e = p[top];
    47. top--;
    48. cout<" 出栈成功"<
    49. return true;
    50. }
    51. //清空栈
    52. void stack_free()
    53. {
    54. while(top != -1)
    55. {
    56. stack_pop();
    57. }
    58. delete [] p;
    59. p = nullptr;
    60. cout<<"清空成功"<
    61. }
    62. //判空
    63. int stack_empty()
    64. {
    65. return top == -1;
    66. }
    67. //判满
    68. int stack_full()
    69. {
    70. return top == MAX-1;
    71. }
    72. //获取栈顶元素
    73. int stack_top()
    74. {
    75. if(stack_empty())
    76. {
    77. cout<<"获取失败"<
    78. return -1;
    79. }
    80. cout<<"栈顶元素为 "<
    81. return p[top];
    82. }
    83. //栈的大小
    84. int stack_size()
    85. {
    86. cout<<"栈的大小为 "<1<
    87. return top+1;
    88. }
    89. };
    90. int main()
    91. {
    92. Stack m;
    93. m.stack_push(6);
    94. m.stack_push(7);
    95. m.stack_push(4);
    96. m.stack_push(8);
    97. m.stack_push(9);
    98. m.stack_pop();
    99. m.stack_top();
    100. m.stack_size();
    101. m.stack_free();
    102. return 0;
    103. }

    2>自行封装一个循环顺序队列的类

    包含私有成员属性:存放队列的数组、队头位置、队尾位置

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

    1. #include
    2. #define MAX 128
    3. using namespace std;
    4. class Queue
    5. {
    6. private:
    7. int *p=new int[MAX];//队列的数组
    8. int tail;//记录队尾元素
    9. int head;//记录对头元素
    10. public:
    11. //构造函数
    12. Queue(int t=0)
    13. {
    14. head=t;
    15. tail=t;
    16. cout<<"无参构造函数"<
    17. }
    18. //析构函数
    19. ~Queue()
    20. {
    21. cout<<"Stack::析构函数"<
    22. }
    23. //拷贝构造函数
    24. Queue(const Queue &other):p(other.p),tail(other.tail),head(other.head)
    25. {
    26. cout<<"拷贝构造函数"<
    27. }
    28. //入队
    29. int queue_push(int e)
    30. {
    31. if(queue_full())
    32. {
    33. cout<<"入队失败"<
    34. return -1;
    35. }
    36. p[tail]=e;
    37. tail++;
    38. cout<<"入队成功"<
    39. return 0;
    40. }
    41. //出队
    42. int queue_pop()
    43. {
    44. if(queue_empty())
    45. {
    46. cout<<"出队失败"<
    47. return -1;
    48. }
    49. int e = p[head];
    50. head = (head+1)%MAX;
    51. cout<" 出队成功"<
    52. return 0;
    53. }
    54. //清空队列
    55. bool queue_delete()
    56. {
    57. while(head!=tail)
    58. {
    59. queue_pop();
    60. }
    61. delete [] p;
    62. p=nullptr;
    63. cout<<"清空队列成功"<
    64. return true;
    65. }
    66. //判空
    67. bool queue_empty()
    68. {
    69. if(head==tail)
    70. {
    71. cout<<"队列空"<
    72. return true;
    73. }
    74. return false;
    75. }
    76. //判满
    77. bool queue_full()
    78. {
    79. if((tail+1)==0)
    80. {
    81. cout<<"队列满了"<
    82. return true;
    83. }
    84. return false;
    85. }
    86. //队列的大小
    87. void queue_getsize()
    88. {
    89. int size;
    90. size=(tail-head+MAX)%MAX;
    91. cout<<"队的大小为:"<
    92. }
    93. void show(int i)
    94. {
    95. cout<" ";
    96. }
    97. };
    98. int main()
    99. {
    100. Queue q1;
    101. int e;
    102. int s;
    103. q1.queue_empty();
    104. cout<<"请输入要入队的个数:";
    105. cin>>s;
    106. for(int i=0;i
    107. {
    108. cout<<"请输入要入队的元素:";
    109. cin>>e;
    110. q1.queue_push(e);
    111. }
    112. q1.queue_getsize();
    113. for(int i=0;i
    114. {
    115. q1.show(i);
    116. }
    117. cout<
    118. q1.queue_delete();
    119. return 0;
    120. }

     

     

  • 相关阅读:
    使用 MyBatis 日志插件实现日志记录
    聊聊芯片制造中的金属杂质
    Keil 厂商DFP pack实现原理
    LeetCode每日一题(2196. Create Binary Tree From Descriptions)
    SprIngSecurity实战(二)各种架构中所应用的安全性
    java 并发执行批量异步任务(Future、 CompletableFuture 实现)
    Airpods的电池健不健康不直观,但例行检查很重要!如何检查Airpods的电池健康状况
    【详细学习SpringBoot核心源码之SpringApplication构造器&Run方法源码详细流程-4】
    es6运算符扩展
    牛血清白蛋白包裹金纳米簇
  • 原文地址:https://blog.csdn.net/qq_53478460/article/details/132779228