Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.

Similar presentations


Presentation on theme: "Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques."— Presentation transcript:

1 Chapter 8 Lists

2 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques Define a list abstract data type Demonstrate how a list can be used to solve problems Examine various list implementations Compare list implementations

3 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-3 Lists Lists are linear collections, like stacks and queues, but are more flexible Adding and removing elements in lists are not restricted by the collection structure That is, they don't have to operate on one end or the other We will examine three types of list collections: – ordered lists – unordered lists – indexed lists

4 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-4 Ordered Lists The elements in an ordered list are ordered by some inherent characteristic of the elements – names in alphabetical order – scores in ascending order Therefore, the elements themselves determine where they are stored in the list

5 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-5 FIGURE 8.1 A conceptual view of an ordered list

6 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-6 Unordered Lists There is an order to the elements in an unordered list, but that order is not based on element characteristics The user of the list determines the order of the elements A new element can be put on the front or the rear of the list, or it can be inserted after a particular element already in the list

7 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-7 FIGURE 8.2 A conceptual view of an unordered list

8 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-8 Indexed Lists In an indexed list, elements are referenced by their numeric position in the list Like an unordered list, there is no inherent relationship among the elements The user can determine the order Every time the list changes, the indexes are updated

9 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-9 FIGURE 8.3 A conceptual view of an indexed list

10 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-10 List Operations There are several operations common to all three list types These include removing elements in various ways and checking the status of the list The key differences between the list types involve the way elements are added

11 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-11 FIGURE 8.4 The common operations on a list

12 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-12 FIGURE 8.5 The operation particular to an ordered list

13 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-13 FIGURE 8.6 The operations particular to an unordered list

14 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-14 FIGURE 8.7 The operations particular to an indexed list

15 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-15 List Operations As with other collections, we use Java interfaces to define the collection operations Recall that interfaces can be defined via inheritance (derived from other interfaces) We define the common list operations in one interface, then derive three others from it that define the interfaces of the three list types

16 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-16 FIGURE 8.8 The various list interfaces

17 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-17 Listing 8.1

18 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-18 Listing 8.1 (cont.)

19 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-19 Listing 8.2

20 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-20 Listing 8.3

21 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-21 Listing 8.4

22 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-22 Listing 8.4 (cont.)

23 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-23 Tournament Maker Let's use an ordered list to help manage a bowling tournament Teams are ordered by their number of wins in the regular season To create the first round match-ups, the team with the best record is matched against the team with the worst record The second best team is matched against the second worst team, etc.

24 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-24 FIGURE 8.9 Bowling league team names and number of wins

25 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-25 FIGURE 8.10 Sample tournament layout for a bowling league tournament

26 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-26 Tournament Maker A class that represents a team will be Comparable, based on their number of wins

27 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-27 Listing 8.5

28 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-28 Listing 8.6

29 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-29 Listing 8.6 (cont.)

30 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-30 Listing 8.6 (cont.)

31 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-31 Listing 8.7

32 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-32 Listing 8.7 (cont.)

33 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-33 Listing 8.7 (cont.)

34 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-34 Listing 8.7 (cont.)

35 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-35 FIGURE 8.11 UML description of the Tournament class

36 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-36 The Josephus Problem In a Josephus problem, a set of elements are arranged in a circle Starting with a particular element, every i th element is removed Processing continues until there is only one element left The question: given the starting point and remove count (i), which element is left?

37 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-37 Listing 8.8

38 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-38 Listing 8.8 (cont.)

39 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-39 Listing 8.8 (cont.)

40 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-40 FIGURE 8.12 UML description of the Josephus program

41 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-41 Implementing Lists with Arrays An array implementation of a list could follow strategies similar to those used for a queue We could fix one end of the list at index 0 and shift as needed when an element is added or removed Or we could use a circular array to avoid the shift at one end However, there is no avoiding a shift when an element in the middle is added or removed Let's examine the fixed version

42 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-42 FIGURE 8.13 An array implementation of a list

43 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-43 ArrayList - the remove Operation

44 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-44 ArrayList - the find Operation

45 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-45 ArrayList - the contains Operation

46 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-46 ArrayOrderedList - the add Operation

47 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-47 Implementing lists with links As with stack and queue, we can implement a list collection with an underlying linked list All operations can be accomplished using techniques similar to ones we've already explored However, let's examine the remove operation with this traditional approach, then alter the underlying representation for comparison

48 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-48 LinkedList - the remove Operation

49 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-49 LinkedList - the remove Operation

50 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-50 LinkedList - the remove Operation

51 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-51 Doubly Linked Lists A doubly linked list has two references in each node, one to the next element in the list and one to the previous element This makes moving back and forth in a list easier, and eliminates the need for a previous reference in particular algorithms There is, however, a bit more overhead when managing the list

52 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-52 Listing 8.9

53 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-53 Listing 8.9 (cont.)

54 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-54 Listing 8.9 (cont.)

55 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-55 Listing 8.9 (cont.)

56 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-56 FIGURE 8.14 A doubly linked list

57 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-57 Doubly Linked Lists Lets revisit the remove method for linked implementation of a list, this time using a doubly linked list Notice how much more elegant the code in this version is compared to our earlier version The cost of this elegance is the overhead associated with storing and managing additional links

58 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-58 DoubleList - the remove Operation

59 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-59 DoubleList - the remove Operation

60 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-60 Analysis of List Implementations In both array and linked implementations, many operations are similar in efficiency Most are O(1), except when shifting or searching need to occur, in which case they are order O(n) In particular situations, the frequency of the need for particular operations may guide the use of one approach over another


Download ppt "Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques."

Similar presentations


Ads by Google