/* LwC_Utils.java ** ** Java class containing several utility methods that process ** lists with cursors. ** ** Author: R. McCloskey and .... (students) ** Date: March 2025 */ public class LwC_Utils { /* Displays the items in the given list, from front to rear, each one ** followed by the specified terminator String. */ public static void printList(ListWithCursors list, String terminator) { ListCursor cur = list.getCursor().toFront(); while (!cur.atRear()) { System.out.print(cur.getItem() + terminator); cur.toNext(); } System.out.println(); cur.dispose(); } /* Returns the length of (i.e., number of nodes in) the given list. */ public static int lengthOf(ListWithCursors list) { int cntr = 0; ListCursor c = list.getCursor(); c.toFront(); // loop invariant: cntr = # of nodes preceding c's position while ( !c.atRear() ) { cntr = cntr + 1; c.toNext(); } c.dispose(); return cntr; } /* Returns a new list that contains the items in the given array, and ** in the same order. */ public static ListWithCursors listify(T[] ary) { ListWithCursors list = new LwC_ViaArray(); ListCursor cursor = list.getCursor(); cursor.toRear(); // Insert each array element into the list (immediately before rear). for (int i = 0; i != ary.length; i++) { cursor.insert(ary[i]); } cursor.dispose(); return list; } /* Returns a new cursor positioned, within the given list, at the first ** node (i.e., the one closest to the front) containing an item equal to ** the given one. If there is no such node, the returned cursor is ** positioned at the rear of its list. */ public static ListCursor find(ListWithCursors list, Object item) { return list.getCursor(); // STUB!! } /* If the given list has no node containing an item equal to the given one, ** a new node containing the given item is inserted at the rear of the ** list. Otherwise, the list is not changed. ** Hint: Use find() */ public static void insertNonDuplicate(ListWithCursors list, Object item) { // STUB! } /* Within the given list, removes any node whose predecessor contains ** an item equal to its own. */ public static void removeAdjacentDuplicates(ListWithCursors list) { if (list.isEmpty()) { // there can be no duplicates to remove! } else { ListCursor pred = list.getCursor().toFront(); ListCursor crrnt = list.getCursor().toFront().toNext(); /* loop invariant: there are no adjacent duplicates in the * portion of list preceding crrnt && * pred and crrnt are at adjacent positions, the latter being * the successor of the former */ while ( !crrnt.atRear() ) { if (pred.getItem().equals(crrnt.getItem())) { crrnt.remove(); } else { pred.toNext(); // or, equivalently, pred.setTo(crrnt); crrnt.toNext(); } } pred.dispose(); crrnt.dispose(); } } /* Starting at the given cursor's position and continuing to the rear of ** its list, removes every node containing an item equal to the one given. ** (Note: Use the equals() method, NOT the == relational operator!!) ** As a side effect, the given cursor ends up at the rear position of ** its list. */ public static void removeRest(ListCursor cursor, Object item) { // STUB! } /* Within the given list, removes every node containing an item that ** occurs ealier in the list (i.e., closer to the front). That is, ** only the first occurrence of each item remains in the list. ** Hint: Use the removeRest() method (see above) repeatedly, each ** time passing to it a local cursor variable. */ public static void removeDuplicates(ListWithCursors list) { // STUB! } /* Modifies the given list by reversing the order in which its items occur. */ public static void reverse(ListWithCursors list) { // STUB! } }