integer linked lists
play

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


  1. 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 Recursive List Method public static IntList empty() Returns an empty integer list. // Returns true if all the elements in L are positive, false otherwise 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 areAllPositive(IntList 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 - 3 O - 4 Recursive List Method Filter Methods // Returns true if at least one element in L is positive, false otherwise A filter method passes some elements through and public static boolean isSomePositive(IntList L) holds other elements back. { More formally: A filter method transforms one list to another by keeping only those elements satisfying a predicate filter L1 L2 } O - 5 O - 6

  2. Filter Methods filterEvens Example: Create a method which keeps only the even Solution (in Java): elements of an integer list // Returns a new list with only the even elements of list L Solution (in English): public static IntList filterEvens(IntList L) { • 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 What about odds? What about negatives? O - 7 O - 8 Almost Everything in Java is an Object Lists of Objects The ObjectList class is similar to the IntList class, except that elements of lists are objects instead Object of integers. The objects in an object list do not have to be the same type. Point Buggle Color L Ø Point Color Buggle O - 9 O - 10 Recursive ObjectList Method Recursive ObjectList Method // Returns the number of elements in list L Example: Let’s write a method to find the n th element of an object list. public static int length(ObjectList L) { Suppose we had a list of string objects: L Ø “Boo” “happy” “halloween” } nth(2, L) O - 11 O - 12 nth(1, L)

  3. Recursive ObjectList Method And Speaking of Strings... // Returns the n th element in list L We’ve seen character values (in single quotes): public static Object nth(int n, ObjectList L) 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 - 13 O - 14 Recursive List Method Recursive List Method // Returns true if int x is in the list L , false otherwise Write a method which determines if a given integer public static boolean isIn(int x, IntList L) { is in a given list. The method should return true if the integer is in the list, and false otherwise. L 17 -8 4 Ø Examples: isIn(15, L) isIn(-8, L) O - 15 } O - 16

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend