Skip to content

Commit 23d9eae

Browse files
authored
Merge pull request TheAlgorithms#1069 from shellhub/feature
update SinglyLinkedList
2 parents 6e8aaaa + a5997e8 commit 23d9eae

File tree

1 file changed

+53
-41
lines changed

1 file changed

+53
-41
lines changed

DataStructures/Lists/SinglyLinkedList.java

+53-41
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,16 @@ public void insert(int data) {
5454
* @param data data to be stored in a new node
5555
* @param position position at which a new node is to be inserted
5656
*/
57-
5857
public void insertNth(int data, int position) {
59-
if (position < 0 || position > getSize()) {
60-
throw new IndexOutOfBoundsException("position less than zero or position more than the count of list");
61-
} else {
62-
Node cur = head;
63-
Node node = new Node(data);
64-
for (int i = 0; i < position; ++i) {
65-
cur = cur.next;
66-
}
67-
node.next = cur.next;
68-
cur.next = node;
69-
size++;
58+
checkBounds(position, 0, size);
59+
Node cur = head;
60+
for (int i = 0; i < position; ++i) {
61+
cur = cur.next;
7062
}
63+
Node node = new Node(data);
64+
node.next = cur.next;
65+
cur.next = node;
66+
size++;
7167
}
7268

7369
/**
@@ -90,19 +86,28 @@ public void delete() {
9086
* This method deletes an element at Nth position
9187
*/
9288
public void deleteNth(int position) {
93-
if (position < 0 || position > size - 1) {
94-
throw new IndexOutOfBoundsException("position less than zero or position more than the count of list");
95-
} else {
96-
Node cur = head;
97-
for (int i = 0; i < position; ++i) {
98-
cur = cur.next;
99-
}
100-
101-
Node destroy = cur.next;
102-
cur.next = cur.next.next;
103-
destroy = null; // clear to let GC do its work
104-
105-
size--;
89+
checkBounds(position, 0, size - 1);
90+
Node cur = head;
91+
for (int i = 0; i < position; ++i) {
92+
cur = cur.next;
93+
}
94+
95+
Node destroy = cur.next;
96+
cur.next = cur.next.next;
97+
destroy = null; // clear to let GC do its work
98+
99+
size--;
100+
}
101+
102+
/**
103+
* @param position to check position
104+
* @param low low index
105+
* @param high high index
106+
* @throws IndexOutOfBoundsException if {@code position} not in range {@code low} to {@code high}
107+
*/
108+
public void checkBounds(int position, int low, int high) {
109+
if (position < low || position > high) {
110+
throw new IndexOutOfBoundsException(position + "");
106111
}
107112
}
108113

@@ -134,25 +139,27 @@ public boolean isEmpty() {
134139
return size == 0;
135140
}
136141

137-
/**
138-
* Prints contents of the list
139-
*/
140-
public void display() {
141-
Node current = head.next;
142-
while (current != null) {
143-
System.out.print(current.value + " ");
144-
current = current.next;
145-
}
146-
System.out.println();
147-
}
148-
149142
/**
150143
* Returns the size of the linked list
151144
*/
152145
public int getSize() {
153146
return size;
154147
}
155148

149+
@Override
150+
public String toString() {
151+
if (size == 0) {
152+
return "";
153+
}
154+
StringBuilder builder = new StringBuilder();
155+
Node cur = head.next;
156+
while (cur != null) {
157+
builder.append(cur.value).append("->");
158+
cur = cur.next;
159+
}
160+
return builder.replace(builder.length() - 2, builder.length(), "").toString();
161+
}
162+
156163
/**
157164
* Main method
158165
*
@@ -167,19 +174,24 @@ public static void main(String args[]) {
167174
myList.insertHead(7);
168175
myList.insertHead(10);
169176

170-
myList.display(); // 10 -> 7 -> 5
177+
System.out.println(myList);; // 10 -> 7 -> 5
171178

172179
myList.deleteHead();
173180

174-
myList.display(); // 7 -> 5
181+
System.out.println(myList);; // 7 -> 5
175182

176183
myList.insertNth(11, 2);
177184

178-
myList.display(); // 7 -> 5 -> 11
185+
System.out.println(myList);; // 7 -> 5 -> 11
179186

180187
myList.deleteNth(1);
181188

182-
myList.display(); // 7-> 11
189+
System.out.println(myList);; // 7-> 11
190+
191+
myList.clear();
192+
assert myList.isEmpty();
193+
194+
System.out.println(myList); // ""
183195

184196
}
185197
}

0 commit comments

Comments
 (0)