Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A “dequeue” is like a queue but it also allows you to insert into the front of t

ID: 3799576 • Letter: A

Question

A “dequeue” is like a queue but it also allows you to insert into the front of the queue and to remove from the rear of the queue. Create an array-based Dequeue class.

Driver....

public class Driver
{
   public static void main(String[] args)
   { //page 376 #18
       System.out.println("page 376 #18 ");
       Dequeue<Integer> myDequeue;
       myDequeue = new Dequeue<Integer>();
myDequeue.enqueue(23);
       System.out.print("enqueue 23 myDequeue: ");
       System.out.println(myDequeue);
       myDequeue.enqueue(40);
       System.out.print("enqueue 40 myDequeue: ");
       System.out.println(myDequeue);
       myDequeue.addFront(50);
       System.out.print("addFront50 myDequeue: ");
       System.out.println(myDequeue);
       myDequeue.addFront(20);
       System.out.print("addFront20 myDequeue: ");
       System.out.println(myDequeue);
       myDequeue.dequeue();
       System.out.print("dequeue myDequeue : ");
       System.out.println(myDequeue);
       myDequeue.enqueue(8);
       System.out.print("enqueue 8 myDequeue : ");
       System.out.println(myDequeue);
       myDequeue.removeRear();
       System.out.print("removeRear myDequeue: ");
       System.out.println(myDequeue);
       myDequeue.removeRear();
       System.out.print("removeRear myDequeue: ");
       System.out.println(myDequeue);
       myDequeue.dequeue();
       System.out.print("dequeue myDequeue : ");
       System.out.println(myDequeue);
       myDequeue.removeRear();
       System.out.print("removeRear myDequeue: ");
       System.out.println(myDequeue);
      
   }
}

output ...

//Sample Output

page 376 #18

enqueue 23 myDequeue: 23

enqueue 40 myDequeue: 23 40

addFront50 myDequeue: 50 23 40

addFront20 myDequeue: 20 50 23 40

dequeue myDequeue   : 50 23 40

enqueue 8 myDequeue : 50 23 40 8

removeRear myDequeue: 50 23 40

removeRear myDequeue: 50 23

dequeue myDequeue   : 23

removeRear myDequeue:

//End of Sample Output

assuming that DEFCAP is 5

content of Array queue will be

page 376 #18

enqueue 23 myDequeue: DEFCAP=5 front=0 rear=0 23 null null null null

enqueue 40 myDequeue: DEFCAP=5 front=0 rear=1 23 40 null null null

addFront50 myDequeue: DEFCAP=5 front=4 rear=1 23 40 null null 50

addFront20 myDequeue: DEFCAP=5 front=3 rear=1 23 40 null 20 50

dequeue myDequeue   : DEFCAP=5 front=4 rear=1 23 40 null null 50

enqueue 8 myDequeue : DEFCAP=5 front=4 rear=2 23 40 8 null 50

removeRear myDequeue: DEFCAP=5 front=4 rear=1 23 40 null null 50

removeRear myDequeue: DEFCAP=5 front=4 rear=0 23 null null null 50

dequeue myDequeue   : DEFCAP=5 front=0 rear=0 23 null null null null

removeRear myDequeue: DEFCAP=5 front=0 rear=4 null null null null null

IN JAVA PLEASE

Explanation / Answer

public class ArrayStack implements StackADT { private Object[] itsItem = new Object [10]; private int itsSize = 0; public boolean isEmpty() { return itsSize == 0; } //====================== public Object peekTop() { if (isEmpty()) throw new IllegalStateException ("stack is empty"); return itsItem[itsSize - 1]; } //====================== public Object pop() { if (isEmpty()) throw new IllegalStateException ("stack is empty"); itsSize--; return itsItem[itsSize]; } //====================== public void push (Object ob) { if (itsSize == itsItem.length) { Object[] toDiscard = itsItem; itsItem = new Object [2 * itsSize]; for (int k = 0; k < itsSize; k++) itsItem[k] = toDiscard[k]; } itsItem[itsSize] = ob; itsSize++; } //====================== }

public class ArrayQueue implements QueueADT { private Object[] itsItem = new Object [10]; private int itsFront = 0; //location of front element, if any private int itsRear = -1; //location of rear element, if any public boolean isEmpty() { return itsRear == itsFront - 1; } //====================== public Object peekFront() { if (isEmpty()) throw new IllegalStateException ("queue is empty"); return itsItem[itsFront]; } //====================== public Object dequeue() { if (isEmpty()) throw new IllegalStateException ("queue is empty"); itsFront++; return itsItem[itsFront - 1]; } //====================== public void enqueue (Object ob) { if (itsRear == itsItem.length - 1) adjustTheArray(); itsRear++; itsItem[itsRear] = ob; } //====================== private void adjustTheArray() { if (itsFront > itsRear / 4) { itsRear -= itsFront; for (int k = 0; k <= itsRear; k++) itsItem[k] = itsItem[k + itsFront]; itsFront = 0; } else { Object[] toDiscard = itsItem; itsItem = new Object [2 * itsRear]; for (int k = 0; k <= itsRear; k++) itsItem[k] = toDiscard[k]; } // automatic garbage collection gets rid of toDiscard } //====================== }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote