forked from ows-ali/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
211 lines (189 loc) · 4.54 KB
/
LinkedList.java
File metadata and controls
211 lines (189 loc) · 4.54 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Data Structures and Algorithms.
* Copyright (C) 2016 Rafael Guterres Jeffman
*
* See the LICENSE file accompanying this source code, for
* licensing restrictions that might apply.
*
*/
package datastructures;
import java.util.Comparator;
import interfaces.BidirectionalIterator;
import interfaces.Iterator;
import interfaces.Sortable;
/**
* A Double Linked List implementation, which can be iterated with a
* bidirectional iterator, and have its elements sorted.
*/
public class LinkedList<T> implements Iterable<T>, Sortable<T> {
class Node {
T data;
public Node previous;
public Node next;
Node(T data) {
this.data = data;
}
void insert(Node before, Node after) {
this.previous = before;
this.next = after;
if (before != null)
before.next = this;
if (after != null)
after.previous = this;
}
void remove() {
if (previous != null)
previous.next = next;
if (next != null)
next.previous = previous;
next = null;
previous = null;
}
}
class ListaIterator implements BidirectionalIterator<T> {
LinkedList<T> container;
Node current;
public ListaIterator(LinkedList<T> container) {
this.container = container;
this.current = container.head;
}
@Override
public boolean hasNext() {
return current != null;
}
@Override
public boolean hasPrevious() {
return current == null || current.previous != null;
}
@Override
public T next() {
T data = current.data;
current = current.next;
return data;
}
@Override
public T previous() {
if (current == null)
current = container.tail;
else
current = current.previous;
return current.data;
}
@Override
public void insert(T data) {
if (current == head)
throw new IllegalStateException("Usage before next().");
Node novo = new Node(data);
Node old, prev;
if (current == null) {
old = tail;
prev = tail.previous;
} else {
old = current.previous;
prev = old.previous;
}
novo.insert(prev, old);
if (prev == null)
head = novo;
}
@Override
public void append(T data) {
if (head == null) {
String error_msg = "Cannot use iterator on an empty list.";
throw new IllegalStateException(error_msg);
}
Node novo = new Node(data);
if (current == null) {
current = tail;
}
if (current.next == null)
tail = novo;
novo.insert(current, current.next);
current = novo;
}
@Override
public void remove() {
if (current == head)
throw new IllegalStateException("Usage before next().");
if (tail == head || current == null) {
Node prev = tail.previous;
tail.remove();
if (tail == head)
head = prev;
tail = prev;
return;
}
Node next = current;
current = current.previous;
if (current == head)
head = current.next;
current.remove();
current = next;
}
}
private Node head;
private Node tail;
/**
* Check if the container is empty.
* @return True if there are no elements stored, false otherwise.
*/
public boolean isEmpty() {
return head == null;
}
/**
* Add a new element to the end of the structure.
* @param data The element to be added.
*/
public void append(T data) {
Node novo = new Node(data);
if (tail == null) {
head = novo;
} else {
novo.previous = tail;
tail.next = novo;
}
tail = novo;
}
/**
* Returns a bidirectional iterator to the LinkedList.
*/
@Override
public Iterator<T> iterator() {
return new ListaIterator(this);
}
/**
* Sort the elements in the container using insertion sort.
* @param cmp The comparator to be used to sort the elements.
*/
@Override
public void sort(Comparator<T> cmp) {
if (head == null) return;
Node novo = head.next;
while (novo != null) {
Node current = novo.previous, next = novo.next;
while (current!=null && cmp.compare(current.data, novo.data) > 0)
current = current.previous;
// swap nodes
swapNodes(novo, current);
novo = next;
}
}
private void swapNodes(Node novo, Node current) {
if (current != null && current.next == novo)
return;
Node previous = novo.previous;
Node next = novo.next;
previous.next = next;
// remove novo
if (novo == tail)
tail = previous;
novo.remove();
// Insere depois do current.
if (current != null) {
novo.insert(current,current.next);
} else {
novo.insert(null,head);
head = novo;
}
}
}