当前位置:网站首页>OOP栈类模板(模板+DS)
OOP栈类模板(模板+DS)
2022-06-25 04:01:00 【SZU治愈系BUG】
目录
题目描述
借助函数模板实现栈的操作。
栈是一种先进后出的数据结构,它的插入、删除只能在栈顶位置进行。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代码
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<iomanip>
using namespace std;
template<class kind>
class stack {
kind p[10000];
int top = -1;
public:
void push() {
kind num;
cin >> num;
p[++top] = num;
}
void pop() {
if (empty()) {
cout << "Error!" << endl;
} else {
cout << p[top--] << " is popped from stack!" << endl;
}
}
bool empty() {
if (top < 0)
return 1;
return 0;
}
void display() {
if (empty())
cout << "Empty Stack!" << endl;
else {
for (int i = top;i>=0;i--)
cout << p[i] << ' ';
cout << endl;
}
}
};
int main() {
int test, count;
char code;
string index;
cin >> test;
while (test--) {
cin >> code >> count;
if (code == 'I') {
stack<int> temp;
while (count--) {
cin >> index;
if (index == "Push")
temp.push();
else
temp.pop();
}
temp.display();
} else if (code == 'C') {
stack<char> temp;
while (count--) {
cin >> index;
if (index == "Push")
temp.push();
else
temp.pop();
}
temp.display();
} else {
stack<float> temp;
while (count--) {
cin >> index;
if (index == "Push")
temp.push();
else
temp.pop();
}
temp.display();
}
}
}
边栏推荐
猜你喜欢
CTF_ Web: how to recognize and evaluate a regular expression
Office macro virus bounce shell experiment
CTF_ Web: Advanced questions of attack and defense world expert zone WP (1-4)
Basic use of OBS browser+ browser
Read lsd-slam: large scale direct monolithic slam
UCLA | generative pre training for black box optimization
GBASE 8s 总体架构
Error 1062 is reported during MySQL insertion, but I do not have this field.
Cnpm: unable to load file c:\users\administrator\appdata\roaming\npm\cnpm PS1 because running scripts is prohibited on this system.
Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding
随机推荐
Read lsd-slam: large scale direct monolithic slam
Structure syntaxique des procédures stockées gbase 8S
CMD operation MySQL in Windows
cnpm : 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。
LeetCode 剑指Offer II 091 粉刷房子[动态规划] HERODING的LeetCode之路
Unit test coverage
Error 1062 is reported during MySQL insertion, but I do not have this field.
js中的concat()
Value transfer between parent and child components of wechat applet
【esp32学习之路6——flash加密】
CTF_ Web: Advanced questions of attack and defense world expert zone WP (15-18)
Coinlist queuing tutorial to improve the winning rate
CTF_ Web: deserialization of learning notes (II) CTF classic test questions from shallow to deep
Record small knowledge points
Intel 13th generation core showed its true colors for the first time: 68mb cache improved significantly
Upgrade cmake
js的arguments
LeetCode 劍指Offer II 091 粉刷房子[動態規劃] HERODING的LeetCode之路
微信小程序父子组件之间传值
Laravel document sorting 9. Blade template