-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedStackOfStrings.java
More file actions
64 lines (50 loc) · 1.35 KB
/
LinkedStackOfStrings.java
File metadata and controls
64 lines (50 loc) · 1.35 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
import java.util.Iterator;
import java.util.ListIterator;
public class LinkedStackOfStrings<Item> implements Iterable<Item> {
private Node first = null;
ListIterator<Item> iterator = new ListIterator<>();
private class Node {
Item item;
Node next;
}
private boolean isEmpty() {
return first == null;
}
public void push(Item item) {
Node oldFirst = first;
first = new Node();
first.item = item;
first.next = oldFirst;
}
public Item pop() {
Item item = first.item;
first = first.next;
return item;
}
@Override
public Iterator<Item> iterator() {
// TODO Auto-generated method stub
return iterator;
}
class ListIterator<Item> implements Iterator<Item> {
private Node current = first;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public Item next() {
Item item = (Item) current.item;
current = current.next;
return item;
}
@Override
public void remove() {
try {
throw new IllegalAccessException("Cannot call this method");
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}