Presentation is loading. Please wait.

Presentation is loading. Please wait.

8/2/00SEM107- © Kamin and ReddyClass 5 - Lists - 1 Class 5 - Lists r The list data type r Recursive methods on lists.

Similar presentations


Presentation on theme: "8/2/00SEM107- © Kamin and ReddyClass 5 - Lists - 1 Class 5 - Lists r The list data type r Recursive methods on lists."— Presentation transcript:

1 8/2/00SEM107- © Kamin and ReddyClass 5 - Lists - 1 Class 5 - Lists r The list data type r Recursive methods on lists

2 8/2/00SEM107- © Kamin and ReddyClass 5 - Lists - 2 Data structures r Contain large amounts of data r Allow for access to that data r Many different data structures, allowing efficiency for various operations.

3 8/2/00SEM107- © Kamin and ReddyClass 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: x 0, x 1,..., x n-1 (n >= 0) Elements can be added and removed only at the beginning (x 0 ) --- We don’t consider mutable lists.

4 8/2/00SEM107- © Kamin and ReddyClass 5 - Lists - 4 Lists (cont.) Terminology: m First element (x 0 ) is head of the list m List of remaining elements (x 1,..., x n-1 ) is tail of the list m Adding a new element to the front is called consing (for “constructing”) m The nil list is the list with no elements (n=0)

5 8/2/00SEM107- © Kamin and ReddyClass 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 r And a class variable:  IntList nil - the empty (zero-element) list

6 8/2/00SEM107- © Kamin and ReddyClass 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?

7 8/2/00SEM107- © Kamin and ReddyClass 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 2  L.tl() returns the list 3, 5, 7  L.empty() returns false  IL.cons(13, L) returns the list 13, 2, 3, 5, 7  IL.cons(13, L.tl()) returns the list 13, 3, 5, 7  L.tl().tl() returns the list 5, 7

8 8/2/00SEM107- © Kamin and ReddyClass 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()); }

9 8/2/00SEM107- © Kamin and ReddyClass 5 - Lists - 9 Recursion on lists Writing recursive methods on lists follows the same principle as for integers: m To compute f(L), assume f(L’) can be calculated for lists L’ smaller than L, and use f(L’) to calculate f(L). m Some lists are small enough for f to be calculated directly

10 8/2/00SEM107- © Kamin and ReddyClass 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 ?

11 8/2/00SEM107- © Kamin and ReddyClass 5 - Lists - 11 Example: printing lists (cont.) static void printList (IntList L) { if (!L.empty()) { System.out.println(L.hd()); printList(L.tl()); }

12 8/2/00SEM107- © Kamin and ReddyClass 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.

13 8/2/00SEM107- © Kamin and ReddyClass 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()); }

14 8/2/00SEM107- © Kamin and ReddyClass 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)

15 8/2/00SEM107- © Kamin and ReddyClass 5 - Lists - 15 Example: reading integers r Given integer n, read n integers from the user and place them in a list. m Assume you can read n-1 integers and place them in a list; how can you read n integers? m Answer: read the n-1 integers, creating a list L, then read one more integer and place it at the end of L.

16 8/2/00SEM107- © Kamin and ReddyClass 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); }


Download ppt "8/2/00SEM107- © Kamin and ReddyClass 5 - Lists - 1 Class 5 - Lists r The list data type r Recursive methods on lists."

Similar presentations


Ads by Google