Hi friends,
Here I am writing a way to implement basic LinkedList.
First of all LinkedList has the properties of List interface and
AbstractSequentialList class because it implements List interface and extends
AbstractSequentialList.
Example :-
LinkedList myLinkedList = new LinkedList();
// add elements to the linked list
myLinkedList.add("A");
myLinkedList.add("B");
myLinkedList.add("C");
myLinkedList.add("D");
myLinkedList.add("E");
myLinkedList.addLast("Z");
myLinkedList.addFirst("F");
myLinkedList.add(1, "F9");
System.out.println("Values of linked list > > > > > " + myLinkedList);
// remove elements from the linked list
myLinkedList.remove("A");
myLinkedList.remove(2);
System.out.println("Values of linked list after deleting > > > > > "+ myLinkedList);
// remove first and last elements
myLinkedList.removeSecond();
myLinkedList.removeLast();
System.out.println("Values of linked list ----- " + myLinkedList);
}
}
Output:
Values of linked list > > > > > [F, F9, A, B, C, D, E, Z]
Values of linked list after deleting > > > > > [F, F9, C, D, E, Z]
Values of linked list -----: [F, C, D, E]
0 Comment(s)