Question: 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
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
The framework of the CArrayDeque class is given in "CArrayDeque.java" and a test driver is given in "DequeTest.java". Please read them carefully and complete your class methods in "CArrayDeque.java". 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
Deque test driver
public class DequeTest { public static void display(CArrayDeque dq) { System.out.println("The items in queue:"); for (int i=0; i void checkFrontBack(CArrayDeque dq) { T frontItem, backItem; frontItem=(T) dq.retrieveFront(); backItem=(T) dq.retrieveBack(); System.out.println("The front entry is " + frontItem); System.out.println("The back entry is " + backItem); } public static void main(String[] args) { CArrayDeque q =new CArrayDeque<>(); System.out.println("The queue is empty now."); display(q); checkFrontBack(q); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println("After adding at FRONT with One, Two, Three, Four, Five ..."); q.addFront("One"); q.addFront("Two"); q.addFront("Three"); q.addFront("Four"); q.addFront("Five"); display(q); checkFrontBack(q); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println("After adding at BACK with 1, 2, 3, 4, 5 ..."); q.addBack("1"); q.addBack("2"); q.addBack("3"); q.addBack("4"); q.addBack("5"); display(q); checkFrontBack(q); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println("Try to add at BACK with 6 ..."); boolean flag=q.addBack("6"); if (flag==true) System.out.println("Insert successfully!"); else System.out.println("Insert failed! OVERFLOW !"); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println("Delete 6 entries at FRONT ..."); for (int i=0; i<6; i++) { flag=q.removeFront(); if (flag==true) System.out.println("Delete successfully!"); else System.out.println("Insert failed!"); } display(q); checkFrontBack(q); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println("Delete 1 entry at Back and 1 entry at FRONT ..."); flag=q.removeBack(); if (flag==true) System.out.println("Delete successfully!"); else System.out.println("Insert failed!"); flag=q.removeFront(); if (flag==true) System.out.println("Delete successfully!"); else System.out.println("Insert failed!"); display(q); checkFrontBack(q); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println("Continue to delete 6 entries at FRONT ..."); for (int i=0; i<6; i++) { flag=q.removeFront(); if (flag==true) System.out.println("Delete successfully!"); else System.out.println("Empty Queue! Delete failed! UNDERFLOW!"); } display(q); checkFrontBack(q); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
