/** * Returns value of the element that was first added to the queue. * * @return value stored in the head of the queue. */ @Override public Item peek() { if (head == null) { throw new IllegalStateException("Queue cannot be empty."); } return head.data(); }
/** Accesses next element in the queue. */ @Override public Item next() { if (!hasNext()) { throw new NoSuchElementException("Queue cannot be empty."); } else { Item returnValue = current.data(); toRemoveLink = current; current = current.previous(); markNext = true; --index; return returnValue; } }
/** * This method removes the first element added to the queue.Since it's not a circular linked * queue, it has to handle special case when there is only one element in the queue. * * @return returnValue removed data element. */ @Override public Item dequeue() { if (size == 0) { throw new IllegalStateException("Queue cannot be empty."); } // ~Creates a variable to store a current data, that is removed by // dequeue() Item returnValue = head.data(); // ~Special case, where there is only one element in the queue. if (size == 1) { back = null; head = null; --size; return returnValue; } else { // Changes value of head of the current queue. head = head.previous(); head.split(); --size; return returnValue; } }