Question: Please help me to complete the following code. Thank you! DoublyLinkedList.java /** *Add an overloaded remove() method to the List interface which allows * us
Please help me to complete the following code. Thank you!
DoublyLinkedList.java
/** *Add an overloaded remove() method to the List interface which allows * us to remove a specific element from a list. The return value should be * a boolean indicating whether or not the remove() was successful.
*/
import java.util.Iterator; import java.util.ConcurrentModificationException; interface Listextends Iterable { boolean remove(T item); void add(T x); T remove(int i); T get(int i); boolean contains(T x); int size(); default boolean isEmpty() { return size() == 0; } } public class DoublyLinkedList implements List { class Node { T data; Node next, prev; Node(T data) { this(data, null, null); } Node(T data, Node prev, Node next) { this.data = data; this.prev = prev; this.next = next; } } int modCount; final Node head; // always points to the headnode for this list int n; // the number of nodes in this list, initially 0 /** * Creates the empty list. */ public DoublyLinkedList() { head = new Node(null); head.next = head; head.prev = head; } /** * Inserts the value x at the end of this list. */ public void add(T x) { n ++; Node last = head.prev; Node curr = new Node(x, last, head); last.next = curr; head.prev = curr; modCount++; } public boolean remove(T item){ //TODO return false; } public T remove(int i) { Node next = head.next; if (i < 0 || i >= size()) throw new IndexOutOfBoundsException(); for (int j = 0; j < i; j++) { next = next.next; } if(i == 0){ head.next = next.next; }else{ next.prev.next=next.next; next.next.prev=next.prev; } n--; modCount++; return next.data; } public T get(int i) { if (i < 0 || i >= size()) throw new IndexOutOfBoundsException(); Node node = head.next; for(int j = 0; j < i; j++){ node = node.next; } return node.data; } public boolean contains(T x) { boolean a = false; Node r = head.next; while (r !=head) { if (r.data.equals(x)) { a = true; } r = r.next; } return a; } public int size() { return n; } public Iterator iterator() { return new Iterator () { Node p = head.next; int EModcount = modCount; boolean RDRemove; public boolean hasNext() { return p!=head; } public T next() { if(modCount != EModcount) throw new ConcurrentModificationException(); T ans = p.data; p = p.next; RDRemove=true; return ans; } public void remove() { if(EModcount!=modCount) throw new ConcurrentModificationException(); if(!RDRemove) throw new IllegalStateException(); n --; p.prev.prev.next = p; p.prev = p.prev.prev; EModcount++; modCount++; RDRemove = false; } }; } public String toString() { if (isEmpty()) return "()"; Iterator it = iterator(); StringBuilder ans = new StringBuilder("(").append(it.next()); while (it.hasNext()) ans.append(" ").append(it.next()); return ans.append(")").toString(); } }
Testing.java
public class Testing { @Test public void testDLL() { List xs = new DoublyLinkedList<>(); int[] a = new int[] { 4, 3, 6, 5, 7, 8 }; for (int x : a) xs.add(x); assertEquals(6, xs.size()); for (int i = 0; i < a.length; i++) { int x = xs.get(i); assertEquals(a[i], x); } assertFalse(xs.contains(null)); for (int x : a) assertTrue(xs.contains(x)); assertEquals("(4 3 6 5 7 8)", xs.toString()); int val = xs.remove(0); assertEquals(4, val); val = xs.remove(1); assertEquals(6, val); assertEquals(4, xs.size()); assertEquals("(3 5 7 8)", xs.toString()); while (!xs.isEmpty()) xs.remove(xs.size() - 1); assertEquals(0, xs.size()); assertEquals("()", xs.toString()); for (int x : a) xs.add(x); assertEquals("(4 3 6 5 7 8)", xs.toString()); for (int x : xs) assertTrue(xs.contains(x)); } @Test public void testDLLExceptions() { List xs = new DoublyLinkedList<>(); int[] a = new int[] { 4, 3, 6, 5, 7, 8 }; for (int x : a) xs.add(x); Iterator it = xs.iterator(); try { it.remove(); fail(); } catch (IllegalStateException ex) { } assertEquals("(4 3 6 5 7 8)", xs.toString()); assertTrue(it.hasNext()); int val = it.next(); assertEquals(4, val); it.remove(); assertEquals("(3 6 5 7 8)", xs.toString()); try { it.remove(); fail(); } catch (IllegalStateException ex) { } it = xs.iterator(); while (it.hasNext()) if (it.next() % 2 == 0) it.remove(); assertEquals(3, xs.size()); assertEquals("(3 5 7)", xs.toString()); try { for (Integer i : xs) { if (i != 3) xs.remove(0); } fail(); } catch (ConcurrentModificationException ex) { } assertEquals("(5 7)", xs.toString()); try { for (Integer i : xs) xs.add(i); fail(); } catch (ConcurrentModificationException ex) { } assertEquals("(5 7 5)", xs.toString()); try { for (int x : xs) { it = xs.iterator(); while (it.hasNext()) { int y = it.next(); if (y == 7) it.remove(); } } fail(); } catch (ConcurrentModificationException ex) { } } @Test public void testDLLOverloadedRemove() { List xs = new DoublyLinkedList<>(); String[] a = new String[] { "cat", "dog", "pig", "cow", "rat", "bat" }; for (String x : a) xs.add(x); assertTrue(xs.remove("cat")); assertEquals("dog", xs.get(0)); assertTrue(xs.remove("bat")); assertEquals("rat", xs.get(xs.size() - 1)); assertTrue(xs.remove(new String("pig"))); assertEquals("cow", xs.get(1)); }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
