generated from ZipCodeCore/OldTexasCode2
-
Notifications
You must be signed in to change notification settings - Fork 11
/
LinkedList.java
147 lines (127 loc) · 3.71 KB
/
LinkedList.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
package Fall0811;
import java.util.Iterator;
import Summer08.Node;
public class LinkedList implements Iterable {
private Node head;
private Node tail;
private int size;
public Iterator iterator(){
return new LLIterator();
}
private class LLIterator implements Iterator{
private Node nextNode;
private boolean removeOK;
private int posToRemove;
private LLIterator(){
nextNode = head;
removeOK = false;
posToRemove = -1;
}
public boolean hasNext(){
return nextNode != null;
}
public Object next(){
assert hasNext();
Object result = nextNode.getData();
nextNode = nextNode.getNext();
removeOK = true;
posToRemove++;
return result;
}
public void remove(){
assert removeOK;
removeOK = false;
LinkedList.this.remove(posToRemove);
posToRemove--;
}
}
public void makeEmpty(){
// let GC do its job!!!!!!!
head = tail = null;
size = 0;
}
public Object remove(int pos){
assert pos >= 0 && pos < size;
Object result;
if( pos == 0 ){
result = head.getData();
head = head.getNext();
if( size == 1 )
tail = null;
}
else{
Node temp = head;
for(int i = 1; i < pos; i++)
temp = temp.getNext();
result = temp.getNext().getData();
temp.setNext( temp.getNext().getNext() );
if( pos == size - 1)
tail = temp;
}
size--;
return result;
}
public Object get(int pos){
assert pos >= 0 && pos < size;
// array based list
// return myCon[pos]
Object result;
if( pos == size - 1 )
result = tail.getData(); //O(1)
else{
Node temp = head;
for(int i = 0; i < pos; i++)
temp = temp.getNext();
result = temp.getData();
// average case O(N) :((((
}
return result;
}
public void insert(int pos, Object obj){
assert pos >= 0 && pos <= size;
// addFirst?
if(pos == 0)
addFirst(obj); // O(1)
// add last?
else if( pos == size )
add(obj); //at end O(1)
else{
// general case
Node temp = head;
for(int i = 1; i < pos; i++)
temp = temp.getNext();
// I know temp is pointing at the
// node at position pos - 1
Node newNode = new Node(obj, temp.getNext());
temp.setNext( newNode );
size++;
}
}
public void add(Object obj){
Node newNode = new Node(obj, null);
if( size == 0 )
head = newNode;
else
tail.setNext(newNode);
tail = newNode;
size++;
}
public void addFirst(Object obj){
if(size == 0)
add(obj);
else{
Node newNode = new Node(obj, head);
head = newNode;
size++;
}
}
public String toString(){
String result = "";
Node temp = head;
for(int i = 0; i < size; i++){
result += temp.getData() + " ";
temp = temp.getNext();
}
return result;
}
}