-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
28 lines (27 loc) · 690 Bytes
/
Stack.cpp
File metadata and controls
28 lines (27 loc) · 690 Bytes
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
#include <iostream>
#include <list>
#include <memory>
using namespace std;
template <class T> class Stack {
private:
unique_ptr<list<T>> data;
public:
Stack() { data = make_unique<list<T>>(); }
void push(T elem) { data->push_back(elem); }
void pop() { data->pop_back(); }
T &top() { return data->back(); }
bool empty() { return data->empty(); }
size_t size() { return data->size(); }
};
int main() {
Stack<int> stack;
stack.push(10);
stack.push(20);
stack.push(30);
cout << "Top: " << stack.top() << '\n';
cout << "Size: " << stack.size() << '\n';
stack.pop();
cout << "Top: " << stack.top() << '\n';
cout << "Size: " << stack.size() << '\n';
return 0;
}