Sunday, April 28, 2019

Queue Implementation In Java

Queue In Data Structure



Queue Implementation Using Array In Java

package com.queue.example;
import java.util.*;

class QueueImpl {
 /* int array to store queue elements */
 private int Queue[];

 /* front points to front element in the queue */
 private int front;

 /* rear points to last element in the queue */
 private int rear;

 /* maximum capacity of the queue */
 private int capacity;

 /* current size of the queue */
 private int size;

 /* Constructor */
 public QueueImpl(int n) {
  size = 0;
  Queue = new int[n];
  capacity = n;
  front = -1;
  rear = -1;
 }

 /* Method to check if queue is empty */
 public boolean isEmpty() {
  return front == -1;
 }

 /* Method to check if queue is full */
 public boolean isFull() {
  return front == 0 && rear == capacity - 1;
 }

 /* Method to get the size of the queue */
 public int getSize() {
  return size;
 }

 /* Method to check the front element of the queue */
 public int peek() {
  if (isEmpty()) {
   throw new NoSuchElementException("Underflow Exception");
  }
  return Queue[front];
 }

 // Method to get front of queue
 public int front() {
  if (isEmpty()) {
   return Integer.MIN_VALUE;
  }
  return Queue[front];
 }

 // Method to get rear of queue
 public int rear() {
  if (isEmpty()) {
   return Integer.MIN_VALUE;
  }
  return Queue[rear];
 }

 /* Method to add/insert an element into the queue */
 public void enqueue(int i) {
  if (rear == -1) {
   front = 0;
   rear = 0;
   Queue[rear] = i;
  } else if (rear + 1 >= capacity) {
   throw new IndexOutOfBoundsException("Overflow Exception");
  } else if (rear + 1 < capacity) {
   Queue[++rear] = i;
  }
  size++;
 }

 public int dequeue() {
  int eliminate = front;
  if (!isEmpty()) {
   size--;
   eliminate = Queue[front];
   if (front == rear) {
    front = -1;
    rear = -1;
   } else {
    front++;
   }
   return eliminate;
  } else {
   System.out.println("Queue Underflow");
   // throw new NoSuchElementException("Underflow Exception");
   // return Integer.MIN_VALUE;
  }
  return eliminate;
 }

 /* Method to display the status of the queue */
 public void display() {
  System.out.print("Current elements in queue are :");
  if (size == 0) {
   System.out.print("Empty\n");
   return;
  }
  for (int i = front; i <= rear; i++) {
   System.out.print(Queue[i] + " ");
  }
  System.out.println();
 }
}

/* Class QueueImplement */
public class QueueMainClass {
 public static void main(String[] args) {

  System.out.println("1. insert");
  System.out.println("2. remove");
  System.out.println("3. peek");
  System.out.println("4. check empty");
  System.out.println("5. check full");
  System.out.println("6. size");

  /* create object of class QueueImpl */
  QueueImpl queue = new QueueImpl(5);

  System.out.println("Start Add/insert element into Queue: 100, 50, 25");
  queue.enqueue(100);
  queue.enqueue(50);
  queue.enqueue(25);
  System.out.println("After Add/insert element into Queue.");
  queue.display();
  System.out.println();

  System.out.println("Removed Element = " + queue.dequeue());
  System.out.println("After queue.dequeue() call: ");
  queue.display();
  System.out.println();

  System.out.println("Peek Element = " + queue.peek());
  System.out.println("Empty status = " + queue.isEmpty());
  System.out.println("Full status = " + queue.isFull());
  System.out.println("Size = " + queue.getSize());
  /* display Queue */
  queue.display();
  System.out.println();

  System.out.println("Removed Element = " + queue.dequeue());
  System.out.println("Removed Element = " + queue.dequeue());
  System.out.println("Removed Element = " + queue.dequeue());

 }
}
Output:

1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
Start Add/insert element into Queue: 100, 50, 25
After Add/insert element into Queue.
Current elements in queue are :100 50 25 

Removed Element = 100
After queue.dequeue() call: 
Current elements in queue are :50 25 

Peek Element = 50
Empty status = false
Full status = false
Size = 2
Current elements in queue are :50 25 

Removed Element = 50
Removed Element = 25
Queue Underflow
Removed Element = -1

No comments:

Post a Comment