Question
A doubly queue is a linear data structure which enables the user to add and remove integers from either ends , i.e from front or rear. Define a class Dequeue with the following details:
Classname:Dequeue
Data members / instance variables:
arr[]:array to hold upto 100 integer elements
lim: stores the limit of the dequeue
front:to point to the index of front end
rear:to point to the index of rear end
Memberfunctions:
Dequeue(int l):constructor to initialize the data members lim=l,front=rear=0
void addfront(int val):to add integer from the front if possible else display the message(“Overflow from front”)
void addrear(int val):to add integer from the rear if possible else display the message(“Overflow from rear”)
int popfront():returns element from front, if possible otherwise returns -9999
int poprear():returns element from rear, if possible otherwise returns -9999
Specify the class Dequeue giving details of the constructor(int), void addfront(int) , void addrear(int) , int popfront() and int poprear().
The main function and algorithm need not be written.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
public class Dequeue
{
int arr[]=new int[100];
int lim,front,rear;
Dequeue(int l)
{
lim=l;
front=0;
rear=0;
arr=new int[lim];
}
void addrear(int val)
{
if(rear< lim)
{
arr[rear++]=val;
}
else
{
System.out.println("OVERFLOW");
}
}
int poprear()
{
//if front and rear are at zero which means nothing is entered
if(front==0&&rear==0 )
{
return -9999;
}
else
{
rear=rear-1;
return arr[rear];
}
}
int popfront()
{
//If front is equal to rear that means that either all elements of array are deleted
// or no elements are added
if(front==rear)
{
front=0;
rear=0;
return -9999;
}
else
{
return arr[front++];
}
}
void addfront(int val)
{
if(front>0)
{
front=front-1;
arr[front]=val;
}
else
{
System.out.println("Overflow from front");
}
}
void display()
{
for (int i=front;i< rear;i++)
{
System.out.println(arr[i]);
}
}
}
Coding Store
Sale
ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale