Wellesley College CS111: Lists in IntLists with Methods and Examples, Study notes of Computer Science

This document from wellesley college's cs111 computer programming course covers the topic of lists represented as intlists. The concept of box-and-pointer notation, the usefulness of lists, and five core intlist methods: isempty, head, tail, prepend, and empty. The document also includes examples of constructing intlists using empty() and prepend() methods, as well as sharing list nodes and recursive list methods such as sumlist, length, maxlist, and areallpositive.

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-v7i
koofers-user-v7i 🇺🇸

10 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS111 Computer Programming
Department of Computer Science
Wellesley College
Lists
Checking It Twice
Friday, October 26, 2007
Lists 14-2
IntLists represent lists of integers
IntLists are defined recursively:
An IntList is either
othe empty list, or
oa (non-empty) list node with two parts:
1. a head which is an integer, and
2. a tail which is another IntList.
8head
tail
11head
tail
-2head
tail
IntListIntListIntList IntList
empty
// list node // list node // list node // empty list
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Wellesley College CS111: Lists in IntLists with Methods and Examples and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science

Wellesley College

Lists

Checking It Twice

Friday, October 26, 2007

Lists 14-

IntLists represent lists of integers

IntLists are defined recursively:

An IntList is either

o the empty list, or

o a (non-empty) list node with two parts:

1. a head which is an integer, and

2. a tail which is another IntList.

head 8 tail

head 11 tail

head- tail

IntList IntList IntList IntList

empty

// list node // list node // list node // empty list

Lists 14-

Box-and-pointer notation

We represent list nodes and empty lists using a shorthand known as box-and-pointer notation.

head 8 tail

head 11 tail

head- tail

IntList IntList IntList IntList

empty

8 11 -2 Ø

// list node // list node // list node // empty list

Lists 14-

Why are lists useful?

Lists are good for storing many numbers (but you don’t know how many in

advance).

91 0 -22 ... (^38) Ø

list

Lists are also very flexible. E.g., here is a new list which starts

with 21, and then the rest are the same as before.

21 91 0 ... (^38) Ø

list

We could create list2 from scratch, or we could use the first list

as the tail of a new list.

21

list

Lists 14-

isEmpty method

IntList.isEmpty(list1)

IntList.isEmpty(list2)

IntList.isEmpty(list3)

public static boolean isEmpty (IntList L) Returns true if L is an empty integer list and false if L is an integer list node.

1 2 3 Ø

5 -5 Ø

Ø

list list list

Lists 14-

head method

IntList.head(list1)

IntList.head(list2)

IntList.head(list3)

public static int head (IntList L) Returns the integer that is the head component of the integer list node L. Signals an exception if L is empty.

1 2 3 Ø

5 -5 Ø

Ø

list list list

Lists 14-

tail method

IntList.tail(list1)

IntList.tail(list2)

IntList.tail(list3)

public static IntList tail (IntList L) Returns the integer list that is the tail component of the integer list node L. Signals an exception if L is empty.

1 2 3 Ø

5 -5 Ø

Ø

list list list

Lists 14-

heads and tails

IntList.head(IntList.tail(list2))

IntList.head(IntList.tail(IntList.tail(list2)))

1 2 3 Ø

5 -5 Ø

Ø

list list list

Lists 14-

Sharing list nodes

list4^4

IntList list4 = IntList.prepend(4, IntList.prepend(3, IntList.prepend(2, IntList.prepend(1, IntList.empty()))));

IntList list5 = IntList.prepend(7, IntList.prepend(4, IntList.tail(IntList.tail(list4))));

1 Ø

list5 7 4 list6 8

IntList list6 = IntList.prepend(8, IntList.tail(list5));

Lists 14-

Recursive list methods

// Returns the integer sum of all elements in L

public static int sumList (IntList L) {

Lists 14-

Recursive list methods

// Returns the number of elements in list L

public static int length (IntList L) {

Lists 14-

Recursive list methods

// Returns the maximum number in list L

public static int maxList (IntList L) {

Lists 14-

Squaring numbers

o Method mapSquares(IntList L) returns a list, such that each element in the list is the “square” of the corresponding element in “L”. o For example, mapSquares(L)

2 Ø

Lists 14-

mapSquares(IntList L);

// Returns a new list whose elements are the sqaures of // the corresponding elements in the given list public static IntList mapSquares(IntList L) {

if ( ) { // base case return } else { // recursive case return

Lists 14-

mapSquares(IntList L);

// Returns a new list whose elements are the sqaures of // the corresponding elements in the given list public static IntList mapSquares(IntList L) {

if ( InList.isEmpty(L) ) { // base case return empty(); } else { // recursive case return IntList.prepend(IntList.head(L)*IntList.head(L), mapSquares(IntList.tail(L))); } }

Lists 14-

Filter methods

Afilter method passes some elements through and holds other elements back.

More formally: Afilter method transforms one list to another by keeping only those elements satisfying a given predicate.

L1 L

filter

Lists 14-

filterEven(IntList L);

// Returns sublist containing elements of L that are even public static IntList filterEven(IntList L) {

if ( IntList.isEmpty(L) ) { // base case return IntList.empty(); } else if ( (IntList.head(L) % 2) == 0 ) { // recursive case return IntList.preprend(IntList.head(L), filterEven(IntList.tail(L))); } else { return filterEven(IntList.tail(L)); } }