Question
Java Add a nested ReverseIterator class (dont need a remove method, only hasNext() and next() methods). Add a reverseIterator() accessor method to the DoublyLinkedList class
Java
Add a nested ReverseIterator class (dont need a remove method, only hasNext() and next() methods).
Add a reverseIterator() accessor method to the DoublyLinkedList class to return an instance of a ReverseIterator.
Add a remove method to the ForwardIterator class. Look at the java.util.Iterator javadocs for information about how this function should operate.
Modify Tester1 class that demonstrates that all methods shown above operate correctly.
Code provided
public class DoublyLinkedList {
private static class Node { private E element; private Node prev; private Node next;
public Node(E e, Node p, Node n) { element = e; prev = p; next = n; }
public E getElement() { return element; }
public Node getPrev() { return prev; }
public Node getNext() { return next; }
public void setPrev(Node p) { prev = p; }
public void setNext(Node n) { next = n; } } //----------- end of nested Node class -----------
private Node header;
private Node trailer; // trailer sentinel
private int size = 0; // number of elements in the list
public DoublyLinkedList() { header = new Node<>(null, null, null); // create header trailer = new Node<>(null, header, null); // trailer is preceded by header header.setNext(trailer); // header is followed by trailer }
public int size() { return size; }
public boolean isEmpty() { return size == 0; }
public E first() { if (isEmpty()) return null; return header.getNext().getElement(); // first element is beyond header }
public E last() { if (isEmpty()) return null; return trailer.getPrev().getElement(); // last element is before trailer }
public void addFirst(E e) { addBetween(e, header, header.getNext()); // place just after the header }
public void addLast(E e) { addBetween(e, trailer.getPrev(), trailer); // place just before the trailer }
public E removeFirst() { if (isEmpty()) return null; // nothing to remove return remove(header.getNext()); // first element is beyond header }
public E removeLast() { if (isEmpty()) return null; // nothing to remove return remove(trailer.getPrev()); // last element is before trailer }
private void addBetween(E e, Node predecessor, Node successor) { // create and link a new node Node newest = new Node<>(e, predecessor, successor); predecessor.setNext(newest); successor.setPrev(newest); size++; }
private E remove(Node node) { Node predecessor = node.getPrev(); Node successor = node.getNext(); predecessor.setNext(successor); successor.setPrev(predecessor); size--; return node.getElement(); }
public String toString() { StringBuilder sb = new StringBuilder("("); Node walk = header.getNext(); while (walk != trailer) { sb.append(walk.getElement()); walk = walk.getNext(); if (walk != trailer) sb.append(", "); } sb.append(")"); return sb.toString(); } } //----------- end of DoublyLinkedList class -----------
TESTER CLASS
import java.util.Iterator; public class Tester1 { public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList<>(); dll.addFirst("a"); dll.addLast("b"); dll.addLast("c"); for(String s : dll) { System.out.println(s); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started