X703: LinkedQueueAlmostPriorityEnqueue

For the question below, assume the following implementation of LinkedQueue:


public static final class LinkedQueue<T> implements QueueInterface<T> {
    private Node firstNode;
    private Node lastNode;

    public LinkedQueue() {
        firstNode = null;
        lastNode = null;
    }


    @Override
    public T getFront() {
        if (isEmpty()) {
            return null;
        }
        return firstNode.getData();
    }


    @Override
    public boolean isEmpty() {
        return firstNode == null;
    }


    @Override
    public void clear() {
        firstNode = null;
        lastNode = null;

    }

    public class Node{
        private T data; // Entry in bag
        private Node next; // Link to next node

        public Node(T dataPortion) {
            this(dataPortion, null);
        } // end constructor


        public Node(T dataPortion, Node nextNode) {
            data = dataPortion;
            next = nextNode;
        } // end constructor


        public T getData() {
            if (data != null) {
                return data;
            }
            return null;
        }


        public Node getNext() {
            return next;
        }


        public void setNext(Node newNext) {
            next = newNext;
        }

} // end LinkedQueue

Your task is to create an "almostPriority" enqueue method. If something is enqueued, it normally goes to the back of the queue, but in this method it will be set immediately after the first element in the queue.

For example, if you had a linked queue that looked like:

(firstNode) 1 --> 2 --> 3 (lastNode)

and you ran almostPriorityEnqueue(4)

the result would be:

(firstNode) 1 --> 4 --> 2 --> 3 (lastNode)

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.