@@ -54,20 +54,16 @@ public void insert(int data) {
54
54
* @param data data to be stored in a new node
55
55
* @param position position at which a new node is to be inserted
56
56
*/
57
-
58
57
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 ;
70
62
}
63
+ Node node = new Node (data );
64
+ node .next = cur .next ;
65
+ cur .next = node ;
66
+ size ++;
71
67
}
72
68
73
69
/**
@@ -90,19 +86,28 @@ public void delete() {
90
86
* This method deletes an element at Nth position
91
87
*/
92
88
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 + "" );
106
111
}
107
112
}
108
113
@@ -134,25 +139,27 @@ public boolean isEmpty() {
134
139
return size == 0 ;
135
140
}
136
141
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
-
149
142
/**
150
143
* Returns the size of the linked list
151
144
*/
152
145
public int getSize () {
153
146
return size ;
154
147
}
155
148
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
+
156
163
/**
157
164
* Main method
158
165
*
@@ -167,19 +174,24 @@ public static void main(String args[]) {
167
174
myList .insertHead (7 );
168
175
myList .insertHead (10 );
169
176
170
- myList . display () ; // 10 -> 7 -> 5
177
+ System . out . println ( myList ); ; // 10 -> 7 -> 5
171
178
172
179
myList .deleteHead ();
173
180
174
- myList . display () ; // 7 -> 5
181
+ System . out . println ( myList ); ; // 7 -> 5
175
182
176
183
myList .insertNth (11 , 2 );
177
184
178
- myList . display () ; // 7 -> 5 -> 11
185
+ System . out . println ( myList ); ; // 7 -> 5 -> 11
179
186
180
187
myList .deleteNth (1 );
181
188
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 ); // ""
183
195
184
196
}
185
197
}
0 commit comments