• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

LinkedLists




LinkedList It is a predefined class in java.util package. I is used to store elements in the form of nodes. In java Language by default double linked list will be represented by linked class. In the double linked list last node contains 3 blocks out of these address block1 holds address of the previous node address, block2 holds address of next node address and data block holds elements. Linkedlist also accepts any type of heterogeneous elements by supporting duplication.
LinkedList<wrapperclass> objref=new LinkedList<wrapperclass>();         
  
Linkedlist contains following methods.
LinkedList Add Method:
It is used to new element in the new node.
import java.util.*;
class LinkedListAdd
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
al.add(50);
System.out.println("The List Items Are "+al);
}
}


class Main
{
public static void main(String args[])
{
LinkedListAdd d=new LinkedListAdd();
d.list();
}
}

LinkedList AddFirst Method:
It is used to add the element before first node.
import java.util.*;
class LinkedListAddFirst
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
al.addFirst(500);
System.out.println("The List Items Are "+al);
}
}

class Main
{
public static void main(String args[])
{
LinkedListAddFirst d=new LinkedListAddFirst();
d.list();
}
}

LinkedList AddIndex Method
It is used to add the element based on the index value(insertion of the node)
import java.util.*;
class LinkedListAddIndex
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);
al.add(1,1000);
System.out.println(al);
}
}


class Main
{
public static void main(String args[])
{
LinkedListAddIndex d=new LinkedListAddIndex();
d.list();
}
}

LinkedList AddLast Method
It is used to add the new element after the last node.
class LinkedListAddLast
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.addLast(500);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);
}
}

class Main
{
public static void main(String args[])
{
LinkedListAddLast d=new LinkedListAddLast();
d.list();
}
}

Note: by default add method access the last method.
LinkedList Clear Method
It is used ti clear the all elements from all nodes.
import java.util.*;
class LinkedListClear
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);
al.clear();
System.out.println(al);
}
}

class Main
{
public static void main(String args[])
{
LinkedListClear d=new LinkedListClear();
d.list();
}
}

LinkedList GetFirst Method
It is used to get the first node element.
import java.util.*;
class LinkedListGetFirst
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);

System.out.println("The First Item is "+al.getFirst());
}
}

class Main
{
public static void main(String args[])
{
LinkedListGetFirst d=new LinkedListGetFirst();
d.list();
}
}

LinkedList GetIndex Method
It is used to get element based on the index value.
import java.util.*;
class LinkedListGetIndex
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);

System.out.println(al.get(3));
}
}

class Main
{
public static void main(String args[])
{
LinkedListGetIndex d=new LinkedListGetIndex();
d.list();
}
}


LinkedList GetLast Method
It is used to get the last node element.
import java.util.*;
class LinkedListGetLast
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);

System.out.println("The Last Item is "+al.getLast());
}
}

class Main
{
public static void main(String args[])
{
LinkedListGetLast d=new LinkedListGetLast();
d.list();
}
}

LinkedList RemoveFirst Method
It is used to remove first node element.
import java.util.*;
class LinkedListRemoveFirst
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);
al.removeFirst();
System.out.println(al);
}
}

class Main
{
public static void main(String args[])
{
LinkedListRemoveFirst d=new LinkedListRemoveFirst();
d.list();
}
}
LinkedList RemoveIndex Method :
It is used to remove the element based on the index value.
import java.util.*;
class LinkedListRemoveIndex
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);
al.remove(1);
System.out.println(al);
}
}

class Main
{
public static void main(String args[])
{
LinkedListRemoveIndex d=new LinkedListRemoveIndex();
d.list();
}
}

LinkedList RemoveLast Method :
It is used to remove the last node element
import java.util.*;
class LinkedListRemoveLast
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);
al.removeLast();
System.out.println(al);
}
}

class Main
{
public static void main(String args[])
{
LinkedListRemoveLast d=new LinkedListRemoveLast();
d.list();
}
}