Difference Between a Static Queue and a Singly Linked List in Java



In Java List and Queue both are introduced as an ordered list of objects, where the same object may be added more than once. The difference between both comes in the manner of adding elements. In the queue, all the elements get inserted at the rear and removed from the front while we can add an element anywhere in the list.

Sr. No. Key Static Queue Singly Linked List
1 Data initialization. Static Queue works in first out(FIFO) fashion as all the elements get inserted at the REAR and removed from the FRONT of the queue. In the case of Singly Linked List, one can add elements anywhere in the list and also can get any element based on its index in the list.
2 Internal implementation. Internally queue has implemented array which makes it faster for searching and addition of elements. On the other hand, list maintains node and pointers for storing data and address of the next node
3 Size Statis queue is fixed in size and could not be altered. The size of queue is defined at the time of its declaration. List does not have fixed size and so no size is required at the time of declaration of list.
4 Performance The static queue is faster in case of searching for an element while the deletion of an element is slower because deletion requires shifting of all the remaining elements to the front by one position. The list is slower in searching due to its node-based implementation but deletion is faster as the only the address of pointer of one node is need to update instead of updating in the whole list.
5 Data migration. The static queue is always based on First in first out (FIFO) technique. On the other hand List can be FIFO or Last in First out (LIFO).

Example of Static queue and Singly Linked List.

StaticQueueDemo.java

 Live Demo

import java.util.*;
public class StaticQueueDemo {
   public static void main(String args[]) {
      PriorityQueue < String > queue = new PriorityQueue < String > ();
      queue.add("Jack");
      queue.add("Smith");
      queue.add("John");
      System.out.println("head:" + queue.element());
      System.out.println("head:" + queue.peek());
      System.out.println("iterating the queue elements:");
      Iterator itr = queue.iterator();
      while (itr.hasNext()) {
         System.out.println(itr.next());
      }
      queue.remove();
      queue.poll();
      System.out.println("after removing two elements:");
      Iterator < String > itr2 = queue.iterator();
      while (itr2.hasNext()) {
         System.out.println(itr2.next());
      }
   }
}

Output

head:Jack
head:Jack
iterating the queue elements:
Jack
Smith
John
after removing two elements:
Smith

Example

LinkedListDemo.java

 Live Demo

import java.util.*;
public class LinkedListDemo {
   public static void main(String[] args) {
      // Creating a LinkedList
      LinkedList < String > friends = new LinkedList <> ();
      // Adding new elements to the end of the LinkedList using add() method.
      friends.add("John");
      friends.add("Jack");
      friends.add("Smith");
      friends.add("Chris");
      System.out.println("Initial LinkedList : " + friends);
      // Adding an element at the specified position in the LinkedList
      friends.add(3, "Lisa");
      System.out.println("After add(3, \"Lisa\") : " + friends);
   }
}

Output

Initial LinkedList : [John, Jack, Smith, Chris]
After add(3,"Lisa") : [John, Jack, Smith, Chris, Lisa]
Updated on: 2019-09-16T11:10:29+05:30

813 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements