The LinkedList class of Java Basics

The LinkedList class of Java Basics

The LinkedList class extends AbstractSequentialList and implements the List interface. It provides a linked list data structure. It has the following two constructors, which are explained as follows:

LinkedList( ) 
LinkedList(Collection c) 
 

The first constructor creates an empty linked list. The second constructor creates a linked list, which is initialized by the elements in the class set c.

In addition to the methods it inherits, the LinkedList class itself also defines some useful methods, these methods are mainly used to manipulate and access the list. Use the addFirst() method to add elements to the head of the list; use the addLast() method to add elements to the end of the list. Their form is as follows:

void addFirst(Object obj) 
void addLast(Object obj) 
 

Here, obj is the item to be added.

Call the getFirst() method to get the first element. Call the getLast() method to get the last element. Their form is as follows:

Object getFirst( ) 
Object getLast( ) 
 

In order to delete the first element, you can use the removeFirst() method; in order to delete the last element, you can call the removeLast() method. Their form is as follows:

Object removeFirst( ) 
Object removeLast( ) 
 

The following program illustrates several methods supported by LinkedList.

//Demonstrate LinkedList. 
import java.util.*; 
class LinkedListDemo { 
 public static void main(String args[]) { 
 //create a linked list 
 LinkedList ll = new LinkedList(); 
 
 //add elements to the linked list 
 ll.add("F"); 
 ll.add("B"); 
 ll.add("D"); 
 ll.add("E"); 
 ll.add("C"); 
 ll.addLast("Z"); 
 ll.addFirst("A"); 
 ll.add(1, "A2"); 
 System.out.println("Original contents of ll: " + ll); 
 //remove elements from the linked list 
 ll.remove("F"); 
 ll.remove(2); 
 System.out.println("Contents of ll after deletion: " 
 + ll); 
 //remove first and last elements 
 ll.removeFirst(); 
 ll.removeLast(); 
 System.out.println("ll after deleting first and last: " 
 + ll); 
 //get and set a value 
 Object val = ll.get(2); 
 ll.set(2, (String) val + " Changed"); 
 System.out.println("ll after change: " + ll); 
 } 
}
 

The output of the program is as follows:

Original contents of ll: [A, A2, F, B, D, E, C, Z] 
Contents of ll after deletion: [A, A2, D, E, C, Z] 
ll after deleting first and last: [A2, D, E, C] 
ll after change: [A2, D, E Changed, C] 
 

Because LinkedList implements the List interface, call add(Object) to append the item to the end of the list, as the addLast() method does. Use the add(int, Object) form of the add() method to insert the item to the specified position, as in the example of calling add(1, "A2") in the example program.

Notice how the third element in ll is changed by calling the get() and set() methods. In order to obtain the current value of an element, pass and store the subscript value of the element through the get() method. In order to assign a new value to this subscript position, pass the subscript and the corresponding new value through the set() method.