-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntStack.cpp
More file actions
44 lines (37 loc) · 805 Bytes
/
Copy pathIntStack.cpp
File metadata and controls
44 lines (37 loc) · 805 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// Andrei Kolomiets 143-1
// CLion 1.2 MinGW 3.4.1
// 27.01.2016
//
#include <stdexcept>
#include "IntStack.h"
IntStack::IntStack(size_t sz) {
_stack = new int[_ssize = sz]; // Allocate stack
_head = -1; // Set current head
}
IntStack::~IntStack() {
delete _stack; // Free memory
}
void IntStack::push(int el) {
// size_t is unsigned
if (++_head >= _ssize) {
throw std::logic_error("Stack overflow");
}
_stack[_head] = el;
}
int IntStack::pop() {
if (_head >= _ssize) {
throw std::logic_error("Stack is empty");
}
return _stack[_head--];
}
int IntStack::top() {
if (_head >= _ssize) {
throw std::logic_error("Stack is empty");
}
return _stack[_head];
}
void IntStack::clear() {
// init not needed
_head = -1;
}