-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.cpp
126 lines (113 loc) · 2.18 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// John Leeds
// 3/2/2022
// Stack.cpp
// Implements a stack for maze generation / solving
#include <iostream>
#include "Stack.h"
using namespace std;
/*
* constructor
* Creates a StackNode with the given coordinates
*/
StackNode::StackNode(int newCoords[]){
myCoords[0] = newCoords[0];
myCoords[1] = newCoords[1];
next = nullptr;
}
// Getters
int* StackNode::getCoords() {
int* cPtr;
cPtr = myCoords;
return cPtr;
}
StackNode* StackNode::getNext() {
return next;
}
// Setters
void StackNode::setCoords(int newCoords[]){
myCoords[0] = newCoords[0];
myCoords[1] = newCoords[1];
}
void StackNode::setNext(StackNode* newNext){
next = newNext;
}
/*
* constructor
* Creates a new Stack from a pair of coordinates
*/
Stack::Stack(int coords[2]){
head = new StackNode(coords);
}
/*
* push
* Pushes a new value of coordinates to the stack
*/
void Stack::push(int newCoords[]){
StackNode* newHead = new StackNode(newCoords);
newHead->setNext(head);
head = newHead;
}
/*
* peek
* Returns the value of the top of the stack
*/
int* Stack::peek() {
int* cPtr;
cPtr = head->getCoords();
return cPtr;
}
/*
* pop
* Deletes the head of the stack and returns the value
*/
int* Stack::pop(){
int* cPtr;
cPtr = head->getCoords();
head = head->getNext();
return cPtr;
}
/*
* length
* Returns the length of the stack
*/
int Stack::length() {
if ( head == NULL ) return 0;
StackNode* counter = head;
int c = 0;
while ( counter ){
c++;
counter = counter->getNext();
}
return c;
}
/*
* isEmpty
* Returns true of the stack is empty
*/
bool Stack::isEmpty() {
if ( head == NULL ) return true;
return false;
}
/*
* show
* Prints the contents of the stack
*/
void Stack::show(){
if ( head == NULL ){
cout << "Stack is empty." << endl;
return;
}
StackNode* printNode = head;
while ( printNode ){
int* coords = printNode->getCoords();
cout << "(" << coords[0] << ", " << coords[1] << ")" << endl;
printNode = printNode->getNext();
}
}
/*
* getHead
* Returns the head of the stack
*/
StackNode* Stack::getHead(){
return head;
}