c++堆栈实现

时间:2014-05-26 17:10:21   收藏:0   阅读:540

A Stack is a data-structure that
You can only add an element to the top of the Stack, and
You can only read or remove an element also from the top.
Please use a vector to implement a Stack

EXAMPLE INPUT
1 2 3 4 5 6 7 8 9EXAMPLE OUTPUT
9 8 7 6 5 4 3 2 1


主程序

#include <vector>
using namespace std;

class EmptyStackException {};

template <typename E>
class Stack
{
private:
vector<E> elements;

public:

void addFirst(const E & elem);
E removeFirst();
};

#include "source"

#include <iostream>
using namespace std;
代写
int main() {
Stack<int> stack;
for (int i = 0; i < 9; ++ i) {
int value;
cin >> value;
stack.addFirst(value);
}
for (int i = 0; i < 9; ++ i) {
cout << stack.removeFirst() << " ";
}
}

c++堆栈实现,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!