-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.h
More file actions
55 lines (45 loc) · 1.1 KB
/
stack.h
File metadata and controls
55 lines (45 loc) · 1.1 KB
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
51
52
53
54
55
#ifndef STACK_H
#define STACK_H
#include <vector>
#include <stdexcept>
// Use inheritance from std::vector (choose public/private) as appropriate
template <typename T>
class Stack : public std::vector<T>
{
public:
Stack() = default;
~Stack() = default;
bool empty() const;
size_t size() const;
void push(const T& item);
void pop(); // throws std::underflow_error if empty
const T& top() const; // throws std::underflow_error if empty
// Add other members only if necessary
};
template <typename T>
void Stack<T>::push(const T& item){
std::vector<T>::push_back(item);
}
template <typename T>
bool Stack<T>::empty() const{
return std::vector<T>::empty();
}
template <typename T>
size_t Stack<T>::size() const{
return std::vector<T>::size();
}
template <typename T>
void Stack<T>::pop(){
if(empty()){
throw std::underflow_error("Stack is empty");
}
std::vector<T>::pop_back();
}
template <typename T>
const T& Stack<T>::top() const{
if(empty()){
throw std::underflow_error("Stack is empty");
}
return std::vector<T>::back();
}
#endif