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

SimpleQueue Operations

simple
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SimpleQueue Operations

simple
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;

class Queue {
int front, rear, maxsize = 5;
int[] arr = new int[maxsize];

Queue() {
front = -1;
rear = -1;
}

void enqueue(Scanner sc) {


if (rear == maxsize - 1) {
System.out.println("Queue Overflow!!");
} else {
System.out.println("Enter Value");
int val = sc.nextInt();
if (front == -1) {
front = 0;
}
rear++;
arr[rear] = val;
System.out.println("Item enqueued");
}
}

void dequeue() {
if (front == -1 || front > rear) {

System.out.println("Queue Underflow!!");
} else {
System.out.println("Item dequeued: " + arr[front]);
front++;
}

void display() {
if (front == -1) {
System.out.println("Queue is empty");
} else {
System.out.println("Printing queue elements are as below");
for (int i = front; i <= rear; i++) {
System.out.println(arr[i]);
}
}
}
}

public class simpleQueue_Operations {


public static void main(String[] args) {
int choice = 0;
Scanner sc = new Scanner(System.in);
Queue q = new Queue();
System.out.println("*********Queue operations using array*********\n");
System.out.println("\n------------------------------------------------\n");
while (choice != 4) {
System.out.println("\nChoose one from the below options...\n");
System.out.println("\n1.Enqueue\n2.Dequeue\n3.Show\n4.Exit");
System.out.println("\n Enter your choice \n");
choice = sc.nextInt();
switch (choice) {
case 1: {
q.enqueue(sc);
break;
}
case 2: {
q.dequeue();
break;
}
case 3: {
q.display();
break;
}
case 4: {
System.out.println("Exiting....");
break;
}
default: {
System.out.println("Please Enter a valid choice ");
}
}
}
sc.close();
}
}

You might also like