Question: Java/LinkedList package lab4; import java.util.IdentityHashMap; public class IntNode implements Cloneable { private int data; private IntNode next; public IntNode(int d, IntNode n) { data =

Java/LinkedList

package lab4;

import java.util.IdentityHashMap;

public class IntNode implements Cloneable { private int data; private IntNode next; public IntNode(int d, IntNode n) { data = d; next = n; } public IntNode getNext() { return next; } /// Override methods from Object @Override public boolean equals(Object obj) { throw new UnsupportedOperationException("Don't use .equals to compare nodes!"); } @Override public IntNode clone() { try { return (IntNode)super.clone(); } catch (CloneNotSupportedException e) { throw new AssertionError("IntNodes should be cloneable!"); } } protected String originalToString() { return super.toString(); } @Override public String toString() { String nextString = "null"; if (next != null) nextString = next.originalToString(); return super.toString() + "(" + data + "." + nextString + ")"; } /// Print method that is used for testing /** * Convert a list to a string for debugging purposes. * The string is of the form [n1, n2, n3, ...]. * if the list ends in a cycle then we have "..." followed by * the one-based index of the node we go back to. * @param head list t print, may be null * @return string representation of the whole list. */ public static String listToString(IntNode head) { if (head == null) return "[]"; IdentityHashMap m = new IdentityHashMap<>(); StringBuilder sb = new StringBuilder("["); sb.append(head.data); m.put(head, 0); int n = 1; for (IntNode p = head.next; p != null; p = p.next) { Integer boxed = m.get(p); if (boxed != null) { sb.append(", ..." + (n-boxed)); break; } m.put(p, n++); sb.append(", " + p.data); } sb.append("]"); return sb.toString(); } /// Exercises public static IntNode exercise0() { return null; // [] } public static IntNode exercise1() { return null; // TODO [42] } public static IntNode exercise2() { return null; // TODO [1, 2, 3] } public static IntNode exercise3() { return null; // TODO [9, ...1] } public static IntNode exercise4() { return null; // TODO [4, 3, 2, 1, ...3] } public static IntNode exercise5(IntNode param) { return null; // TODO: change second element to 4 } public static IntNode exercise6(IntNode param) { return null; // TODO: remove second element } public static int exercise7(IntNode param) { return 0; // TODO: count number of nodes in list (use NORMAL loop) } public static IntNode exercise8(IntNode param) { final IntNode dummy = new IntNode(999,null); IntNode last = dummy; // "last" node currently is the fake node. // TODO copy list by tacking on each element to the last node // of the result list. Use the NORMAL idiom return dummy.next; } public static IntNode exercise9(IntNode param, int v) { return null; // TODO: add v to the end of a non-empty list (in general!) // NB: We recommend using the FOLLOWER idiom } }

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!