Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You will write code for all the methods of the LinkedListIterator class. You will definitely have to write the code for the hasPrevious, previous and
You will write code for all the methods of the LinkedListIterator class. You will definitely have to write the code for the hasPrevious, previous and add methods. You can add any additional data members to the LinkedListIterator class but you cannot change or remove the existing ones: cursor and expectedModCount. You should not have to write or rewrite any of the code for the hasNext method. You will probably have to add additional code to the LinkedListIterator class constructor, and the next and remove methods. You will not need to create any additional methods for the LinkedListIterator class. You cannot add any nested classes to the LinkedListIterator class. ----> I'm having trouble understanding what to put in the public void add(AnyType newValue) <----- Here's the code: import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; public class DoublyLinkedListimplements List { private static class Node { private AnyType data; private Node prev; private Node next; public Node(AnyType d, Node p, Node n) { setData(d); setPrev(p); setNext(n); } public AnyType getData() { return data; } public void setData(AnyType d) { data = d; } public Node getPrev() { return prev; } public void setPrev(Node p) { prev = p; } public Node getNext() { return next; } public void setNext(Node n) { next = n; } } private int theSize; private int modCount; private Node header; private Node trailer; public DoublyLinkedList() { header = new Node (null, null, null); trailer = new Node (null, null, null); modCount = 0; clear(); } public void clear() { header.setNext(trailer); trailer.setPrev(header); theSize = 0; } public int size() { return theSize; } public boolean isEmpty() { return (size() == 0); } private Node getNode(int index) { return (getNode(index, 0, size()-1)); } private Node getNode(int index, int lower, int upper) { Node currNode; if (index < lower || index > upper) throw new IndexOutOfBoundsException(); int n = size(); if (index < n/2) { currNode = header.getNext(); for (int i = 0; i < index; i++) currNode = currNode.getNext(); } else { currNode = trailer; for (int i = n; i > index; i--) currNode = currNode.getPrev(); } return currNode; } public AnyType get(int index) { Node indexNode = getNode(index); return indexNode.getData(); } public AnyType set(int index, AnyType newValue) { Node indexNode = getNode(index); AnyType oldValue = indexNode.getData(); indexNode.setData(newValue); return oldValue; } public boolean add(AnyType newValue) { add(size(), newValue); return true; } public void add(int index, AnyType newValue) { addBefore(getNode(index, 0, size()), newValue); } private void addBefore(Node nextNode, AnyType newValue) { Node prevNode = nextNode.getPrev(); Node newNode = new Node<>(newValue, prevNode, nextNode); prevNode.setNext(newNode); nextNode.setPrev(newNode); theSize++; modCount++; } public AnyType remove(int index) { return remove(getNode(index)); } private AnyType remove(Node currNode) { Node prevNode = currNode.getPrev(); Node nextNode = currNode.getNext(); prevNode.setNext(nextNode); nextNode.setPrev(prevNode); theSize--; modCount++; return currNode.getData(); } public ListIterator iterator() { return new LinkedListIterator(); } private class LinkedListIterator implements ListIterator { private Node cursor; private int expectedModCount; public LinkedListIterator() { cursor = header.getNext(); expectedModCount = modCount; } public boolean hasPrevious() { return (cursor != header); } public boolean hasNext() { return (cursor != trailer); } public AnyType previous() { AnyType prevValue = cursor.getData();
cursor = cursor.getPrev();
return prevValue;
} public AnyType next() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); if (!hasNext()) throw new NoSuchElementException(); AnyType nextValue = cursor.getData(); cursor = cursor.getNext(); return nextValue; } public void add(AnyType newValue) { // code here } public void remove() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); DoublyLinkedList.this.remove( ); expectedModCount++; } } }
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