2
2
3
3
import me .ramswaroop .common .LinkedList ;
4
4
5
+ import java .util .NoSuchElementException ;
6
+
5
7
import static java .lang .System .out ;
6
8
7
9
/**
@@ -19,9 +21,9 @@ public class SingleLinkedList<E> implements LinkedList<E> {
19
21
@ Override
20
22
public boolean add (E item ) {
21
23
Node <E > newNode = new Node <>(item , null );
22
- if (head == null ) {
24
+ if (head == null ) { // list empty
23
25
head = newNode ;
24
- } else {
26
+ } else { // add to the end of list
25
27
Node <E > curr = head ;
26
28
while (curr .next != null ) {
27
29
curr = curr .next ;
@@ -39,23 +41,19 @@ public boolean add(int index, E item) {
39
41
if (index == 0 ) { // add at first
40
42
addFirst (item );
41
43
} 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 ++;
50
48
}
51
- size ++;
52
49
return true ;
53
50
}
54
51
55
52
@ Override
56
53
public void addFirst (E item ) {
57
54
Node <E > newNode = new Node <>(item , head );
58
55
head = newNode ;
56
+ size ++;
59
57
}
60
58
61
59
@ Override
@@ -80,29 +78,35 @@ public boolean contains(E item) {
80
78
81
79
@ Override
82
80
public E get (int index ) {
83
- return null ;
81
+ return getNode ( index ). item ;
84
82
}
85
83
86
84
@ Override
87
85
public E getFirst () {
88
- return null ;
86
+ return head . item ;
89
87
}
90
88
91
89
@ Override
92
90
public E getLast () {
93
- return null ;
91
+ return getNode ( size - 1 ). item ;
94
92
}
95
93
96
94
@ Override
97
95
public E remove () {
96
+ isLinkedListEmpty ();
97
+
98
98
E item = head .item ;
99
99
head = head .next ;
100
100
return item ;
101
101
}
102
102
103
103
@ Override
104
104
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 ;
106
110
}
107
111
108
112
@ Override
@@ -112,7 +116,11 @@ public boolean remove(E item) {
112
116
113
117
@ Override
114
118
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 ;
116
124
}
117
125
118
126
@ Override
@@ -135,7 +143,7 @@ public void printList() {
135
143
out .println (curr .item + "]" );
136
144
}
137
145
138
- private Node <E > getNode (int index ) {
146
+ private Node <E > getPredecessorNode (int index ) {
139
147
isIndexOutOfBounds (index );
140
148
141
149
Node <E > curr = head ;
@@ -147,6 +155,24 @@ private Node<E> getNode(int index) {
147
155
return curr ;
148
156
}
149
157
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
+
150
176
private void isIndexOutOfBounds (int index ) {
151
177
if (index < 0 && index > size ) {
152
178
throw new IndexOutOfBoundsException ("Index must be less than or equal to: " + size );
0 commit comments