Java LinkedList class

LinkedList class :

  1.  The underlying data structure is Double Linked List.
  2.  Insertion order is preserved.
  3.  Duplicates are allowed.
  4.  Heterogeneous objects are allowed.
  5.  Null insertion is possible.
  6.  Linked List implements Serializable and cloneable interfaces but not Random Access interfaces.
  7.  Linked List is the best choice if our frequent operation is insertion or deletion in the middle.
  8.  Linked List is the worst choice if our frequent operation is retrieval operation.
  9.  Usually, we can use a linked list to implement stacks and queues to provide support for this requirement Linked List class defines the following specific methods.

Methods:

  • void addFirst();
  • void addLast();
  • Object getFirst();
  • Object getLast();
  • Object removeFirst();
  • Object removeLast();

CONSTRUCTOR:

Creates an empty Linked List object.

LinkedList l1 = new LinkedList();

Creates an equivalent Linked List object for a given collection.

LinkedList l1 = new LinkedList(Collection c);

Example of LinkedList :

import java.util.LinkedList;

public class LinkedListDemo{
	
	public static void main(String arg[]){
		
		LinkedList  l1  =  new  LinkedList();
		l1.add("Preeti");
		l1.add(30);
		l1.add(null);
		l1.add("Preeti");
		l1.set(0,"Software");
		l1.add(0,"venkey");
		l1.addFirst("ccc");
		System.out.println(l1);
	}
}

Output :

[ccc, venkey, Software, 30, null, Preeti]


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.