-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocationStack.java
246 lines (209 loc) · 6.62 KB
/
LocationStack.java
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/** CMPT_435L_800
* Project 1 -- Maze Solver
* Filename: LocationStack.java
* Student Name: Eric Stenton
* Due Date: February 12, 2020
* Version 1.0
*
* This file contains functions pertaining to the creation of a stack that
* holds location node objects that serve as a linked list structure. Each
* node points to a location in which the search has defined as its current
* path.
*/
/**
* LocationStack
*
* This class defines a stack that holds location objects. It provides standard
* stack functions such as push, pop, and others. The top variable is a
* reference to the uppermost location node stored in the stack.
*/
class LocationStack {
private LocationStack(LocationStack s) { assert(false); }
private LocationStackNode top;
/** LocationStack
* parameters: nothing
* return value: nothing
*
* This function serves as the constructor for the LocationStack object.
* It requires no input variables and it initializes the top variable to
* null.
*/
LocationStack() {
top = null;
}
/** push
* parameters:
* loc -- Location object to push onto stack.
* return value: nothing
*
* This function adds a location object onto the stack. It also reorders
* the previous top location node under the new one and replaces the top
* variable.
*/
void push(Location loc) {
// Create a new node where next node is the previous one on top
LocationStackNode node = new LocationStackNode(loc, top);
// Make the top node variable equal to the new one
top = node;
}
/** pop
* parameters: nothing
* return value: nothing
*
* This function removes the top location node on the stack. It does this
* simply by replacing the top variable with the next node.
*/
void pop() {
// Replace the top node with the node specified as the next one
top = top.getNextNode();
}
/** getTop
* parameters: nothing
* return value:
* Location -- The location that is currently at the top of the stack.
*
* This function retrieves the location at the top of the stack and
* returns it.
*/
Location getTop() {
return top.getLocation();
}
/** isEmpty
* parameters: nothing
* return value:
* boolean -- true if the stack is empty and false if it is not
*
* This function determines if the stack is empty by checking if the
* top location node is null.
*/
boolean isEmpty() {
// Stack is empty only when the top node is null
return top == null;
}
/** isOn
* parameters:
* loc -- A location object to check if it exists in the stack.
* return value:
* boolean -- true if location object is in stack and false if not.
*
* This function loops through the stack and checks if the given location
* object is equal to any of the location objects in the stack.
*/
boolean isOn(Location loc) {
// Loop through nodes and check for an equal location
LocationStackNode currentNode = top;
while (currentNode != null) {
if ( currentNode.getLocation().isEqual(loc) ) {
return true; // An equal location was found
}
currentNode = currentNode.getNextNode();
}
return false; // An equal location was not found
}
/** streamOut
* parameters:
* s -- The stack to be printed.
* return value: nothing
*
* This function prints the contents of the location stack by first
* reversing the linked list of nodes within it, then prints each location
* as the linked list is returned to its original order.
*/
void streamOut(LocationStack s) {
// Get starting variables -- will act as a sliding window when iterating
LocationStackNode previousNode = null;
LocationStackNode currentNode = top;
LocationStackNode nextNode;
// Initial pass & link switch
while (currentNode != null) {
// Define next node
nextNode = currentNode.getNextNode();
// Switch link
currentNode.setNextNode(previousNode);
// Move node 'window' down by one
previousNode = currentNode;
currentNode = nextNode;
}
// Replace top with the last one after loop
top = previousNode;
// Reset variables
previousNode = null;
currentNode = top;
// Second pass & link switch
while (currentNode != null) {
// Print location
currentNode.getLocation().streamOut();
// Define next node
nextNode = currentNode.getNextNode();
// Switch link
currentNode.setNextNode(previousNode);
// Move node 'window' down by one
previousNode = currentNode;
currentNode = nextNode;
// Get prints to be on new lines
System.out.println("");
}
// Replace top with the last one after loop
top = previousNode;
}
}
/**
* LocationStackNode
*
* This class defines a location node that acts as a component within a linked
* list which serves as the implementation of the location stack. Each node
* has a location object and a reference to the node that follows it.
*/
class LocationStackNode {
private LocationStackNode() { assert(false); }
private LocationStackNode(LocationStackNode s) { assert(false); }
private Location location;
private LocationStackNode nextNode;
/** LocationStackNode
* parameters:
* loc -- The location object to be stored within the node.
* next -- A reference to the following location node.
* return value: nothing
*
* This function serves as the constructor for the LocationStackNode object.
* It initializes the location and nextNode variables with the given
* parameters.
*/
LocationStackNode(Location loc, LocationStackNode next) {
location = loc;
nextNode = next;
}
/** getLocation
* parameters: nothing
* return value:
* Location -- The location object stored within the node object.
*
* This function simply returns a reference to the location object stored
* in the location node object. Otherwise it returns null.
*/
Location getLocation() {
return location;
}
/** getNextNode
* parameters: nothing
* return value:
* LocationStackNode -- A reference to the location node that follows.
*
* This function returns the location node that follows. Otherwise it
* returns null.
*/
LocationStackNode getNextNode() {
return nextNode;
}
/** getNextNode
* parameters:
* next -- A reference to a location node to be set as the next one.
* return value: nothing
*
* This function sets the next location node of a given location node with
* the specified one.
*/
void setNextNode(LocationStackNode next) {
nextNode = next;
}
}