/* An instance of this Java class has several "utility" methods pertaining to ** Recursive Lists containing integer values. ** ** Author: R. McCloskey and < Lab TEAM MEMBERS > ** Last modified: October 2020 ** Known defects: ... */ public class RecListLabUtils { // Symbolic constant for the empty list. Use a static method // in RecursiveListEmpty to obtain an empty list. private static final RecursiveList EMPTY = RecursiveListEmpty.emptyListFactory(); /* Returns a one-element list containing k. */ public RecursiveList singleton(int k) { return EMPTY.cons(k); } /* Returns a recursive list whose elements are those in the given ** array, and in the same order. */ public RecursiveList listify(Integer[] a) { RecursiveList result = EMPTY; // Place array elements into the list from the end of the array // to the beginning. for (int i = a.length; i != 0; i--) { result = result.cons(a[i-1]); } return result; } /* Returns the length of the given list. ** Logic: An empty list has length zero and the length of a ** non-empty list is one more than the length of its tail. */ public int lengthOf(RecursiveList list) { if (list.isEmpty()) { return 0; } else { return 1 + lengthOf(list.tailOf()); } } /* Returns the k-th element of the given list (counting from 1). ** pre: 0 < k <= lengthOf(list) ** Logic: The first element of a list is its head. For k>1, the ** k-th element of a list is the (k-1)-st element of its tail. */ public int get(RecursiveList list, int k) { if (k == 1) { return list.headOf(); } else { return get(list.tailOf(), k-1); } } /* Returns the list obtained by inserting the given integer into ** the k-th position in the given list (where positions are numbered ** starting at 1). ** pre: 1 <= k <= lengthOf(list)+1 ** Logic: To insert an item at position 1, we simply place it at ** the head of the list. ** To insert an item at position k (k>1), we use recursion to ** to produce the list that results from inserting that item ** into position k-1 of the tail. The result is completed by ** "re-attaching" the head using cons(). */ public RecursiveList put(RecursiveList list, int item, int k) { if (k == 1) { return list.cons(item); } else { RecursiveList rest = put(list.tailOf(), item, k-1); return rest.cons(list.headOf()); } } /* Returns the number of times the given integer appears in the given list. ** Example: 4 appears three times in [4, 7, 9, 4, 3, 9, 4, 8, 5]. */ public int numOccur(RecursiveList list, int k) { return 0; // STUB! } /* Returns a list identical to the given one, except that the first ** occurrence of the given number (if it exists) is omitted. ** Example: Given the list [5, 12, 3, 9, 4, 3, 5, 3, 7] and k = 3, ** the resulting list should be [5, 12, 9, 4, 3, 5, 3, 7] */ public RecursiveList removeFirst(RecursiveList list, int k) { return EMPTY; // STUB! } /* Returns a list identical to the given one, except that all ** occurrences of the given number are omitted. ** Example: Given the list [5, 12, 3, 9, 4, 3, 5, 3, 7] and k = 3, ** the resulting list should be [5, 12, 9, 4, 5, 7] */ public RecursiveList removeAll(RecursiveList list, int k) { return EMPTY; // STUB! } /* Returns a list containing the prefix of length k of the given list. ** (If the given list has fewer than k elements, the whole of it is ** returned.) ** Example: Given the list [3,9,11,5,7,0] and k = 4, the resulting list ** should be [3,9,11,5] */ public RecursiveList prefixOf(RecursiveList list, int k) { return EMPTY; // STUB! } /* Returns a list containing the suffix of given list obtained by ** "chopping off" its prefix of length k. ** (If the given list has fewer than k elements, the empty list is ** returned.) ** Example: Given the list [3,9,11,5,7,0] and k = 4, the resulting list ** should be [7,0] */ public RecursiveList suffixOf(RecursiveList list, int k) { return EMPTY; // STUB! } /* Reports whether or not the elements in the given list are in ascending ** order (meaning that each element is <= to the one that follows it). */ public boolean isAscending(RecursiveList list) { return false; // STUB! } /* Returns the list obtained by "inserting" k into its rightful place ** within the given list, whose elements are assumed to be in ascending ** order. Example: Given the list [2, 5, 6, 10, 14, 23] and k = 11, ** the list returned should be [2, 5, 6, 10, 11, 14, 23] ** pre: isAscending(list) */ public RecursiveList orderedInsert(RecursiveList list, int k) { if (list.isEmpty()) { return singleton(k); } // one-node list containing k else { return list; // STUB! } } }