Question: When I run the following code, I didn't pass all the tests. I don't know how to fix it. Please help me to fix it.
When I run the following code, I didn't pass all the tests. I don't know how to fix it. Please help me to fix it. I appreciate. import java.util.Iterator; import java.util.ConcurrentModificationException; interface List extends Iterable { void add(T x); // simple add 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; } } 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() { Node head = new Node(null, null, null); head.next = head; head.prev = head; this.head = head;//Save it to state } /** * 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; } public T remove(int i) { if (i < 0 || i >= size()) throw new IndexOutOfBoundsException(); if (head == null) return null; Node temp = head; if(i == 0){ head.next = temp.next.next; return temp.data; } for (int j = 0; temp!=null && j < i-1; j++) temp = temp.next; Node next = temp.next.next; temp.next =next; return next.data; } public T get(int i) { if (i < 0 || i >= size()) throw new IndexOutOfBoundsException(); Node node = head; for(int j = 0; j next; return node.data; } public boolean contains(T x) { boolean found = false; Node current = head.next; for(int j = 0; j < n; j++) { if (current.equals(x)) found = true; current = current.next; } return found; } public int size() { return n; } public Iterator iterator() { return new Iterator() { Node p = head.next; public boolean hasNext() { return p!=head; } public T next() { if(!hasNext()) throw new ConcurrentModificationException(); T ans = p.data; p = p.next; return ans; } public void remove() { // TODO: This must run in O(1) time. This method can be // called only once per call to next(). // Throw an Illegal StateException if next() has not yet been // called, or remove() has already been called after the last // call to next(). if(next()== null) throw new IllegalStateException(); n --; p.prev.prev.next = p; p.prev = p.prev.prev; } }; } 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(); } public static void main(String... args) { List xs = new DoublyLinkedList<>(); int[] a = new int[] { 4, 3, 6, 5, 7, 8 }; for (int x : a) xs.add(x); assert 6 == xs.size(); for (int i = 0; i < a.length; i++) assert xs.get(i) == a[i]; assert !xs.contains(null); for (int x : a) assert xs.contains(x); assert "(4 3 6 5 7 8)".equals(xs.toString()); assert xs.remove(0) == 4; assert xs.remove(1) == 6; assert 4 == xs.size(); assert "(3 5 7 8)".equals(xs.toString()); while (!xs.isEmpty()) xs.remove(xs.size() - 1); assert 0 == xs.size(); assert "()".equals(xs.toString()); for (int x : a) xs.add(x); assert "(4 3 6 5 7 8)".equals(xs.toString()); for (int x : xs) assert xs.contains(x); Iterator it = xs.iterator(); try { it.remove(); assert false; } catch (IllegalStateException ex) { } assert "(4 3 6 5 7 8)".equals(xs.toString()); assert it.hasNext(); assert 4 == it.next(); it.remove(); assert "(3 6 5 7 8)".equals(xs.toString()); try { it.remove(); assert false; } catch (IllegalStateException ex) { } int x; it = xs.iterator(); while (it.hasNext()) if (it.next() % 2 == 0) it.remove(); assert 3 == xs.size(); assert "(3 5 7)".equals(xs.toString()); try { for (Integer i : xs) { if (i != 3) xs.remove(0); } assert false; } catch (ConcurrentModificationException ex) { } assert "(5 7)".equals(xs.toString()); try { for (Integer i : xs) xs.add(i); assert false; } catch (ConcurrentModificationException ex) { } assert "(5 7 5)".equals(xs.toString()); System.out.println("All tests passed..."); } } 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
