Queue is an entity which can hold a maximum of 100 integers. The queue enables the user to add integers from the rear and remove integers from the front.
Define a class Queue with the following details:
Class name: Queue
Data members/instance variables:
Que[]: array to hold the integer elements
size: stores the size of the array
front: to point the index of the front
rear: to point the index of the rear
Member functions:
Queue(int mm): constructor to initialize the data size = mm, front = 0, rear = 0
void addele(int v): to add integer from the rear if possible else display the message “Overflow”
int delete(): returns elements from front if present, otherwise displays the message “Underflow” and returns -9999
void display(): displays the array elements
Specify the class Queue giving details of only the functions void addele(int) and int delete(). Assume that the other functions have been defined.
The main() function and algorithm need not be written.
import java.util.Scanner;
class Queue{
int que[];
int size;
int front;
int rear;
public Queue(int m){
size = m;
que = new int[size];
front = 0;
rear = 0;
}
public void addele(int v){
if(rear == size)
System.out.println("Overflow");
else{
que[rear++] = v;
System.out.println(v + " PUSHED");
}
}
public int delele(){
if(rear == 0){
System.out.println("Underflow");
return -9999;
}
int v = que[front++];
if(front >= rear){
front = 0;
rear = 0;
}
return v;
}
public void display(){
if(rear == 0)
System.out.println("QUEUE EMPTY");
else{
for(int i = front; i < rear; i++)
System.out.print(que[i] + " ");
System.out.println();
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Queue size: ");
int max = Integer.parseInt(in.nextLine());
if(max > 100)
max = 100;
Queue obj = new Queue(max);
while(true){
System.out.println("1. PUSH ELEMENT");
System.out.println("2. POP ELEMENT");
System.out.println("3. DISPLAY ELEMENTS");
System.out.print("ENTER YOUR CHOICE: ");
int choice = Integer.parseInt(in.nextLine());
switch(choice){
case 1:
System.out.print("Element to be pushed: ");
int v = Integer.parseInt(in.nextLine());
obj.addele(v);
break;
case 2:
v = obj.delele();
if(v != -9999)
System.out.println(v + " POPPED");
break;
case 3:
obj.display();
break;
default:
System.out.println("Bye!");
return;
}
}
}
}