X810: Iterators DoublyLinkedList next()

For this question, assume the following implementation of a doubly linked list:

    public static class DLList<T> implements Iterable<T> {
        private int numberOfEntries;
        private Node<T> head;
        private Node<T> tail;

        public DLList() {
            head = null;
            tail = null;
            this.numberOfEntries = 0;
        }// end default constructor



        public boolean isEmpty() {
            return numberOfEntries == 0;
        }

        public int getNumberOfEntries() {
            return numberOfEntries;
        }

        public void clear() {
            head = null;
            tail = null;
            this.numberOfEntries = 0;
        }

        public Iterator<T> iterator() {
            return new DLListIterator();
        }

        ...

        // Node
        public class Node<E> {
            private E data; // Entry in bag
            private Node<E> next; // Link to next node
            private Node<E> previous; // Link to previous Node

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


            public Node(E dataPortion, Node<E> nextNode) {
                this(dataPortion, nextNode, null);
            } // end constructor


            public Node(E dataPortion, Node<E> nextNode, Node<E> previousNode) {
                data = dataPortion;
                next = nextNode;
                previous = previousNode;
            } // end constructor


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


            public Node<E> getNext() {
                return next;
            }


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


            public Node<E> getPrevious() {
                return previous;
            }


            public void setPrevious(Node<E> newPrevious) {
                previous = newPrevious;
            }


        } // end Node


        // Iterator
        public class DLListIterator implements Iterator<T> {
            private Node<T> current;
            private boolean canRemove;

            public DLListIterator() {
                // get first non-sentinal node
                current = head;
                canRemove = false;
            }


            @Override
            public boolean hasNext() {
                return current != null;
            }


            @Override
            public void remove() {
                ...
            }// end remove
        }// end iterator

    }

Implement the next() method in the Iterator class. Check the javadocs if you need help remembering what this method is supposed to do!
You can find documentation for the Iterators at: https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.