-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyStack.cpp
50 lines (42 loc) · 842 Bytes
/
myStack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "Deque.h"
#include <stdexcept>
#include <sstream>
#include <string>
template <typename T, typename Container = myDeque<T> >
class myStack
{
private:
Container data;
public:
void push(const T & value)
{
data.push_back(value);
}
void pop()
{
if(data.empty()) throw std::runtime_error("Stack is empty!");
else data.pop_back();
}
T& top()
{
if(data.empty()) throw std::runtime_error("Stack is empty!");
else return data[data.size()-1];
}
size_t size()
{
return data.size();
}
bool empty()
{
return data.empty();
}
};
int main()
{
myStack<int> st;
st.push(1);
std::cout << st.size() << std::endl;
std::cout << st.top() << std::endl;
st.push(3);
std::cout << st.top() << std::endl;
}