Question: You are provided an implementation singly-linked list and a partially implemented Iterator class. Starting with this implementation, modify it so that it is a doubly-linked

You are provided an implementation singly-linked list and a partially implemented

Iterator class. Starting with this implementation, modify it so that it is a doubly-linked list,

and finish the implementation of a class for an Iterator that can traverse this list both

forwards and backwards

public class LinkedList {

Node itsFirstNode; Node itsLastNode; private int size;

public LinkedList() { itsFirstNode = null; itsLastNode = null; size = 0; }

public int size() { return this.size; } public Iterator getIterator() { return new Iterator(this); }

// THIS WILL NEED TO BE MODIFIED FOR DOUBLY LINKED LIST public void add(T element) {

Node node = new Node(element);

if (itsFirstNode == null) { itsFirstNode = node; itsLastNode = node; } else { itsLastNode.setNextNode(node); itsLastNode = node; } size++; }

// THIS WILL NEED TO BE MODIFIED FOR DOUBLY LINKED LIST public void add(T element, int index) { int counter = 0; Node newNode = new Node(element); Node current = itsFirstNode; while (current != null ) { if (counter == index - 1 ) break; current = current.getNextNode(); counter++; } newNode.setNextNode(current.getNextNode()); current.setNextNode(newNode); size++; }

public T get(int index) { int counter = 0; Node current = itsFirstNode; while (current != null ) { if (counter == index) break; current = current.getNextNode(); counter++; } return current.getData(); }

// TO BE IMPLEMENTED /* // returns true if element is in the list, false if not public boolean contains(T element) {

}

// returns the index of the element if it is in the list, -1 if not found public int indexOf(T element) {

}

// returns an Iterator at the location of the element if it is in the list // returns the null reference if the element is not found public Iterator iteratorAt(T element) {

} */

public String toString() { String returnVal = ""; Node current = itsFirstNode; if (size != 0 ) { while (current != null ) { returnVal += current.toString(); returnVal += " "; current = current.getNextNode(); } } return returnVal; } // end toString

class Node { private T data; private Node itsNext; private Node itsPrior; public Node(T data) { itsNext = null; itsPrior = null; this.data = data; } public T getData() { return this.data; } public Node getNextNode() { return itsNext; }

// TO BE IMPLEMENTED /* public Node getPriorNode() {

} */ public void setNextNode(Node next) { itsNext = next; }

// TO BE IMPLEMENTED /* public void setPriorNode(Node prior) {

} */ public String toString() { return data.toString(); } } // end of Node }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!