File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed
Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -211,6 +211,21 @@ public boolean search(int key) {
211211 return false ;
212212 }
213213
214+ /**
215+ * Return element at special index.
216+ *
217+ * @param index given index of element
218+ * @return element at special index.
219+ */
220+ public int getNth (int index ) {
221+ checkBounds (index , 0 , size - 1 );
222+ Node cur = head ;
223+ for (int i = 0 ; i < index ; ++i ) {
224+ cur = cur .next ;
225+ }
226+ return cur .value ;
227+ }
228+
214229
215230 @ Override
216231 public String toString () {
@@ -251,6 +266,11 @@ public static void main(String[] arg) {
251266 && list .search (1 )
252267 && !list .search (100 );
253268
269+ /* Test get function */
270+ assert list .getNth (0 ) == 10
271+ && list .getNth (2 ) == 5
272+ && list .getNth (4 ) == 1 ;
273+
254274 /* Test delete function */
255275 list .deleteHead ();
256276 list .deleteNth (1 );
You can’t perform that action at this time.
0 commit comments