consider a linked structure in the form of a doubly linked list DoublyLinkedList of objects of generic
Question:
consider a linked structure in the form of a doubly linked list DoublyLinkedList of objects of generic type E.
The DoublyLinkedList class has as instance variables:
Node head: 1st element of doubly linked list Node tail: last element of doubly linked list int size: the number of items in the list The DoublyLinkedList class has an internal ListIterator class allowing to first browse in the forward direction and then in the opposite direction on an object of type DoublyLinkedList.The attached DoublyLinkedList class has the usual necessary instance methods provided, among others:
-DoublyLinkedList(): Constructor with no arguments -Size(): returns the number of elements in the list -void addFirst (E elem ): insertion at the beginning of the list (provided) ... The DoublyLinkedList class also has an iterator. An object of the DoublyLinkedList class has an iterator method that returns a ListIterator object that realizes the attached Iterator interface:
public interface Iterator {
// returns true if there are elements left to go in the forward direction
booleanhasNext();
// returns true if there are elements left to go in the opposite direction
boolean hasPrevious();
// returns the next element of the path first direct and then inverse
E next ( );
}
ListIterator has two instance variables currentForward and currentBackward for forward and reverse traversal respectively.
A call to the hasNext method of an iterator returns true if there are still elements to return in the direct iteration (from the first to the last element of the list), and false otherwise. The hasNext method is provided attached.
A call to the hasPrevious method of an iterator returns true if there are still elements to return in the reverse iteration (from the last to the first element of the list), and false otherwise.
A call to the next method of an iterator returns the next object of the iteration in both directions (first direct then inverse).
In this question and the next, you will implement the attached iterator interface.
QUESTION:
Implement the hasPrevious() method in the space below.
Implement the next() method in the space below.
The DoublyLinkedList class contains all the getters needed to terminate this program.
DoublyLInkedList:
import java.util.NoSuchElementException;
public class DoublyLinkedList { private static class Node { private T value; private Node next ; private Node previous ;
private Node (T value , Node previous, Node next ) { this.value = value; this.next = next ; this.previous = previous ; } } // end of Node
// instance variables for DoublyLinkedList private Node head; private Node tail; private int size ;
// constructors public DoublyLinkedList() { head = null; tail = null; size=0; }
/*size() * **/ int size(){ return size; }
public boolean isEmpty() { return size == 0; }
/*insertion d'un element au dbut de de la liste * **/ public void addFirst (E elem ) { Node newNode ; newNode = new Node(elem , null, head ) ; if ( head == null) { head = newNode ; tail = head ; } else { newNode.next.previous = newNode; head = newNode ; } size++; } /*Retourne la valeur sauvegarde la position pos de la liste * **/ public E get ( int pos ) { if (pos < 0 || pos >= size) throw new IllegalArgumentException("NO ELEMENT"); Node p ; p = head ; for (int i =0 ; i
/* * Inner class ListIterator */ private class ListIterator implements Iterator { private Node currentForward,currentBackward ;
private ListIterator( ) {//constructeur currentForward = null; currentBackward = null; }
public boolean hasNext ( ) { return ((currentForward == null && head != null) || (currentForward != null && currentForward.next != null ) ); }
public boolean hasPrevious ( ) { //YOUR CODE COMES HERE }
public E next ( ) { if(hasNext()) { //YOUR 1ST PIECE OF CODE HERE } else if (hasPrevious ( )){ //YOUR 2ND PIECE OF CODE HERE } //Other code here } }// end of ListIterator
public Iterator iterator ( ) { return new ListIterator(); }
}// end of DoublyLinkedList
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang