Queue
Queue
A Queue is a linear data structure which enables the user to add integers from rear end and remove
integers from the front end, using the concept of FIFO (Fist In First Out) principle. Define a class
Queue with the following details:
Class Name: Queue
Data members:
q[]: array to hold elements.
n: to store the size of the array.
f: to point the index of front end.
r: to point the index of rear end.
Member methods:
Queue(int n1): parameterized constructor.
void addQueue(int x): to push or insert argument x at the rear location, if possible, otherwise display
"Queue Overflow".
int deleteQueue(): to pop integer from the front location of the queue, if possible, return the
integer, otherwise display "Queue Underflow".
void display(): to print the elements present in the queue, if possible, otherwise display "Queue
Underflow".
import java.util.*;
class Queue_Diganta Biswas
{ //start of class
int q[]; //declaration of variables
int f,r,n;
Queue_ Diganta Biswas (int n1)
{
q=new int[n];
n=n1; //initializing variables
f=r=0;
}
void addQueue(int x)
{
if (r < n - 1)
{
q[r] = x;
r++;
}
else
{
System.out.println("Queue Overflow");
}
}
void display()
{
if (f == r)
{
System.out.println("Queue Underflow");
return;
}
for (int i = f; i < r; i++)
{
System.out.print(q[i] + " ");
}
System.out.println();
}
Variable description:-
Name Datatype Purpose
q[] int array to hold elements.
N int to store the size of the array.
f int to point the index of front end.
r int to point the index of rear end.
n1 int formal parameter
x int formal parameter
ch int switch case variable
no int variable to pass to methods