Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me finish the TODO in the following code. I appreciate. SelfAdjustingList.java public class SelfAdjustingList extends DoublyLinkedList { // TODO: New code goes here.

Please help me finish the TODO in the following code. I appreciate.

SelfAdjustingList.java

public class SelfAdjustingList extends DoublyLinkedList {  // TODO: New code goes here.   /**  * TODO:Add more tests.  */   public static void main(String... args) { SelfAdjustingList xs = new SelfAdjustingList<>(); for (int x = 1; x <= 10; x++) xs.add(x); for (int i = 0; i < xs.size(); i++) assert 10 - i == xs.get(i); for (int i = 0; i < xs.size(); i++) { int x = xs.get(i); assert x == xs.find(i); } for (int i = 0; i < xs.size(); i++) { int x = xs.find(i); assert x == xs.get(0); } System.out.println("All tests passed..."); } } 

DoublyLinkedList.java TODO: When I run this, I didn't pass all the tests. Please help me fix it. Thank you!

import java.util.Iterator; import java.util.ConcurrentModificationException;  interface List extends Iterable { 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; } } 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

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

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What must a creditor do to become a secured party?

Answered: 1 week ago

Question

When should the last word in a title be capitalized?

Answered: 1 week ago