Homework Help Question & Answers

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...

2. In Ex 19 of ME2 we used a DList<E list to store the values of the stack, so we can say that our stack is really just a spe

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(); } }

2. In Ex 19 of ME2 we used a DList
0 0
Next > < Previous
ReportAnswer #1

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;
    }
}
Add Homework Help Answer
Add Answer of:
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...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
More Homework Help Questions Additional questions in this topic.
Need Online Homework Help?
Ask a Question
Get FREE Expert Answers
WITHIN MINUTES
Related Questions