class 5 - lists
DESCRIPTION
Class 5 - Lists. The list data type Recursive methods on lists. Data structures. Contain large amounts of data Allow for access to that data Many different data structures, allowing efficiency for various operations. Lists. - PowerPoint PPT PresentationTRANSCRIPT
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 1
Class 5 - Lists
The list data typeRecursive methods on lists
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 2
Data structures
Contain large amounts of dataAllow for access to that dataMany different data structures, allowing
efficiency for various operations.
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 3
Lists
Lists are a simple data structure in which data are stored in a row.
We will talk first about lists of integers:x0, x1, ..., xn-1 (n >= 0)
Elements can be added and removed only at the beginning (x0) --- We don’t consider mutable lists.
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 4
Lists (cont.)
Terminology: First element (x0) is head of the list
List of remaining elements (x1, ..., xn-1) is tail of the list
Adding a new element to the front is called consing (for “constructing”)
The nil list is the list with no elements (n=0)
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 5
List operations
We assume a type IntList, for variables that contain such lists
We assume a class IL that defines the following class method: IntList cons (int i, IntList L) -
construct list containing i at front
And a class variable: IntList nil - the empty (zero-element) list
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 6
List operations (cont.)
IntList objects have the following instance methods: int hd () - return head of the list IntList tl () - return tail of the list boolean empty ()- is the list empty?
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 7
Examples of list operations
Suppose L is the list 2, 3, 5, 7
IL.cons(2,IL.cons(3,IL.cons(5,IL.cons(7,IL.nil)))) L.hd() returns 2L.tl() returns the list 3, 5, 7L.empty() returns falseIL.cons(13, L) returns the list 13, 2, 3, 5, 7IL.cons(13, L.tl()) returns the list
13, 3, 5, 7L.tl().tl() returns the list 5, 7
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 8
Example
We assume we have defined the classes IntList and IL as described above.
public static void main (String[] args) { IntList L = IL.nil; L = IL.cons(5, L); L = IL.cons(10, L); System.out.println(L.hd()); System.out.println(L.tl().hd());}
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 9
Recursion on lists
Writing recursive methods on lists follows the same principle as for integers: To compute f(L), assume f(L’) can be calculated
for lists L’ smaller than L, and use f(L’) to calculate f(L).
Some lists are small enough for f to be calculated directly
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 10
Example: printing lists
Print all the elements in a list, one per line: Assume you can print the tail of L (i.e. printList(L.tl())), so how do you print L?
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 11
Example: printing lists (cont.)
static void printList (IntList L) { if (!L.empty()) { System.out.println(L.hd()); printList(L.tl()); }}
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 12
Example: printing lists (cont.)
Print all the elements in a list, all on one line, separated by a comma and a space: Assume you can print the tail of L (i.e. printList(L.tl())), so how do you print L?
Answer: print L.hd(), followed by a comma and space, but only if the tail of L is non-empty.
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 13
Example: printing lists (cont.)
static void printList (IntList L) { if (L.empty()) return; else if (L.tl().empty())) System.out.print(L.hd()); else { System.out.print(L.hd()); System.out.print(“, “); printList(L.tl()); }}
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 14
Example: addtoend
The method
can be defined recursively. It adds the integer i at the end of the list L. For example, if L is the list 3, 5, 7, then addtoend(L, 2) returns the list 3, 5, 7, 2.
We will see how to define addtoend in the next class, but we can use it for ...IntList addtoend (IntList L, int i)
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 15
Example: reading integers
Given integer n, read n integers from the user and place them in a list. Assume you can read n-1 integers and place them in
a list; how can you read n integers? Answer: read the n-1 integers, creating a list L, then
read one more integer and place it at the end of L.
8/2/00 SEM107- © Kamin and Reddy Class 5 - Lists - 16
Example: reading integers (cont.)
static IntList readList (int n) { if (n == 0) return IL.nil; else { IntList L = readList(n-1); int i = Keyboard.readInt(); return addtoend(L, i); }}