Queue Implementation using java
Tags: java, productivity, beginners, algorithms
Published: 2022-03-05 11:32:37

Hello guys today I am going to show you the implementation of Queue in Java. I was reading the FreeCodeCamp tutorial of Queue and i find it very useful and Straight forward tutorial so , i am discussing that with all of you 
Let's get started...

What is Queue?
Queue is a linear data structure that follows the first-in-first-out sequence. It means that the item which will be inserted first will be the one removed out first or you can say the items are removed in the order they are inserted.

The Queue consist of Two parts namely Front ( Where the items are removed) and Back( where the items are inserted).

Common Operations of a Queue
The following operations are commonly used in a queue:

Enqueue: Adds an item from the back of the queue.
Dequeue: Removes an item from the front of the queue.
Front/Peek: Returns the value of the item in front of the queue without dequeuing (removing) the item.
IsEmpty: Checks if the queue is empty.
IsFull: Checks if the queue is full.
Display: Prints all the items in the queue.

## Code implementation -
```java
public class Example {

public static void main(String[] args) {
    Queue myQueue = new Queue();
    
    myQueue.enQueue(3);
    myQueue.enQueue(2);
    myQueue.enQueue(1);
    myQueue.display();
    myQueue.deQueue();
    myQueue.peak();
    
}
}
class Queue {
  
  int queueLength = 3;
  int items[] = new int[queueLength];
  int front = -1; 
  int back = -1;
  
  boolean isFull(){
      if(back == queueLength - 1){
          return true;
      } else {
          return false;
      }
  }
  
  boolean isEmpty(){
      if(front == -1 && back == -1){
          return true;
      } else {
          return false;
      }
  }

   void enQueue(int itemValue) {
      if(isFull()){
          System.out.println("Queue is full");
      } else if(front == -1 && back == -1){
          front = back = 0;
          items[back] = itemValue;
      } else{
          back++;
          items[back] = itemValue;
      }
  }
   void deQueue(){
      if(isEmpty()){
          System.out.println("Queue is empty. Nothing to dequeue");
      } else if (front == back){
          front = back = -1;
      } else {
          front++;
      }
  }
  void display(){
      int i;
     
      if(isEmpty()){
          System.out.println("Queue is empty");
      } else {
          for(i = front; i <= back; i++){
              System.out.println(items[i]);
          }
      }
  }
  void peak(){
      System.out.println("Front value is: " + items[front]);
  }
}
```

## Explaination
* First We have created our variables and their parameters. We are using 3 as the maximum number of items that can be enqueued in the array. we have set the initial index of the front and back to -1.
* Next, we'll define the isEmpty and isFull functionalities.
* isEmpty () method is quite simple , for the isFull() method , our maximum number of items allowed in the array is 3 but three items in an array is not denoted by index 3 but 2 since the first index is 0. So maximum length minus 1 gives us index 2 which is the third cell in an array.When all the cells have been enqueued with a value up to the third cell, the array is full.

* enQueue- If the array is full then we get a message saying it is full. If the front and back is -1 then the item is assigned to the first cell which is index 0 – otherwise, the value is inserted and the back position is incremented.

* deQueue- if the array is empty, we get the corresponding message. If the front has met the back, we reset their index back to -1. If the last two conditions are not applicable, then the front is incremented.

* display- if the array is not empty, we loop through and print all the items.
* peak- This simply prints the value of the front item.

Thats it for this post.
THANK YOU FOR READING THIS POST AND IF YOU FIND ANY MISTAKE OR WANTS TO GIVE ANY SUGGESTION , PLEASE MENTION IT IN THE COMMENT SECTION.
^^You can help me by some donation at the link below Thank you👇👇 ^^
☕ --> https://www.buymeacoffee.com/waaduheck <--

Also check these posts as well
https://dev.to/shubhamtiwari909/higher-order-function-in-javascript-1i5h

https://dev.to/shubhamtiwari909/styled-componenets-react-js-15kk

https://dev.to/shubhamtiwari909/introduction-to-tailwind-best-css-framework-1gdj