• 2023年9月8日


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

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

    1. #include
    2. using namespace std;
    3. class Stack
    4. {
    5. private:
    6. int data[8];
    7. int top=-1;
    8. public:
    9. //构造函数
    10. Stack()
    11. {
    12. }
    13. //构析函数
    14. ~Stack()
    15. {
    16. }
    17. //拷贝构造函数
    18. Stack(const Stack &other)
    19. {
    20. }
    21. //判空
    22. bool stack_empty(Stack *p)
    23. {
    24. if(p->top==-1)
    25. {
    26. return 1;
    27. }
    28. return 0;
    29. }
    30. //判满
    31. bool stack_full(Stack *p)
    32. {
    33. if(p->top==7)
    34. {
    35. return 1;
    36. }
    37. return 0;
    38. }
    39. //入栈
    40. void stack_push(Stack *p,int e)
    41. {
    42. if(NULL==p||stack_full(p))
    43. {
    44. return;
    45. }
    46. p->top++;
    47. p->data[p->top]=e;
    48. cout<
    49. cout<<"入栈成功"<
    50. return ;
    51. }
    52. //出栈
    53. void stack_pop(Stack *p)
    54. {
    55. if(NULL==p||stack_empty(p))
    56. {
    57. return ;
    58. }
    59. int e=p->data[p->top];
    60. cout<"出栈成功"<
    61. p->top--;
    62. return;
    63. }
    64. //销毁栈
    65. void stack_free(Stack *p)
    66. {
    67. if(NULL==p)
    68. {
    69. return ;
    70. }
    71. delete p;
    72. p=nullptr;
    73. cout<<"销毁成功"<
    74. return ;
    75. }
    76. //获取栈顶元素
    77. void stack_gettop(Stack *p)
    78. {
    79. if(NULL==p)
    80. {
    81. return ;
    82. }
    83. int e = p->data[p->top];
    84. cout<<"栈顶元素为"<
    85. return ;
    86. }
    87. //求栈的大小
    88. void stack_size(Stack *p)
    89. {
    90. if(NULL==p)
    91. {
    92. return ;
    93. }
    94. cout<<"栈的大小为"<top+1<
    95. }
    96. void stack_show(Stack *p)
    97. {
    98. for(int i=0;i<=p->top;i++)
    99. {
    100. cout<<" "<data[i];
    101. }
    102. cout<
    103. return ;
    104. }
    105. };
    106. int main()
    107. {
    108. //初始化
    109. Stack zhan;
    110. //定义一个
    111. Stack *p=new Stack(zhan);
    112. //入栈
    113. zhan.stack_push(p,812);
    114. zhan.stack_push(p,627);
    115. zhan.stack_push(p,908);
    116. zhan.stack_push(p,929);
    117. zhan.stack_push(p,199);
    118. zhan.stack_show(p);
    119. cout<<"*******************************"<
    120. //出栈
    121. zhan.stack_pop(p);
    122. zhan.stack_pop(p);
    123. zhan.stack_pop(p);
    124. zhan.stack_show(p);
    125. //获取栈顶元素
    126. zhan.stack_gettop(p);
    127. //求栈大小
    128. zhan.stack_size(p);
    129. //销毁栈
    130. zhan.stack_free(p);
    131. return 0;
    132. }

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

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

    1. #include
    2. using namespace std;
    3. class Queue
    4. {
    5. private:
    6. int data[8];
    7. int front=0;
    8. int tail=0;
    9. public:
    10. //构造函数
    11. Queue()
    12. {
    13. }
    14. //构析函数
    15. ~Queue()
    16. {
    17. }
    18. //拷贝构造函数
    19. Queue(const Queue &other)
    20. {
    21. }
    22. //判空
    23. int queue_empty(Queue *p)
    24. {
    25. if(NULL==p)
    26. {
    27. cout<<"队列不合法"<
    28. return -1;
    29. }
    30. return 0;
    31. }
    32. //判满
    33. int queue_full(Queue *p)
    34. {
    35. if(NULL==p)
    36. {
    37. cout<<"所给队列不和发"<
    38. return -1;
    39. }
    40. return 0;
    41. }
    42. //入队
    43. void queue_push(Queue *p,int e)
    44. {
    45. if(NULL==p||queue_full(p))
    46. {
    47. cout<<"入队失败"<
    48. return ;
    49. }
    50. p->data[p->tail]=e;
    51. p->tail=(p->tail+1)%8;
    52. cout<<"入队成功"<
    53. return ;
    54. }
    55. //出队
    56. void queue_pop(Queue *p)
    57. {
    58. if(NULL==p||queue_empty(p))
    59. {
    60. cout<<"出队失败"<
    61. return ;
    62. }
    63. cout<data[p->front]<<"出队成功"<
    64. p->front=(p->front+1)%8;
    65. return ;
    66. }
    67. //求队列长度
    68. void queue_size(Queue *p)
    69. {
    70. if(NULL==p)
    71. {
    72. cout<<"所给队列不合法"<
    73. return ;
    74. }
    75. cout<<"队列长度为"<<(p->tail+8-p->front)%8<
    76. return ;
    77. }
    78. //销毁队列
    79. void queue_free(Queue *p)
    80. {
    81. if(NULL!=p)
    82. {
    83. free(p);
    84. p=NULL;
    85. cout<<"释放成功"<
    86. return ;
    87. }
    88. cout<<"所给队列不合法"<
    89. return ;
    90. }
    91. //遍历
    92. void queue_show(Queue *p)
    93. {
    94. for(int i=p->front;i!=p->tail;i=(i+1)%8)
    95. {
    96. cout<data[i]<<" ";
    97. }
    98. cout<
    99. }
    100. };
    101. int main()
    102. {
    103. //定义一个队列
    104. Queue dl;
    105. //创建指针 申请空间
    106. Queue *p=new Queue(dl);
    107. //入队
    108. dl.queue_push(p,8);
    109. dl.queue_push(p,60);
    110. dl.queue_push(p,19);
    111. dl.queue_push(p,627);
    112. dl.queue_push(p,78);
    113. dl.queue_push(p,1);
    114. dl.queue_push(p,2);
    115. dl.queue_show(p);
    116. cout<<"**********************"<
    117. //出队
    118. dl.queue_pop(p);
    119. dl.queue_pop(p);
    120. dl.queue_pop(p);
    121. dl.queue_show(p);
    122. cout<<"**********************"<
    123. //求队列长度
    124. dl.queue_size(p);
    125. //销毁队列
    126. dl.queue_free(p);
    127. return 0;
    128. }

  • 相关阅读:
    数字IC手撕代码-XX公司笔试真题(脉冲密度调制)
    04.Linux文件管理
    Mathorcup数学建模竞赛第四届-【妈妈杯】C题:最佳旅游路线设计与对比(附lingo实现代码)
    线程简单知识点
    Java面向对象07:简答小结类与对象
    湖南中创教育提示你积极维权,保护自身利益
    2023/11/8JAVA学习
    【springMVC】高级部分
    171.Hadoop(七):Yarn的架构,工作机制,调度器,常用命令
    【网络工程】2、eNSP工具下载与安装
  • 原文地址:https://blog.csdn.net/2201_75732711/article/details/132766772