-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.hpp
More file actions
79 lines (70 loc) · 1.17 KB
/
Stack.hpp
File metadata and controls
79 lines (70 loc) · 1.17 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef STACK_HPP
#define STACK_HPP
#include "List.hpp"
template <typename T>
class Stack
{
private:
List<T> internal_list;
public:
class NoElement {};
void clear();
bool is_empty() const;
unsigned long size() const;
T top() const;
void push(const T &);
void pop();
void print() const;
};
template <typename T>
void Stack<T>::clear()
{
internal_list.clear();
}
template <typename T>
bool Stack<T>::is_empty() const
{
return internal_list.is_empty();
}
template <typename T>
unsigned long Stack<T>::size() const
{
return internal_list.size();
}
template <typename T>
T Stack<T>::top() const
{
T value;
try
{
value = internal_list.front();
}
catch (typename List<T>::NoElement)
{
throw NoElement();
}
return value;
}
template <typename T>
void Stack<T>::push(const T & value)
{
internal_list.push_front(value);
}
template <typename T>
void Stack<T>::pop()
{
try
{
internal_list.pop_front();
}
catch (typename List<T>::NoElement)
{
throw NoElement();
}
}
template <typename T>
void Stack<T>::print() const
{
internal_list.print_forwards();
}
#endif