-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlab_stack.h
More file actions
97 lines (86 loc) · 1.86 KB
/
lab_stack.h
File metadata and controls
97 lines (86 loc) · 1.86 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef STACK_H
#define STACK_H
#include <exception>
#include <vector>
using namespace std;
class Stack
{
private:
const int maxSize;
vector<char> stack;
public:
class Full: public exception
{
public:
virtual const char* what() const throw()
{
return "Stack is full";
}
};
class Empty: public exception
{
public:
virtual const char* what() const throw()
{
return "Stack is empty";
}
};
/** Initialise the stack, maxSize is the maximum number of
values that can be stored in the stack at any time */
Stack( const int _maxSize ) : maxSize(_maxSize)
{
}
~Stack()
{
}
/** Returns the number of values currently stored in the
stack */
int num_items() const
{
return stack.size();
}
/** Add value to the top of the stack, raises Stack::Full
exception if stack is full */
void push( char value )
{
if( num_items() < maxSize )
{
stack.emplace_back( value );
}
else
{
throw Full();
}
}
/** Returns the value currently stored at the top of the
stack, raises Stack::Empty exception if stack is
empty */
char top()
{
if( num_items() > 0 )
{
return stack[ stack.size() ];
}
else
{
throw Empty();
}
}
/** Removes and returns the value currently stored at the
top of the stack, raises Stack::Empty exception if stack
is empty */
char pop()
{
if( num_items() > 0 )
{
char value = stack.back();
stack.pop_back();
return value;
}
else
{
throw Empty();
}
}
};
#endif