-
Notifications
You must be signed in to change notification settings - Fork 0
/
SinglyLinkedList.java
197 lines (167 loc) · 4.99 KB
/
SinglyLinkedList.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
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
package dsa_dp.data_structures.linked_list;
public class SinglyLinkedList<T> {
Node head = null;
int size = 0;
/**
* Node item class,only accessible from the singlyLinkedList class
*/
private class Node {
public T data;
public Node next;
public Node(T data) {
this.data = data;
this.next = null;
}
public Node(T data, Node next) {
this.data = data;
this.next = next;
}
}
/**
* Adds a new node to the end of the LinkedList, returns true if it succeeds, false otherwise
* If it's the first element, it sets it as the LinkedList 'head'
*
* @param data T
* @return boolean
*/
public boolean add(T data) {
if (this.head == null) {
this.addFirst(data);
return true;
}
Node node = this.head;
try {
while (node.next != null) {
node = node.next;
}
node.next = new Node(data);
this.size++;
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* Adds the given element at the first position of the LinkedList
*
* @param data T
*/
public void addFirst(T data) {
this.head = new Node(data, this.head);
this.size++;
}
/**
* Adds a node at the given index of the LinkedList
* If the LinkedList is empty the Node will be set as the head
* In case the specified index is out of range it throws an IndexOutOfBoundsException
*
* @param index int
* @param data T
*/
public void insert(int index, T data) throws IndexOutOfBoundsException {
checkIfIndexIsViable(index);
Node currentNode = this.head;
Node prevNode = null;
for (int i = 0; i < index; i++) {
prevNode = currentNode;
currentNode = currentNode.next;
}
assert prevNode != null;
// Here the new Node is created.
// The previous node will point to the new Node while the new node
// will point to the node that the previous node was pointing to before.
// Sorry for the pun :)
prevNode.next = new Node(data, currentNode);
this.size++;
}
/**
* Returns the Object at the given index in the LinkedList, if the index
* is out of range an IndexOutOfBoundsException is thrown
*
* @param index int
* @return T
* @throws IndexOutOfBoundsException out of range index
*/
public T get(int index) throws IndexOutOfBoundsException {
checkIfIndexIsViable(index);
Node currentNode = this.head;
for (int i = 0; i < index; i++) {
currentNode = currentNode.next;
}
return currentNode.data;
}
/**
* Updates the object at the specified index with the given value
*
* @param index int
* @param data T
*/
public void set(int index, T data) throws IndexOutOfBoundsException {
checkIfIndexIsViable(index);
Node currentNode = this.head;
for (int i = 0; i < index; i++) {
currentNode = currentNode.next;
}
currentNode.data = data;
}
/**
* Removes the object at the given index of the LinkedList.
*
* @param index int
* @throws IndexOutOfBoundsException out of range index
*/
public void remove(int index) throws IndexOutOfBoundsException {
checkIfIndexIsViable(index);
// Handling the head removing case
if (index == 0) {
this.head = this.head.next;
this.size--;
return;
}
Node currentNode = this.head;
Node prevNode = currentNode;
for (int i = 0; i < index; i++) {
prevNode = currentNode;
currentNode = currentNode.next;
}
prevNode.next = currentNode.next;
currentNode = null; // Unsetting the removed element
this.size--;
}
/**
* Prints the LinkedList in a suitable format
* O(n)
*/
public void print() {
if (this.size > 0) {
Node node = this.head;
StringBuilder sb = new StringBuilder(this.size);
sb.append("[");
while (node != null) {
sb.append(node.data).append(",");
node = node.next;
}
sb.deleteCharAt(sb.length() - 1).append("]");
System.out.println(sb);
} else {
System.out.println("[]");
}
}
/**
* Returns the current size of the LinkedList
*
* @return int
*/
public int size() {
return this.size;
}
/**
* Throws an IndexOutOfBoundsException if the given index is out of the list boundaries
*
* @param index int
*/
private void checkIfIndexIsViable(int index) {
if (index > this.size - 1 || index < 0) throw new IndexOutOfBoundsException();
}
}