Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – LinkedList

Declaration

 class LinkedList<E>

LinkedList extends AbstractSequentialList class.

It implements the List interface.

It works with linked-list data structure.

Allow duplicate elements.

Elements insertion order is maintained.

List of LinkedList Constructors (CLICK HERE)

List of LinkedList Methods (CLICK HERE)

Non Generics Demo

In non-generic demo, we have used non-generic LinkedList class, constructor & methods. Therefore it can contain any type of elements (heterogeneous elements) as shown in program below:

Example

 import java.util.LinkedList;

 public class LinkedListDemo1 
 {
     public static void main(String args[])
     {
         LinkedList ll = new LinkedList();
         ll.add("A"); // String added
         ll.add(10);  // Integer added
         ll.add('Z');  // Character added 
         ll.add(66.66); // Float added
         System.out.println(ll);
     }
 }

Output

[A, 10, Z, 66.66]

If we want LinkedList must contain elements of same type then we must use generic LinkedList.

Generics Demo

In the program below we have used generic LinkedList that can contain only String type of elements.
If we tried to insert element other than String, we will get compile time error.

Example – I

 import java.util.LinkedList;

 public class LinkedListDemo2 
 {
     public static void main(String args[])
     {
         LinkedList<String> ll = new LinkedList<String>();
         ll.add("A");
         ll.add("B");
         ll.add("XYZ");
         ll.add("D");
     //  ll.add(10);  // Integer value is not allowed
                      // because ll is of type LinkedList<String>
                      // it can't be used to refer to an
                      // object of LinkedList<Integer>
         System.out.println(ll);
     }
 }

Output

[A, B, XYZ, D]

Example – II

 import java.util.LinkedList;

 public class LinkedListDemo3 
 {
     public static void main(String args[]) 
     {
         LinkedList<String> ll = new LinkedList<String>();
         ll.add("A");
         ll.add("B");
         ll.add("C");
         ll.add("D");
         System.out.println(ll);
         ll.set(2, "Z");
         System.out.println(ll);
         ll.addFirst("X");
         System.out.println(ll);
         ll.addLast("Y");
         System.out.println(ll);
         System.out.println(ll.getFirst());
         System.out.println(ll.getLast());
         ll.removeFirst();
         System.out.println(ll);
         ll.removeLast();
         System.out.println(ll);
         LinkedList<String> ll2 = new LinkedList<String>();
         ll2.add("P");
         ll2.add("Q");
         ll2.add("R");
         ll.addAll(ll2);
         System.out.println(ll);
     }
 }

Output

[A, B, C, D]
[A, B, Z, D]
[X, A, B, Z, D]
[X, A, B, Z, D, Y]
X
Y
[A, B, Z, D, Y]
[A, B, Z, D]
[A, B, Z, D, P, Q, R]

Example – III

 import java.util.LinkedList;

 class Employee 
 {
     int emp_id;
     String name;
     double salary;
	
     Employee(int i, String s, double d)
     {
         emp_id = i;
         name = s;
         salary = d;
     }
 }

 class LinkedListDemo4 
 {
     public static void main(String args[])
     {
         Employee e1 = new Employee(2,"DAVID", 65000.98);
         Employee e2 = new Employee(5,"JAMES", 78000.69);
         Employee e3 = new Employee(1,"RICKY", 45987.45);
         Employee e4 = new Employee(9,"JACK", 130006.68);
		
         LinkedList<Employee> ll  = new LinkedList<Employee>();
         ll.add(e1);
         ll.add(e2);
         ll.add(e3);
         ll.add(e4);
		
         for(Employee e:ll)
         {
             System.out.println(e.emp_id+"  "+e.name+"  "+e.salary);
         }
     }
 }

Output

2 DAVID 65000.98
5 JAMES 78000.69
1 RICKY 45987.45
9 JACK 130006.68

 

Leave a reply