您的位置:首页 > 理论基础 > 数据结构算法

计蒜客 数据结构 栈 数列翻转

2016-08-11 22:08 375 查看
//数据结构 栈 数列翻转
#include<iostream>
#include<string>
#include<cassert>
using namespace std;
template<class Type> class Stack {
private:
Type *urls;
int max_size, top_index;
public:
Stack(int length_input) {
urls = new Type[length_input];
max_size = length_input;
top_index = -1;
}
~Stack() {
delete[] urls;
}
bool push(const Type &element) {
if (top_index >= max_size - 1) {
return false;
}
top_index++;
urls[top_index] = element;
return true;
}
bool pop() {
if (top_index < 0) {
return false;
}
top_index--;
return true;
}
Type top() {
assert(top_index >= 0);
return urls[top_index];
}
bool empty(){
if(top_index < 0){
return true;
}
else{
return false;
}
}

};
int main() {
//n表示输入元素个数,num表示输入的元素
int n,num;
cin>>n;
//定义一个int类型的栈stack,n表示栈里最多有n个元素
Stack<int> stack(n);
for(int i = 1;i <= n; i++){
cin>>num;
stack.push(num);
}
while(!stack.empty()){
cout<<stack.top()<<" ";
stack.pop();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 C++