blob: e58ea6d613b29a10f1ea4a74b3c376983e0036bd (
plain)
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
|
import java.util.NoSuchElementException;
public class SListIterator<T>
{
private Node firstNode;
private int numberOfEntries;
public SListIterator()
{
firstNode = null;
numberOfEntries = 0;
}
public void addToFirst(T aData)
{
firstNode = new Node(aData, firstNode);
numberOfEntries++;
}
public T getEntry(int givenPosition)
{
T result = null;
if((givenPosition >= 1) && (givenPosition <= numberOfEntries))
{
result = (getNodeAt(givenPosition)).data;
}
return result;
}
private Node getNodeAt(int givenPosition)
{
Node currentNode = firstNode;
for(int counter = 1; counter < givenPosition; counter++)
{
currentNode = currentNode.next;
}
return currentNode;
}
public Iterator<T> getIterator()
{
return new IteratorForSList();
}
private class IteratorForSList implements Iterator<T>
{
Node node;
private IteratorForSList()
{
node = firstNode;
}
public boolean hasNext()
{
return node != null;
}
public T next()
{
T temp = node.data;
node = node.next;
return temp;
}
public T remove()
{
throw new UnsupportedOperationException("remove() is not supported by this iterator");
}
}
private class Node
{
private T data;
private Node next;
private Node(T aData, Node nextNode)
{
data = aData;
next = nextNode;
}
}
}
|