Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 81887c5

Browse files
author
Ram swaroop
committed
linkedlist remove almost done
1 parent 325ce5e commit 81887c5

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

src/me/ramswaroop/common/LinkedList.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public interface LinkedList<E> {
9595
* Retrieves and removes the head (first element) of this list.
9696
*
9797
* @return
98+
* @throws java.util.NoSuchElementException if this list is empty
9899
*/
99100
E remove();
100101

src/me/ramswaroop/linkedlists/SingleLinkedList.java

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import me.ramswaroop.common.LinkedList;
44

5+
import java.util.NoSuchElementException;
6+
57
import static java.lang.System.out;
68

79
/**
@@ -19,9 +21,9 @@ public class SingleLinkedList<E> implements LinkedList<E> {
1921
@Override
2022
public boolean add(E item) {
2123
Node<E> newNode = new Node<>(item, null);
22-
if (head == null) {
24+
if (head == null) { // list empty
2325
head = newNode;
24-
} else {
26+
} else { // add to the end of list
2527
Node<E> curr = head;
2628
while (curr.next != null) {
2729
curr = curr.next;
@@ -39,23 +41,19 @@ public boolean add(int index, E item) {
3941
if (index == 0) { // add at first
4042
addFirst(item);
4143
} else { // add at any other location
42-
Node<E> curr = head;
43-
int i = 0;
44-
while (i < index - 1) {
45-
curr = curr.next;
46-
i++;
47-
}
48-
Node<E> newNode = new Node<>(item, curr.next);
49-
curr.next = newNode;
44+
Node<E> nodeAtPrevIndex = getPredecessorNode(index);
45+
Node<E> newNode = new Node<>(item, nodeAtPrevIndex.next);
46+
nodeAtPrevIndex.next = newNode;
47+
size++;
5048
}
51-
size++;
5249
return true;
5350
}
5451

5552
@Override
5653
public void addFirst(E item) {
5754
Node<E> newNode = new Node<>(item, head);
5855
head = newNode;
56+
size++;
5957
}
6058

6159
@Override
@@ -80,29 +78,35 @@ public boolean contains(E item) {
8078

8179
@Override
8280
public E get(int index) {
83-
return null;
81+
return getNode(index).item;
8482
}
8583

8684
@Override
8785
public E getFirst() {
88-
return null;
86+
return head.item;
8987
}
9088

9189
@Override
9290
public E getLast() {
93-
return null;
91+
return getNode(size - 1).item;
9492
}
9593

9694
@Override
9795
public E remove() {
96+
isLinkedListEmpty();
97+
9898
E item = head.item;
9999
head = head.next;
100100
return item;
101101
}
102102

103103
@Override
104104
public E remove(int index) {
105-
return null;
105+
isIndexOutOfBounds(index);
106+
107+
Node<E> prevNode = getPredecessorNode(index);
108+
prevNode.next = prevNode.next.next;
109+
return prevNode.next.item;
106110
}
107111

108112
@Override
@@ -112,7 +116,11 @@ public boolean remove(E item) {
112116

113117
@Override
114118
public E set(int index, E item) {
115-
return null;
119+
isIndexOutOfBounds(index);
120+
121+
Node<E> node = getNode(index);
122+
node.item = item;
123+
return node.item;
116124
}
117125

118126
@Override
@@ -135,7 +143,7 @@ public void printList() {
135143
out.println(curr.item + "]");
136144
}
137145

138-
private Node<E> getNode(int index) {
146+
private Node<E> getPredecessorNode(int index) {
139147
isIndexOutOfBounds(index);
140148

141149
Node<E> curr = head;
@@ -147,6 +155,24 @@ private Node<E> getNode(int index) {
147155
return curr;
148156
}
149157

158+
private Node<E> getNode(int index) {
159+
isIndexOutOfBounds(index);
160+
161+
Node<E> curr = head;
162+
int i = 0;
163+
while (i < index) {
164+
curr = curr.next;
165+
i++;
166+
}
167+
return curr;
168+
}
169+
170+
private void isLinkedListEmpty() {
171+
if (head == null) {
172+
throw new NoSuchElementException("LinkedList empty");
173+
}
174+
}
175+
150176
private void isIndexOutOfBounds(int index) {
151177
if (index < 0 && index > size) {
152178
throw new IndexOutOfBoundsException("Index must be less than or equal to: " + size);

0 commit comments

Comments
 (0)