-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnhancedStack.java
More file actions
267 lines (235 loc) · 5.45 KB
/
EnhancedStack.java
File metadata and controls
267 lines (235 loc) · 5.45 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import java.util.EmptyStackException;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* EnhancedStack.java
* A generic class, beside keeping a reference to the top of the stack,
* we also keep a reference to the smallest element in the stack.
* Each stack member keeps a reference to the smallest element
* in the stack when it was added.
*
* @author Huy Tu
* @version 2/21/2014
*
* @param <T>
* The container for the data in the node.
*/
public class EnhancedStack <T extends Comparable<?super T>>
implements Iterable<T>
{
private Node<T> top;
private Node<T> minimum;
private int size;
/**
* EnhancedStack constructor creates a stack
* while initialize minimum, top, and size variables.
*/
public EnhancedStack()
{
top = null;
minimum = null;
size = 0;
}
/**
*
* @return the top element.
*/
public T top()
{
if (top == null)
{
throw new NullPointerException();
}
else
{
return top.getData();
}
}
/**
* @return the number of elements on the Stack
*/
public int size()
{
return size;
}
/**
*
* @return if the stack is empty.
*/
public boolean isEmpty()
{
return (top == null);
}
/**
*
* @return data of the minimum node.
*/
public T minimum()
{
return minimum.getData();
}
/**
* Adds the data item to the top of the stack.
* @param data Command Line Argument
*/
public void push(T data)
{
if (size == 0)
{
top = new Node<T>(data, null, null);
minimum = top;
}
else
{
top = new Node<T>(data, top, minimum);
if (top.getData().compareTo(minimum.getData()) < 0)
{
minimum = top;
}
}
size++;
}
/**
* Return the element on top of the stack
* While popping that element out.
* @return Command Line Argument
*/
public T pop()
{
T answer;
if (top == null)
{
throw new EmptyStackException();
}
answer = top.getData();
if (top == minimum)
{
minimum = top.small;
}
top = top.getLink();
size--;
return answer;
}
/**
* Implement iterator for the stack.
* @return Command Line Argument
*/
public Iterator<T> iterator()
{
// TODO Auto-generated method stub
return (Iterator<T>) new Tools<T>(top);
}
/**
* Node class to create node element in the stack.
*
* @author Huy Tu
* @param <T> The container for the data in the node.
*/
private static class Node<T>
{
public T data;
public Node<T> link;
public Node<T> small;
/**
* Constructor takes a data element and node reference.
*
* @param data
* The data for this node.
* @param link
* The next node in the list.
* @param small
* The smallest node in the list.
*/
public Node(T data, Node<T> link, Node<T> small)
{
this.data = data;
this.link = link;
this.small = small;
}
/**
* Changes the link of this node.
*
* @param newLink
* The new link.
*/
public void setLink(Node<T> newLink)
{
link = newLink;
}
/**
* Changes the data for this node.
*
* @param newData
* The new data.
*/
public void setData(T newData)
{
data = newData;
}
/**
* @return the link.
*/
public Node<T> getLink()
{
return link;
}
/**
* @return the data.
*/
public T getData()
{
return data;
}
}
/**
* Inner iterator class for the Enhanced Stack.
*
* @author Huy Tu
* @param <T> The container for the data in the node.
*/
private class Tools<T> implements Iterator<T>
{
Node<T> cursor;
/**
* Constructor for inner Class Tools<E>.
* @param head Command Line Argument
*/
private Tools(Node<T> head)
{
cursor = head;
}
/**
* @return Command Line Argument
* Return the next node.
*/
public T next()
{
if (!hasNext())
{
throw new NoSuchElementException("");
}
else
{
T node = cursor.getData();
cursor = cursor.getLink();
return node;
}
}
/**
* @return Command Line Argument
* Check if there is a next element of the current node.
*/
public boolean hasNext()
{
return (cursor != null);
}
/**
* Remove the current element,
* Throw UnsupportedOperationException().
*/
public void remove()
{
throw new UnsupportedOperationException();
}
}
}