Question
IN JAVA: Using a circular array to implement a deque, which includes the following operations create an empty deque test if the deque is empty
IN JAVA:
Using a circular array to implement a deque, which includes the following operations
create an empty deque
test if the deque is empty
insert a new item at front into the deque
insert a new item at back into the deque
remove the item at front in the deque
remove the item at back in the deque
get the item at front in the deque
get the item at back in the deque
Retrieves all entries that are in the deque
Code:
public class CArrayDeque{ private T[] items; private int front; private int back; private int size; final static int CAPACITY=10; // Default constructor public CArrayDeque() { //TO DO } /** Sees whether this queue is empty. @return True if the queue is empty, or false if not. */ public boolean isEmpty() { //TO DO return false; } /** Adds a new entry to this queue at front. @param newEntry The object to be added as a new entry. @return True if the addition is successful, or false if not. */ public boolean addFront(T newEntry) { //TO DO return false; } /** Adds a new entry to this queue at back. @param newEntry The object to be added as a new entry. @return True if the addition is successful, or false if not. */ public boolean addBack(T newEntry) { //TO DO return false; } /** Removes the entry at front from the queue, if possible. @return True if the removal was successful, or false if not. */ public boolean removeFront() { //TO DO return false; } /** Removes the entry at back from the queue, if possible. @return True if the removal was successful, or false if not. */ public boolean removeBack() { //TO DO return false; } /** Retrieve the entry at front in the queue, if possible. @return the front entry if the retrieve was successful, or null if not. */ public T retrieveFront() { //TO DO return null; } /** Retrieve the entry at back in the queue, if possible. @return the front entry if the retrieve was successful, or null if not. */ public T retrieveBack() { //TO DO return null; } /** Retrieves all entries that are in this queue. @return A newly allocated array of all the entries in this queue. */ public T[] toArray(){ //TO DO return null; } }
Following is an example of the output:
The queue is empty now. The items in queue: null null null null null null null null null null The front entry is null The back entry is null ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ After adding at FRONT with One, Two, Three, Four, Five ... The items in queue: null null null null null Five Four Three Two One The front entry is Five The back entry is One ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ After adding at BACK with 1, 2, 3, 4, 5 ... The items in queue: 1 2 3 4 5 Five Four Three Two One The front entry is Five The back entry is 5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Try to add at BACK with 6 ... Insert failed! OVERFLOW ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Delete 6 entries at FRONT ... Delete successfully! Delete successfully! Delete successfully! Delete successfully! Delete successfully! Delete successfully! The items in queue: null 2 3 4 5 null null null null null The front entry is 2 The back entry is 5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Delete 1 entry at Back and 1 entry at FRONT ... Delete successfully! Delete successfully! The items in queue: null null 3 4 null null null null null null The front entry is 3 The back entry is 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Continue to delete 6 entries at FRONT ... Delete successfully! Delete successfully! Empty Queue! Delete failed! UNDERFLOW! Empty Queue! Delete failed! UNDERFLOW! Empty Queue! Delete failed! UNDERFLOW! Empty Queue! Delete failed! UNDERFLOW! The items in queue: null null null null null null null null null null The front entry is null The back entry is null
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