Question
Very Easy Java Question!! Implement the prepend() and popRear() methods in the following CircularDeque class, which uses a circular array to store the elements. public
Very Easy Java Question!!
Implement the prepend() and popRear() methods in the following CircularDeque class, which uses a circular array to store the elements.
public class CircularDeque
/* Build a circular array-based deque with specified capacity * @param: int capacity : size of the array to use */ public CircularDeque(int capacity) { array = new Object[capacity]; }
/* if deuque is not full, append element e to the deque and return true, * otherwise return false * @param: T e : Element of type e to appended to the dequee * @return: true if append successful and false otherwise */ public boolean append(T e) { if (size == array.length) { return false; } array[(front + size) % array.length] = e; size++; return true; }
/* if deuque is not full, prepend element e to the deque and return true, * otherwise return false * @param: T e : Element of type e to prepended to the dequee * @return: true if prepend successful and false otherwise */ public boolean prepend(T e) { // WRITE code here (see append() above for hints) return true; }
/* if deuque is not empty, remove and return the element at the front of the * deque, else return null * @return: element of type T if deque was not empty, else null */ public T popFront() { T e = null; if (size > 0) { e = (T)array[front]; front = (front + 1) % array.length; size--; } return e; }
/* if deuque is not empty, remove and return the element at the front of the * deque, else return null * @return: element of type T if deque was not empty, else null */ public T popRear() { T e = null; // WRITE code here (see popFront() above for hints)
return e; } /* returns true if empty and false otherwise * @return: boolean true if empty */ public boolean isEmpty() { return size == 0; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started