Integer Linked Lists An integer list is either: (1) empty, - - PDF document

integer linked lists
SMART_READER_LITE
LIVE PREVIEW

Integer Linked Lists An integer list is either: (1) empty, - - PDF document

Integer Linked Lists An integer list is either: (1) empty, represented by (null) Lists, Too or (2) a listNode , whose head is an integer and whose tail is an integer list L 17 -8 4 O - 1 O - 2 Class Methods of IntList Contract


slide-1
SLIDE 1

O - 1

Lists, Too

O - 2

Integer Linked Lists

An integer list is either: (1) empty, represented by Ø (null)

  • r

(2) a listNode, whose head is an integer and whose tail is an integer list

17

  • 8

4

Ø L

O - 3

Class Methods of IntList Contract

public static IntList empty()

Returns an empty integer list.

public static IntList prepend (int n, IntList L)

Returns a new integer list node whose head is n and whose tail is L.

public static boolean isEmpty (IntList L)

Returns true if L is an empty integer list and false if L is an integer list node.

public static int head (IntList L)

Returns the integer that is the head component of the integer list node L.

public static IntList tail (IntList L)

Returns the integer list that is the tail component of the integer list node L.

public static boolean equals (IntList L1, IntList L2)

Returns true if L1 and L2 have the same length and the same elements in the same order; otherwise returns false .

public static String toString (IntList L)

Returns a string representation of of the integer list L in which the integer list elements are separated by commas and delimited by square brackets. For example, if L1 is a list containing the sequence of integers 6, -17 and 42, then toString(L1) returns "[6,-17,42]" .

public static IntList fromString (String s)

If s is the printed representation of an integer list (i.e., comma separated integers delimited by square brackets), returns an IntList with that representation. For example, fromString("[7,-2,4]") returns an 3-element integer list with elements 7,-2 , and 4. O - 4

// Returns true if all the elements in L are positive, false otherwise public static boolean areAllPositive(IntList L) { }

Recursive List Method

O - 5

// Returns true if at least one element in L is positive, false otherwise public static boolean isSomePositive(IntList L) { }

Recursive List Method

O - 6

Filter Methods

More formally: A filter method transforms one list to another by keeping only those elements satisfying a predicate

L1 L2

filter

A filter method passes some elements through and holds other elements back.

slide-2
SLIDE 2

O - 7

Filter Methods

Example: Create a method which keeps only the even elements of an integer list Solution (in English):

  • Solve this problem recursively by solving a smaller instance of the

same problem; e.g., tail(list)

  • Suppose we have a method which returns an IntList containing only

the even elements of the tail(list)

  • Look at head(list) → if even number, then “keep it” (prepend it to

filterEvens(tail(list)) else just process the tail recursively

O - 8

filterEvens

Solution (in Java):

// Returns a new list with only the even elements of list L public static IntList filterEvens(IntList L) { }

What about odds? What about negatives?

O - 9

Lists of Objects

The ObjectList class is similar to the IntList class, except that elements of lists are objects instead

  • f integers.

The objects in an object list do not have to be the same type.

Ø L

Point Buggle Color

O - 10

Almost Everything in Java is an Object

Object

Buggle Color Point

O - 11

Recursive ObjectList Method

// Returns the number of elements in list L public static int length(ObjectList L) { }

O - 12

Recursive ObjectList Method

Example: Let’s write a method to find the nth element of an object list. Suppose we had a list of string objects:

Ø L

“Boo” “happy” “halloween”

nth(2, L) nth(1, L)

slide-3
SLIDE 3

O - 13

Recursive ObjectList Method

// Returns the nth element in list L public static Object nth(int n, ObjectList L) { }

O - 14

And Speaking of Strings...

We’ve seen character values (in single quotes):

char firstLetter = ‘B’;

Now let’s look at strings (in double quotes):

String fruit = “banana”; char letter = fruit.charAt(1); int numCharacters = fruit.length(); int index = fruit.indexOf(‘a’); int indexStartingAt2 = fruit.indexOf(‘a’, 2);

O - 15

Recursive List Method

Write a method which determines if a given integer is in a given list. The method should return true if the integer is in the list, and false otherwise.

17

  • 8

4

Ø L

Examples: isIn(15, L) isIn(-8, L)

O - 16

Recursive List Method

// Returns true if int x is in the list L, false otherwise public static boolean isIn(int x, IntList L) { }