forked from alexhagiopol/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.h
69 lines (67 loc) · 2.33 KB
/
Stack.h
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
//
// Created by Alex Hagiopol on 7/30/18.
//
#pragma once
#include "chapter_02_linked_lists/Node.h"
namespace chapter_03{
template <typename T>
class Stack{
protected:
chapter_02::SinglyLinkedNode<T>* _head;
public:
Stack(){_head = nullptr;}
// check if stack is empty
bool isEmpty() {return _head == nullptr;}
// add item to top of stack
virtual void push(const T& data) {
chapter_02::SinglyLinkedNode<T>* node = new chapter_02::SinglyLinkedNode<T>(data, _head);
_head = node;
}
// remove item from top of stack
virtual T pop(){
if (_head != nullptr){
chapter_02::SinglyLinkedNode<T>* temp = _head;
T tempValue = temp->getValue();
_head = _head->getNext();
delete temp;
return tempValue;
} else {
return static_cast<T>(0); // nothing to return if stack is empty;
}
}
// inspect item at top of stack without removing
T peek(){
if (_head != nullptr){
return _head->getValue();
} else {
return static_cast<T>(0); // nothing to return if stack is empty;
}
}
// destructor
~Stack(){
while (_head != nullptr){
chapter_02::SinglyLinkedNode<T>* temp = _head;
_head = _head->getNext();
delete temp;
}
}
// compare values in stack top-to-bottom to values in vector front-to-back for easy unit testing
bool operator==(const std::vector<T>& vector){
chapter_02::SinglyLinkedNode<T>* temp = _head;
for (T item : vector){
if (temp == nullptr || item != temp->getValue()){
return false; // vector length > stack height
}
if (item != temp->getValue()){
return false; // value inequality
}
temp = temp->getNext();
}
if (temp != nullptr) return false; // stack height > vector length
return true;
}
bool operator!=(const std::vector<T>& vector){
return !(*this==vector);
}
}; // class Stack
}; // namespace chapter_03