码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • OOP栈类模板(模板+DS)


    目录

    题目描述

    AC代码


    题目描述

    借助函数模板实现栈的操作。

    栈是一种先进后出的数据结构,它的插入、删除只能在栈顶位置进行。Push为入栈操作,即插入,Pop为出栈操作,即删除。

    栈的操作类似叠盘子,先放的盘子在底下,后放的盘子上面。当要取盘子,就从最上面取。

    例如入栈数据1到2再到3,那么3在最上面,1在最下面。当要出栈数据,就是3先出,接着是2,最后是1出栈。

    要求你自行定义栈结构,并利用函数模板以及类模板完成对char,int和float型数据的处理。其中,我们定义:

    输入

    第一行为测试数据数

    对于每组测试数据,第一行为数据类型,第二行为操作数

    对于每次操作均进行Push或Pop操作。要注意,当空栈弾栈时要给出Error提示

    输出

    对于每次入栈,不需输出。对于每次出栈,输出出栈元素或给出Error提示。当完成所有操作后,依次逆序输出栈中剩余元素

    输入样例1

    3
    I
    6
    Push 6
    Push 1
    Push 1
    Pop
    Pop
    Pop
    C
    4
    Pop
    Push a
    Push a
    Pop
    F
    8
    Push 4.1
    Push 4.2
    Push 14.1
    Push 4.2
    Push 1314
    Push 1314
    Pop
    Pop

    输出样例1

    1 is popped from stack!
    1 is popped from stack!
    6 is popped from stack!
    Empty Stack!
    Error!
    a is popped from stack!
    a 
    1314 is popped from stack!
    1314 is popped from stack!
    4.2 14.1 4.2 4.1 

    AC代码

    1. #include<iostream>
    2. #include<string>
    3. #include<algorithm>
    4. #include<cmath>
    5. #include<iomanip>
    6. using namespace std;
    7. template<class kind>
    8. class stack {
    9. kind p[10000];
    10. int top = -1;
    11. public:
    12. void push() {
    13. kind num;
    14. cin >> num;
    15. p[++top] = num;
    16. }
    17. void pop() {
    18. if (empty()) {
    19. cout << "Error!" << endl;
    20. } else {
    21. cout << p[top--] << " is popped from stack!" << endl;
    22. }
    23. }
    24. bool empty() {
    25. if (top < 0)
    26. return 1;
    27. return 0;
    28. }
    29. void display() {
    30. if (empty())
    31. cout << "Empty Stack!" << endl;
    32. else {
    33. for (int i = top;i>=0;i--)
    34. cout << p[i] << ' ';
    35. cout << endl;
    36. }
    37. }
    38. };
    39. int main() {
    40. int test, count;
    41. char code;
    42. string index;
    43. cin >> test;
    44. while (test--) {
    45. cin >> code >> count;
    46. if (code == 'I') {
    47. stack<int> temp;
    48. while (count--) {
    49. cin >> index;
    50. if (index == "Push")
    51. temp.push();
    52. else
    53. temp.pop();
    54. }
    55. temp.display();
    56. } else if (code == 'C') {
    57. stack<char> temp;
    58. while (count--) {
    59. cin >> index;
    60. if (index == "Push")
    61. temp.push();
    62. else
    63. temp.pop();
    64. }
    65. temp.display();
    66. } else {
    67. stack<float> temp;
    68. while (count--) {
    69. cin >> index;
    70. if (index == "Push")
    71. temp.push();
    72. else
    73. temp.pop();
    74. }
    75. temp.display();
    76. }
    77. }
    78. }
  • 相关阅读:
    给出一个有序的整数数组 A 和有序的整数数组 B ,请将数组 B 合并到数组 A 中,变成一个有序的升序数组
    K8S部署Dashboard和Heapster
    目的和目标的差异|丰田自动工程完结的目的、目标、应用化的意义和明确、二
    MFC Windows 程序设计[150]之上报列表图标全集
    CS144 计算机网络 Lab3:TCP Sender
    leetcode 108.将有序数组转换为二叉搜索树
    【TypeScript基础】TypeScript之常用类型(上)
    【Pygame 学习笔记】8.精灵
    基于springboot汽车租赁系统
    【Ascend C算子开发(入门)】——Ascend C编程模式与范式
  • 原文地址:https://blog.csdn.net/weixin_62264287/article/details/125453388
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号