Linked List Example

This section discusses an example to demonstrate the various methods of List interface. We are using two classes ArrayList and LinkedList in the example code.

Linked List Example

This section discusses an example to demonstrate the various methods of List interface. We are using two classes ArrayList and LinkedList in the example code.

Linked List Example

Linked List Example

     

This section discusses an example to demonstrate the various methods of List interface. We are using two classes ArrayList and LinkedList in the example code. The code below is similar to the previous example, but it performs many List operations. Lets discuss the example code.

Description of program:

This program helps you in storing the large amount of data as a collection. The LinkedList is a part of collection that constructs a list containing the elements of the specified collection. Iterator methods returns the values in the order in which they are stored.

If you want to insert the data in the linkedList then use add() method. The hasNext() method returns true if the iterator contains more elements and the next() method returns the next element in the iteration. To insert and remove the data at first, last and specified position in the linkedList, you use the addFirst(), addLast(), add(), removeFirst(), removeLast() and remove() methods. To retrieve the element with respect to a specified position use the getFirst(), getLast() and get() methods.  

Here is the code of program:

import java.util.*;

public class LinkedListExample{
  public static void main(String[] args) {
  System.out.println("Linked List Example!");
  LinkedList <Integer>list = new LinkedList<Integer>();
  int num1 = 11, num2 = 22, num3 = 33, num4 = 44;
  int size;
  Iterator iterator;
  //Adding data in the list
  list.add(num1);
  list.add(num2);
  list.add(num3);
  list.add(num4);
  size = list.size();
  System.out.print"Linked list data: ");  
  //Create a iterator
  iterator = list.iterator()
  while (iterator.hasNext()){
  System.out.print(iterator.next()+" ");  
  }
  System.out.println();
  //Check list empty or not
  if (list.isEmpty()){
  System.out.println("Linked list is empty");
  }
  else{
  System.out.println"Linked list size: " + size);
  }
  System.out.println("Adding data at 1st location: 55");
  //Adding first
  list.addFirst(55);
  System.out.print("Now the list contain: ");
  iterator = list.iterator();
  while (iterator.hasNext()){
  System.out.print(iterator.next()+" ");  
  }
  System.out.println();
  System.out.println("Now the size of list: " + list.size());
  System.out.println("Adding data at last location: 66");
  //Adding last or append
  list.addLast(66);
  System.out.print("Now the list contain: ");
  iterator = list.iterator();
  while (iterator.hasNext()){
  System.out.print(iterator.next()+" ");  
  }
  System.out.println();
  System.out.println("Now the size of list: " + list.size());
  System.out.println("Adding data at 3rd location: 55");
  //Adding data at 3rd position
  list.add(2,99);
  System.out.print("Now the list contain: ");
  iterator = list.iterator();
  while (iterator.hasNext()){
  System.out.print(iterator.next()+" ");  
  }
  System.out.println();
  System.out.println("Now the size of list: " + list.size());
  //Retrieve first data
  System.out.println("First data: " + list.getFirst());
  //Retrieve lst data
  System.out.println("Last data: " + list.getLast());
  //Retrieve specific data
  System.out.println("Data at 4th position: " + list.get(3));
  //Remove first
  int first = list.removeFirst();
  System.out.println("Data removed from 1st location: " + first);
  System.out.print("Now the list contain: ");
  iterator = list.iterator();
  //After removing data
  while (iterator.hasNext()){
  System.out.print(iterator.next()+" ");  
  }
  System.out.println();
  System.out.println("Now the size of list: " + list.size());
  //Remove last
  int last = list.removeLast();
  System.out.println("Data removed from last location: " + last);
  System.out.print("Now the list contain: ");
  iterator = list.iterator();
  //After removing data
  while (iterator.hasNext()){
  System.out.print(iterator.next()+" ");  
  }
  System.out.println();
  System.out.println("Now the size of list: " + list.size());
  //Remove 2nd data
  int second = list.remove(1);
  System.out.println("Data removed from 2nd location: " + second);
  System.out.print("Now the list contain: ");
  iterator = list.iterator();
  //After removing data
  while (iterator.hasNext()){
  System.out.print(iterator.next()+" ");  
  }
  System.out.println();
  System.out.println("Now the size of list: " + list.size());
  //Remove all
  list.clear();
  if (list.isEmpty()){
  System.out.println("Linked list is empty");
  }
  else{
  System.out.println"Linked list size: " + size);
  }
  }
}

Download this example.

Output of program:

C:\vinod\collection>javac LinkedListExample.java

C:\vinod\collection>java LinkedListExample
Linked List Example!
Linked list data: 11 22 33 44
Linked list size: 4
Adding data at 1st location: 55
Now the list contain: 55 11 22 33 44
Now the size of list: 5
Adding data at last location: 66
Now the list contain: 55 11 22 33 44 66
Now the size of list: 6
Adding data at 3rd location: 55
Now the list contain: 55 11 99 22 33 44 66
Now the size of list: 7
First data: 55
Last data: 66
Data at 4th position: 22
Data removed from 1st location: 55
Now the list contain: 11 99 22 33 44 66
Now the size of list: 6
Data removed from last location: 66
Now the list contain: 11 99 22 33 44
Now the size of list: 5
Data removed from 2nd location: 99
Now the list contain: 11 22 33 44
Now the size of list: 4
Linked list is empty

C:\vinod\collection>