Monday, 15 February 2016

Data Structures - Java Program to implement queue operations using an array


Queue is a data structure  , similar to Stack , but here in queue we have openings at both the ends , one for insertion and other for deletion.


Some of the common operations of queue are :

  • Adding an item to queue(enqueue)
  • Removing an item from a queue(dequeue)
  • Overflow Condition(Queue full)
  • Underflow Condition(Queue empty)
  • Getting the front element(peek)



Overflow situation occur when the queue is full and then we are trying to  insert/add another item to the queue .

Underflow situation occur when the queue is empty and the we are trying to remove/delete an item from the queue.

The principle of queue is First In First Out (FIFO).



Download code

PROGRAM :

package mycodingcorner;

import java.util.Scanner;

public class Queue_Array {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int rear = -1;
        int front = -1;
        int item;
        System.out.print("Enter Size of Queue : \t ");
        int MAX = scan.nextInt();
        int QueueArray[] = new int[MAX];
        while (true) {
            System.out.println("\nQueue Operations  :");
            System.out.println("1.Insert/Add");
            System.out.println("2. Remove/Delete");
            System.out.println("3. Peek/Front item");
            System.out.println("4. Display Queue");
            System.out.println("5. Exit");
            System.out.print("Enter your choice :\t");
            int choice = scan.nextInt();
            switch (choice) {
                case 1:
                    if (rear == MAX - 1) {
                        System.out.println("Queue Overflow \n");
                    } else {
                        if (front == -1) {
                            front = 0;
                        }
                        System.out.print("Inset the element in queue : \t");
                        item = scan.nextInt();
                        rear = rear + 1;
                        QueueArray[rear] = item;
                    }

                    break;
                case 2:

                    if (front == -1 || front > rear) {
                        System.out.println("Queue Underflow \n");
                        return;
                    } else {
                        System.out.println("Element deleted from queue is " + QueueArray[front] + "\n");
                        front = front + 1;
                    }
                    break;
                case 3:
                    if (front == -1 || front > rear) {
                        System.out.println("Queue Underflow \n");
                        return;
                    } else {
                        System.out.println("The front element is : \t" + QueueArray[front]);
                    }
                    break;
                case 4:
                    if (front == -1 || front > rear) {
                        System.out.println("Queue Underflow \n");
                        return;
                    } else {
                        System.out.print("The current elements in queuue are :");
                        for (int i = front; i <= rear; i++) {
                            System.out.print(QueueArray[i] + " ");
                        }
                    }
                    break;
                case 5:
                    System.exit(0);
                    break;
                default:
                    System.out.println("Wrong Entry \n ");
                    break;
            }

        }
    }
}
Download code

OUTPUT :

Java - Queue Operations


Java - Queue Underflow

Java - Queue Overflow