Up: Unit 12 Previous: Summary of the graphical

## Exercises

Exercise 12.1 Provide an iterative implementation of the following operations on lists of strings:

• public static int length(ListNode lis)
that returns the length of lis;
• public static ListNode insertAfter(ListNode lis, String s, String given)
that returns the list obtained by modifying lis by inserting a new element s after a given element, if such an element is present, and returns lis unmodified otherwise;
• public static ListNode modify(ListNode lis, String old, String ne)
that returns the list obtained by modifying lis by changing the first occurrence of old to ne;
• public static ListNode modifyAll(ListNode lis, String old, String ne)
that returns the list obtained by modifying lis by changing all occurrences of old to ne;
• public static ListNode insertBefore(ListNode lis, String s, String given)
that returns the list obtained by modifying lis by inserting a new element s before a given element, if such an element is present, and returns lis unmodified otherwise;
• public static ListNode copy(ListNode lis)
that returns a copy of the list lis, i.e., a list containing the same sequence of elements as lis, but using new nodes;
• public static ListNode invert(ListNode lis)
that modifies lis by inverting the links among its nodes, and returns a reference to the inverted list;
• public static ListNode deleteDoubles(ListNode lis)
that returns the list obtained by modifying lis by deleting all occurrences of an element apart from the first one.
• public static boolean searchSequence(ListNode lis1, ListNode lis2)
that checks whether the lis lis1 contains a subsequence of consecutive elements that coincides with lis2. For example, if the sequence of elements of lis1 is (A B B C D E), and the sequence of elements of lis2 is (B B C), the result should be true. Instead, if lis1 is again (A B B C D E) and lis2 is (A B C), the result should be false.

Exercise 12.2 Provide a recursive implementation of the operations of Exercise 12.1.

Exercise 12.3 Whenever on the domain of the elements of a list an order is defined, we can consider ordered lists, in which the sequence of elements in a list is ordered. Provide a recursive and an iterative implementation of the following operations on ordered lists of integers:

• check whether a list is actually ordered;
• search of a given element, exploiting the fact that the list is ordered to interrupt the search when the element has not been found and we know that it cannot appear in the rest of the list;
• insertion of a new element, maintaining the list ordered;
• deletion of a given element;
• merge of two ordered lists into a new ordered list containing the elements of both lists (but whose nodes are newly created);
• merge of two ordered lists into an ordered list that reuses the nodes of the two lists (hence, the two lists are destroyed by the method call);
• intersection of two ordered lists, to produce a new ordered list containing exactly those elements that are present in both lists.

Exercise 12.4 Define a class ListOfPersons, each of whose objects maintains the information about a sequence of objects of a class Person (for each person, the information of interest are the name, surname, age, and city of residence). The class ListOfPersons should provide, besides the methods for insertion, deletion and modification of a person, a method to print all persons in the list, a method to count the number of persons in the list that live in a given city passed as parameter, and a method that computes the average age of all persons in the list.

Exercise 12.5 Solve Esercise 7.12 by removing the assumption that at most 10 persons live in an apartment, and handling such an information by means of a linked list, possibly using the class defined in the preceding exercise. Add to the class Apartment a method countPersons() that returns the number of persons living in the apartment, and checkFamily() that checks whether all persons living in the apartment have the same surname.

Exercise 12.6 Define a class ListCDs that maintains information about a sequence of objects of a class CD (for each CD, the information of interest are author, title, publication year, and price). The class ListCDs should provide, besides the methods for inserting, deleting and printing the elements of a list, a method that calculates the total price of the CDs contained in the list, and a method that returns the list of CDs of a given author passed as parameter.

Exercise 12.7 Define a class MyString, that handles sequences of characters (by means of a list of char) and implements some methods similar to those of the class String. Specifically, implement a constructor that creates an object of the class starting from a string, and the methods concat(), equals(), substring(), length(), charAt(), indexOf(), and toUpperCase(). Realize also a method, external to the class MyString, that behaves in the same way as the method Integer.parseInt(). More precisely, given an object of the class MyString, the method should return the integer value corresponding to the numeric digits represented in the MyString object, or should throw an exception if the object contains characters that are different from the digits '0', ..., '9'. Implement the latter method both using iteration and using recursion.

Exercise 12.8 Define a class BigNumber, whose objects represent positive integers with an unbounded number of digits, realized by means of a list of integers. The class should implement a constructor that creates a BigNumber object starting from a string of numeric digits, the methods to calculate the sum and difference of two BigNumbers, the predicates to compare whether two BigNumbers are equal and whether one is smaller than another, and the method toString that constructs the string corresponding to the number. For the difference, return the object representing the number 0 in the case where the result of the subtraction would be negative. We suggest to represent a BigNumber using a list of integers in which the least significant digit is the first element of the list, and the most significant digit the last element of the list.

Up: Unit 12 Previous: Summary of the graphical