-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
62 lines (48 loc) · 1.07 KB
/
Copy pathStack.java
File metadata and controls
62 lines (48 loc) · 1.07 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
public class Stack<T> implements GenericStack<T> {
private Node<T> top = null;
public boolean isEmpty() { return top == null; }
public void push (T t) {
Node<T> n = new Node<T>(t, top);
top = n;
}
public T pop () {
if (top == null)
return null;
Node<T> n = top;
top = n.getNext();
return n.getValue();
}
public T peek () {
if (top == null)
return null;
return top.getValue();
}
public String toString () {
String str = "[";
Node<T> n = top;
while (n != null) {
str += n.getValue().toString();
if (n.hasNext()) {
str += ", ";
}
n = n.getNext();
}
str += "]";
return str;
}
}
class Node<T> {
private T value;
private Node<T> next = null;
public Node (T value) {
this.value = value;
}
public Node (T value, Node<T> next) {
this.value = value;
this.next = next;
}
public T getValue () { return value; }
public Node<T> getNext () { return next; }
public boolean hasNext () { return next != null; }
public void setNext (Node<T> next) { this.next = next; }
}