wellnwill Marathahalli Bangalore

LinkedList

LinkedList class object represent the functionality of doubly linkedlist.

LinkedList is slow for searching operation.

LinkedList is fast to insert and delete operation because internally several shift operations are not performed. The object of LinkedList class can be also used to implement the concept of queue, dequeue and Stack.

Doubly Linked List structure

ArrayList LinkedList
represents the functionality of dynamic or resizable array. represents the functionality of doubly Linkedlist.
ArrayList is fast for searching operation because it implements RandomAccess interface. LinkedList is slow for searching operation because it does not implement RandomAccess interface.
ArrayList is slow to insert and delete operation because internally several shift operation are performed LinkedList is fast to insert and delete operation because internally several shift operation are not performed.
We can access data randomly. We cannot access data randomly. We have to traverse to the specific node.

Commonly used constructors of LinkedList class

public LinkedList() :Constructs an empty LinkedList object.

public LinkedList(Collection c) :Constructs a LinkedList object containing the elements of the specified collection.

Commonly used methods of LinkedList class

Commonly used constructors of LinkedList class

public Object getFirst():

This method return the first element of LinkedList. public Object getLast():

This method return the last element of LinkedList.

public Object removeFirst():

This method removes and returns the first element from LinkedList.

public Object removeLast():

This method removes and returns the last element from LinkedList.

public void addFirst(Object element):

This method inserts the specified element at the beginning of LinkedList.

public void addLast(Object element):

This method inserts the specified element at the end of LinkedList.

                                                      
import java.util.*;
class Test240
{
public static void main(String[] args){
LinkedList l=new LinkedList();
l.add(10);
l.add(20);
l.add(30);
l.add("Mac");
l.add(40);
l.add(50);
l.add("RAJ");
l.addFirst("John");
l.addLast("David");
System.out.println("elements :"+l);
System.out.println(l.size());
//remove element from LinkedList
l.remove("RAJ");
//l.remove(20);//it will try to remove the element of 20th index.
//and throw IndexOutOfBoundsException.//l.remove(2);
System.out.println("elements after deletion:"+l);
//remove element from 0th index
l.remove(0);
System.out.println("elements after deletion:"+l);
//remove first and last elements
l.removeFirst();
l.removeLast();
System.out.println(l);
Iterator itr=l.iterator();
while(itr.hasNext()){
	Object obj=itr.next();
	System.out.println(obj);
}
}
}
Footer with Map