Advertisements
Advertisements
Question
Recycle is an entity which can hold at the most 100 integers. The chain enables the user to add and remove integers from both the ends i.e. front and rear.
Define a class ReCycle with the following details:
Class name | ReCycle |
Data members/instance variables: | |
ele[ ] | the array to hold the integer elements |
cap | stores the maximum capacity of the array |
front | to point the index of the front |
rear | to point the index of the rear |
Methods / Member functions: | |
ReCycle (int max) | constructor to initialize the data cap = max, front = rear = 0 and to create the integer array. |
void pushfront(int v) | to add integers from the front index if possible else display the message(“full from front”). |
int popfront( ) | to remove the return elements from front. If array is empty then return-999 |
void pushrear(int v) | to add integers from the front index if possible else display the message(“full from rear”). |
int poprear( ) | to remove and return elements from rear. If the array is empty then return-999. |
- Specify the class ReCycle giving details of the functions void pushfront(int) and int poprear( ). Assume that the other functions have been defined.
The main( ) function and algorithm need NOT be written. - Name the entity described above and state its principle.
Answer in Brief
Solution
i.
class ReCycle
{
void pushfront(int v)
{
if(front !=0)
q[front--]=v;
else
System.out.println("FULL FROM FRONT");
}
int poprear()
{
if(front !=rear)
return(q[rear--]);
else
return-999;
}
}
ii. Entity is dequeue and works on the principle of FIFO
shaalaa.com
Basic Algorithms and Programs Using the Above Data Structures
Is there an error in this question or solution?