-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStack.cpp
63 lines (54 loc) · 1.15 KB
/
Stack.cpp
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
//
// Created by brett on 06/02/18.
//
#include <iostream>
#include "Stack.h"
/**
* Default Construction Function for a Stack.
*/
Stack::Stack() : _top(nullptr) { }
/**
* Custom Destruction Function for a Stack. Loop while _top != nullptr and Pop().
*/
Stack::~Stack() {
while (_top != nullptr) {
Pop();
}
}
/**
* Push a new Point to the Stack.
* @param data Stack being pushed to Stack.
*/
void Stack::Push(Point data) {
_top = new StackNode(data, _top);
}
/**
* Peek into the Stack and return the current _top Point.
* @return Point from the topmost StackNode.
*/
Point Stack::Peek() {
return _top->getPoint();
}
void Stack::Pop() {
if (_top != nullptr) {
StackNode* node = _top;
_top = _top->getNext();
delete node;
} else {
std::cerr << "error: can not Pop() an empty Stack!" << std::endl;
}
}
/**
* Checks if the Stack is empty.
* @return Boolean if _top is a nullptr.
*/
bool Stack::empty() {
return _top == nullptr;
}
/**
* Return the topmost Point from the Stack.
* @return Point from the topmost portion of Stack.
*/
Point Stack::getTop() {
return _top->getPoint();
}