0% found this document useful (0 votes)
14 views

Queue

This Java code defines a Queue class that implements a first-in first-out (FIFO) queue data structure using an array. The Queue class tracks the front and rear indexes of the queue, and provides methods to check if the queue is empty/full, get the size, enqueue elements to the rear, dequeue elements from the front, and peek at the front element. The main method demonstrates using the Queue class by prompting the user to choose between enqueue, dequeue, peek, size, and exit options in a menu-driven interface.

Uploaded by

sahilahemad05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Queue

This Java code defines a Queue class that implements a first-in first-out (FIFO) queue data structure using an array. The Queue class tracks the front and rear indexes of the queue, and provides methods to check if the queue is empty/full, get the size, enqueue elements to the rear, dequeue elements from the front, and peek at the front element. The main method demonstrates using the Queue class by prompting the user to choose between enqueue, dequeue, peek, size, and exit options in a menu-driven interface.

Uploaded by

sahilahemad05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

Scanner;

public class Queue {

private int maxSize;

private int front;

private int rear;

private int[] queueArray;

public Queue(int size) {

this.maxSize = size;

this.queueArray = new int[maxSize];

this.front = 0;

this.rear = -1;

public boolean isEmpty() {

return (rear == -1);

public boolean isFull() {

return (rear == maxSize - 1);

public int size() {

return rear - front + 1;

public void enqueue(int data) {

if (!isFull()) {
queueArray[++rear] = data;

System.out.println("Enqueued: " + data);

} else {

System.out.println("Queue is full. Cannot enqueue " + data);

public int dequeue() {

if (!isEmpty()) {

int removedItem = queueArray[front++];

System.out.println("Dequeued: " + removedItem);

return removedItem;

} else {

System.out.println("Queue is empty. Cannot dequeue.");

return -1; // You can choose a different sentinel value if needed.

public int peek() {

if (!isEmpty()) {

return queueArray[front];

} else {

System.out.println("Queue is empty. Cannot peek.");

return -1; // You can choose a different sentinel value if needed.

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the size of the queue: ");

int queueSize = scanner.nextInt();

Queue myQueue = new Queue(queueSize);

while (true) {

System.out.println("\nOptions:");

System.out.println("1. Enqueue");

System.out.println("2. Dequeue");

System.out.println("3. Peek");

System.out.println("4. Queue Size");

System.out.println("5. Exit");

System.out.print("Enter your choice: ");

int choice = scanner.nextInt();

switch (choice) {

case 1:

System.out.print("Enter element to enqueue: ");

int enqueuedItem = scanner.nextInt();

myQueue.enqueue(enqueuedItem);

break;

case 2:

myQueue.dequeue();

break;

case 3:

int peekedItem = myQueue.peek();

if (peekedItem != -1) {

System.out.println("Front element: " + peekedItem);

}
break;

case 4:

System.out.println("Queue size: " + myQueue.size());

break;

case 5:

scanner.close();

System.exit(0);

default:

System.out.println("Invalid choice. Try again.");

You might also like