e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } }
f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex 19 public Queue() { super(); } public Queue enqueue(E pData) { push(pData); return this; } public E dequeue() { return pop(); } public E peek() { return peek(); } }
Implementation of Option B is the correct one.
DList.java import java.util.LinkedList; public class DList<E> { //intialize the list LinkedList<E> list = new LinkedList<>(); //get the size of the list public int getSize(){ return list.size(); } //remove the elment from the list public E remove(int index){ return list.remove(index); } //set the element at index public void set(int index, E value){ list.set(index,value); } //get the elemnt from the index public E get(int index){ return list.get(index); } }
Queue.java public class Queue<E> { private DList<E> mList; public Queue(){ setList(new DList<>()); } protected int indexOfFromt(){ return 0; } protected int indexOfRear(){ return getList().getSize() - 1; } public Queue<E> enqueue(E pData){ getList().set(indexOfFromt(),pData); return this; } public E dequeue(){ E value = getList().remove(indexOfRear()); return value; } public E peek(){ E value = getList().get(indexOfFromt()); return value; } private DList<E> getList(){ return mList; } private void setList(DList<E> pList){ mList = pList; } }
e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return t...