Repository navigation
Expand file tree
/
Copy pathSortedLinkedList.java
More file actions
198 lines (168 loc) · 5.15 KB
/
Copy pathSortedLinkedList.java
File metadata and controls
198 lines (168 loc) · 5.15 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
/**
* @author Hechie#1558
*/
import java.util.*;
public class SortedLinkedList<E> implements SortedList<E> {
private Node<E> head;
private Comparator<E> comparator;
/**
* Constructor from class SortedLinkedList, sorts based on a comparator
* @param comparator comparator to sort on the insertion
*/
public SortedLinkedList(Comparator<E> comparator) {
this.head = null;
this.comparator = comparator;
}
/**
* Constructor from class SortedLinkedList, sorts based on type's natural order
*/
public SortedLinkedList() {
this.head = null;
this.comparator = (Comparator<E>) Comparator.naturalOrder();
}
/**
* Adds a new element in the SortedLinkedList
* @param elem element to be added in the SortedLinkedList
*/
@Override
public void add(E elem) {
boolean switched = false;
if (head == null) {
head = new NodeClass<>(elem);
}
else {
Node<E> current = head;
Node<E> newNode = new NodeClass<>(elem);
Node<E> previousNode = null;
while (current != null) {
if (comparator.compare(elem, current.getElem()) < 0) {
if (current.equals(head)) {
newNode.link(current);
head = newNode;
switched = true;
break;
}
else {
previousNode.link(newNode);
newNode.link(current);
switched = true;
break;
}
}
previousNode = current;
current = current.next();
}
if (!switched)
previousNode.link(newNode);
}
}
/**
* Obtains an element at the given index
* @param index index of the element
* @return element at the given index / null if index is out of bounds
*/
@Override
public E get(int index) {
Node <E> current = head;
for (int i = 0; current != null; i++) {
if (i == index)
return current.getElem();
current = current.next();
}
return null;
}
/**
* Obtains the index with the first occurence of the element
* @param elem element to search
* @return index with the first occurence of the element / -1 if the element does not exist
*/
@Override
public int indexOf(E elem) {
Node<E> current = head;
for (int i = 0; current != null; i++) {
if (current.getElem().equals(elem))
return i;
current = current.next();
}
return -1;
}
/**
* Obtains the size of the collection
* @return size of the collection
*/
@Override
public int size() {
Node<E> current = head;
int i = 0;
while (current != null) {
current = current.next();
i++;
}
return i;
}
/**
* Removes the element at the given index
* @param index index where the element is stored
* @return true if the element was removed successfully / false otherwise
* @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size())
*/
@Override
public E remove(int index) throws IndexOutOfBoundsException {
Node<E> current = head;
Node<E> previous = null;
Node<E> removed = null;
for (int i = 0; current != null; i++) {
if (i == index && i == 0) {
removed = current;
head = current.next();
return removed.getElem();
}
else if (i == index) {
removed = current;
previous.link(current.next());
return removed.getElem();
}
previous = current;
current = current.next();
}
throw new IndexOutOfBoundsException();
}
/**
* Obtains an iterator for the SortedLinkedList
* @return iterator for the SortedLinkedList
*/
@Override
public Iterator<E> iterator() {
List<E> elems = new ArrayList<>();
Node<E> current = head;
for (int i = 0; current != null; i++) {
elems.add(i, current.getElem());
current = current.next();
}
return elems.iterator();
}
/**
* Auxiliar method to use print on SortedLinkedList
* @return String to be printed
*/
@Override
public String toString() {
StringBuilder elems = new StringBuilder();
Node<E> current = head;
if (head == null)
return null;
else {
elems.append("[");
while (true) {
if (current.next() == null) {
elems.append(current.getElem());
break;
}
elems.append(current.getElem()).append(", ");
current = current.next();
}
elems.append("]");
}
return elems.toString();
}
}