Delete target might be a little tricky because (in general) it must unlink a node from the middle of the list and then link the resulting two parts together.
Here is a brief testing program. A thorough testing program would test each case of each method.
public class LinkedListTester { public static void main( String[] args ) { // create a linked list LinkedList list = new LinkedList(); // insert some nodes list.insertFirst( 99 ); list.insertFirst( 77 ); list.insertFirst( 55 ); list.insertFirst( 11 ); // traverse the list list.traverse(); System.out.println(); list.insertLast( 5 ); list.insertLast( 6 ); list.insertLast( 7 ); list.insertLast( 8 ); // traverse the list list.traverse(); System.out.println(); // delete some nodes list.deleteFirst(); list.deleteLast(); // traverse the list list.traverse(); System.out.println(); // get some values System.out.println("value at 1: " + list.get(1) ); System.out.println("value at 5: " + list.get(5) ); System.out.println("value at 7: " + list.get(7) ); } }
Here is example output:
PS C:\JavaSource> dir *.java Directory: C:\CAI\java9Lessons\chap131\Private Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 7/4/2019 4:59 PM 2667 LinkedList.java -a---- 7/4/2019 4:43 PM 975 LinkedListTester.java -a---- 7/4/2019 3:34 PM 401 Node.java PS C:\JavaSource> javac LinkedListTester.java PS C:\JavaSource> java LinkedListTester 11, 55, 77, 99, 11, 55, 77, 99, 5, 6, 7, 8, 55, 77, 99, 5, 6, 7, value at 1: 77 value at 5: 7 Exception in thread "main" java.lang.IndexOutOfBoundsException at LinkedList.get(LinkedList.java:128) at LinkedListTester.main(LinkedListTester.java:38) PS C:\JavaSource>
What happened at the end? Why was an Exception
thrown?