Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz

Article

Java Queue in Depth

47 sec read

What is Queue ?

A queue or FIFO (first in, first out) is an abstract data type that serves as a collection of elements, with two principal operations: enqueue, the process of adding an element to the collection.(The element is added from the rear side) and dequeue, the process of removing the first element that was added. (The element is removed from the front side). It can be implemented by using both array and linked list.

Example :-

import java.util.*;

public class JavaStackDemo {
  public static void main(String[] args)
  {
  //Create an empty Stack
  Stack stack = new Stack();
  
  System.out.println("After Push Operation on Stack :");
  stack.push("first");
  printStackElements(stack);
  stack.push("second");
  printStackElements(stack);
  stack.push("third");
  printStackElements(stack);
  
  System.out.println("
After Pop Operation on Stack :");
  stack.pop();
  printStackElements(stack);
  stack.pop();
  printStackElements(stack);
  stack.pop();
  printStackElements(stack);
  
  
  }
  
  private static void printStackElements(Stack stack)
  {
    if(stack.isEmpty())
    {
      System.out.println("No more items left on the stack");
    }
    else
    {
      System.out.printf("%s
", stack);
    }
  }
}

Output :-

[first, second, third] 
[second, third] 
second 

Recommended Articles

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor