-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
163 lines (148 loc) · 4.58 KB
/
LinkedList.java
File metadata and controls
163 lines (148 loc) · 4.58 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
public class LinkedList {
private ListNode head;
private ListNode tail;
private Comparator comparator;
public LinkedList(Comparator comparator) {
head = tail = null;
this.comparator = comparator;
}
public boolean isEmpty() {
return (head==null);
}
public void addToHead(Object item) {
if (isEmpty()) {
head = tail = new ListNode(item);
} else {
head = new ListNode(item, head);
}
}
public void addToTail(Object item) {
if (isEmpty()) {
head = tail = new ListNode(item);
} else {
tail.next = new ListNode(item);
tail = tail.next;
}
}
public Object removeFromHead() throws EmptyListException {
Object item = null;
if (isEmpty()) {
throw new EmptyListException();
}
item = head.data;
if (head == tail) // there's only one single node
head = tail = null;
else
head = head.next;
return item;
}
public Object removeFromTail() throws EmptyListException {
if (isEmpty()) {
throw new EmptyListException();
}
Object item = tail.data;
if (head == tail) { // there is only one node
head = tail = null;
return item;
}
ListNode current = head;
while (current.next != tail)
current = current.next;
tail = current;
tail.next = null;
return item;
}
public String toString() {
String s = "[ ";
ListNode current = head;
while (current != null) {
s += current.data + " ";
current = current.next;
}
return s + "]";
}
public void println() {
ListNode current = head;
while (current != null) {
System.out.println(current.data);
current = current.next;
}
}
public void insertInOrder (Object item) {
if (isEmpty()) {
head = tail = new ListNode (item);
} else {
if (comparator.isGreaterThanOrEqualTo(head.data, item)) {
addToHead(item);
} else if (comparator.isLessThanOrEqualTo(tail.data, item)) {
addToTail(item);
} else {
// insert in the middle
ListNode current = head;
while (current.next != null) {
if (comparator.isGreaterThanOrEqualTo(current.next.data, item)) {
ListNode newNode = new ListNode(item);
newNode.next = current.next;
current.next = newNode;
return;
}
current = current.next;
}
}
}
}
public void insertToken (TokenData item) {
TokenData oldItem = null;
boolean exist=true;
try {
oldItem = (TokenData)removeItem(item);
}
catch (ItemNotFoundException ex) {
oldItem = item;
exist=false;
}
finally {
if(exist)
oldItem.list.insertInOrder(item.list.head.data); // update line to exist item
insertInOrder(oldItem);
}
}
public boolean exist(Object item) {
if(isEmpty())
return false;
if (comparator.isEqualTo(head.data, item))
return true;
else if (comparator.isEqualTo(tail.data, item))
return true;
ListNode current = head;
while (current.next != null) {
if (comparator.isEqualTo(current.next.data, item)) {
return true;
}
current = current.next;
}
return false;
}
public Object removeItem (Object item) throws ItemNotFoundException {
if (isEmpty()) {
throw new ItemNotFoundException();
}
if (comparator.isEqualTo(head.data, item))
return removeFromHead();
else if (comparator.isEqualTo(tail.data, item))
return removeFromTail();
else {
// remove a node in the middle
ListNode current = head;
while (current.next != null) {
if (comparator.isEqualTo(current.next.data, item)) {
Object result = current.next.data;
current.next = current.next.next;
return result;
}
current = current.next;
}
throw new ItemNotFoundException();
}
}
}