Write a menu driven program in Java to perform the operations on an Integer
Queue by making use of the concept of interfaces. And create custom
exceptions to deal with the following situations
A non-integer value is encountered in the queue
b. Insert operation when the Queue if full
c. Remove operation when queue is empty.
public class Main {
public static void main(String[] args)
{
Queue q1 = new Queue(5);
q1.enqueue(1);
q1.enqueue(2);
q1.enqueue(3);
q1.enqueue(4);
q1.enqueue(5);
q1.display();
q1.dequeue();
q1.dequeue();
q1.display();
q1.frontD();
}
}
class Queue {
private static int front, rear, cap;
private static int queue[];
Queue(int c)
{
front = rear = 0;
cap = c;
queue = new int[cap];
}
static void enqueue(int d)
{
if (cap == rear) {
System.out.printf("Queue is full");
return;
}
else {
queue[rear] = d;
rear++;
}
return;
}
static void dequeue()
{
if (front == rear) {
System.out.printf("Queue is empty");
return;
}
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
if (rear < cap)
queue[rear] = 0;
rear--;
}
return;
}
static void display()
{
if (front == rear) {
System.out.printf("Queue is Empty");
return;
}
for (int i = front; i < rear; i++) {
System.out.printf(" %d <-- ", queue[i]);
}
return;
}
static void frontD()
{
if (front == rear) {
System.out.println("Queue is Empty");
return;
}
System.out.println("Front Element is: "+queue[front]);
return;
}
}
Comments
Leave a comment