Question
Define a class Repeat which allows the user to add elements from one end(rear) and remove elements from the other end(front)only.
The following details of the class Repeat are given below:
Classname:Repeat
Data Members/ instance variables:
st[]:an array to hold a maximum of
100 integer elements
cap:stores the capacity of the array
f:to point the index of the front
r:to point the index of the rear
Member functions:
Repeat(int m):constructor to initialize the data members cap=m, f=0,r=0 and to create the integer array
void pushvalue(int v):to add integers from the rear index if possible else display the message(“OVERFLOW”)
int popvalue():to remove and return element from the front.If array is empty then return -9999
void disp():Displays the elements present in the list
a) Specify.the class Repeat giving details of the constructor(int), member function void pushvalue(int) , int popvalue() and void disp() . The main() function need not be written.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
public class Repeat
{
int st[]=new int[100];
int cap,f,r;
public Repeat(int m)
{
cap=m;
f=0;
r=0;
st=new int[cap];
}
public void pushvalue(int v)
{
if((r+1)<=cap)
{
r=r+1;
st[r]=v;
}
else
{
System.out.println("OVERFLOW");
}
}
public int popvalue()
{
int v;
if(r!=f)
{
f=f+1;
v=st[f];
return v;
}
else
{
return -9999;
}
}
public void display()
{
if(r!=f)
{
for(int i=f+1;i<=r;i++)
{
System.out.println(st[i]);
}
}
else
{
System.out.println("Queue is empty");
}
}
}