Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with remove methods . 8 public class DoublyLinkedList implements List {/ private class Node { private E value; private Node next, prev; 140

Need help with remove methods

.image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

8 public class DoublyLinkedList implements List {/ private class Node { private E value; private Node next, prev; 140 public Node (E value, Node next, Node prev) { this.value = value; this.next = next; this.prev = prev; public Node(E value) { this(value, null, null); // Delegate to other constructor public Node() { this(null, null, null); // Delegate to other constructor 26 29 public E getValue() { return value; 30 public void setValue(E value) { this.value = value; public Node getNext() { return next; public void setNext(Node next) { this.next = next; public Node getPrev() { return prev; 96 public void setPrev(Node prev) {/ this.prev = prev; public void setPrev(Node prev) { this.prev = prev; public void clear() { value = null; next = prev = null; } // End of Node class private class ListIterator implements Iterator {/ private Node nextNode; public ListIterator() { nextNode = header.getNext(); @Override public boolean hasNext() { return nextNode != null; @Override public E next() { if (hasNext()) { E val = nextNode.getValue(); nextNode = nextNode.getNext(); return val; else throw new NoSuchelementException(); } // End of ListIterator class /* private fields */ private Node header, trailer; // "dummy" nodes private int currentSize; public boolean remove(E obj) { Node curNode = header; Node nextNode = curNode.getNext(); // Traverse the list until we find the element or we reach the end while (nextNode != trailer && !nextNode.getValue().equals(obj)) { curNode = nextNode; nextNode = nextNode.getNext(); // Need to check if we found it if (nextNode != trailer) { // Found it! // If we have A B C, need to get to A C curNode.setNext(nextNode.getNext()); // TODO For a DLL, what else needs to be done? See comment above for a hint. nextNode.clear(); / free up resources currentSize--; return true; else return false; @Override public boolean remove(int index) { Node rmNode; // TODO These variables could be helpful: Node prevNode, nextNode; // Feel free to declare and use them in any methods, but they're not required. // First confirm index is a valid position if (index = size ) throw new IndexOutOfBounds Exception(); // If we have A B C, need to get to A C rmNode = get_node(index); // Get the node that is to be removed // TODO For a DLL. what needs to be done? rmNode.clear(); currentSize--; return true; @Override public int removeAll(E obj) { int counter = 0; Node curNode = header; Node nextNode = curNode.getNext(); /* We used the following in ArrayList, and it would also work here, * but it would have running time of O(n^2). * * while (remove(obi)) counter++; * * // Traverse the entire list while (nextNode != trailer) { if (nextNode.getValue().equals(obj)) { // Remove nextNode /* TODO For a DLL, what needs to be done? * You can declare more Node variables if it helps make things clear. nextNode.clear(); currentSize--; counter++; /* Node that was pointed to by nextNode no longer exists so reset it such that it's still the node after curNode */ nextNode = curNode.getNext(); else { curNode = nextNode; nextNode = nextNode.getNext(); return counter; 8 public class DoublyLinkedList implements List {/ private class Node { private E value; private Node next, prev; 140 public Node (E value, Node next, Node prev) { this.value = value; this.next = next; this.prev = prev; public Node(E value) { this(value, null, null); // Delegate to other constructor public Node() { this(null, null, null); // Delegate to other constructor 26 29 public E getValue() { return value; 30 public void setValue(E value) { this.value = value; public Node getNext() { return next; public void setNext(Node next) { this.next = next; public Node getPrev() { return prev; 96 public void setPrev(Node prev) {/ this.prev = prev; public void setPrev(Node prev) { this.prev = prev; public void clear() { value = null; next = prev = null; } // End of Node class private class ListIterator implements Iterator {/ private Node nextNode; public ListIterator() { nextNode = header.getNext(); @Override public boolean hasNext() { return nextNode != null; @Override public E next() { if (hasNext()) { E val = nextNode.getValue(); nextNode = nextNode.getNext(); return val; else throw new NoSuchelementException(); } // End of ListIterator class /* private fields */ private Node header, trailer; // "dummy" nodes private int currentSize; public boolean remove(E obj) { Node curNode = header; Node nextNode = curNode.getNext(); // Traverse the list until we find the element or we reach the end while (nextNode != trailer && !nextNode.getValue().equals(obj)) { curNode = nextNode; nextNode = nextNode.getNext(); // Need to check if we found it if (nextNode != trailer) { // Found it! // If we have A B C, need to get to A C curNode.setNext(nextNode.getNext()); // TODO For a DLL, what else needs to be done? See comment above for a hint. nextNode.clear(); / free up resources currentSize--; return true; else return false; @Override public boolean remove(int index) { Node rmNode; // TODO These variables could be helpful: Node prevNode, nextNode; // Feel free to declare and use them in any methods, but they're not required. // First confirm index is a valid position if (index = size ) throw new IndexOutOfBounds Exception(); // If we have A B C, need to get to A C rmNode = get_node(index); // Get the node that is to be removed // TODO For a DLL. what needs to be done? rmNode.clear(); currentSize--; return true; @Override public int removeAll(E obj) { int counter = 0; Node curNode = header; Node nextNode = curNode.getNext(); /* We used the following in ArrayList, and it would also work here, * but it would have running time of O(n^2). * * while (remove(obi)) counter++; * * // Traverse the entire list while (nextNode != trailer) { if (nextNode.getValue().equals(obj)) { // Remove nextNode /* TODO For a DLL, what needs to be done? * You can declare more Node variables if it helps make things clear. nextNode.clear(); currentSize--; counter++; /* Node that was pointed to by nextNode no longer exists so reset it such that it's still the node after curNode */ nextNode = curNode.getNext(); else { curNode = nextNode; nextNode = nextNode.getNext(); return counter

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions