80
1 Chapter 4 Unordered List

Chapter 4

  • Upload
    lerato

  • View
    20

  • Download
    0

Embed Size (px)

DESCRIPTION

Unordered List. Chapter 4. Learning Objectives. Describe the properties of an unordered list. Study sequential search and analyze its worst-case and average running times. Discover how the entries of a list may be dynamically rearranged at achieve better search times. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 4

1

Chapter 4

Unordered List

Page 2: Chapter 4

2

Learning Objectives

● Describe the properties of an unordered list.● Study sequential search and analyze its worst-

case and average running times.● Discover how the entries of a list may be

dynamically rearranged at achieve better search times.

● Understand the public interface of an unordered list class in Java and the running times of its methods.

Page 3: Chapter 4

3

Learning Objectives

● Develop a set of classes for an expense processing application based on an unordered list.

● Understand how object-oriented programming can be used to write a single piece of code in Java that can perform equality checking based on different criteria for different input objects.

● Learn what linked lists are, why they are useful, and how to build and manipulate them.

Page 4: Chapter 4

4

Learning Objectives

● Implement a linked lest class in Java and analyze the running times of its methods.

● Implement an unordered list class in Java using a linked list component.

Page 5: Chapter 4

5

4.1 Unordered List Properties

● Keeping track of daily expenses. It would be useful to write a program that maintains

an expense list of all recorded expenses, that can be used to find quick answers to simple budgeting type questions.

Page 6: Chapter 4

6

4.1 Unordered List Properties

Page 7: Chapter 4

7

4.1 Unordered List Properties

Page 8: Chapter 4

8

4.1 Unordered List Properties

● Answer the following questions: What is the maximum (or minimum) expense, and

on what item? What is the average expense? What is the total amount spent on a given item?

● All these question may be answered by scanning such a list from the beginning and terminating when our question is answered.

Page 9: Chapter 4

9

4.1 Unordered List Properties

Page 10: Chapter 4

10

4.2 Sequential Search

● Operation contains searches for a specific itme in the list. Since the list is unordered, the only way to conduct

the search is to look at every element in the sequence.

If a match is found, the operation returns true, otherwise it returns false.

Page 11: Chapter 4

11

4.2 Sequential Search

Page 12: Chapter 4

12

4.2 Sequential Search

● Best case 1

● Worst case n

● Unsuccessful search? n

Page 13: Chapter 4

13

4.3 A List Class

● NoSuchElementException thrown back.

Page 14: Chapter 4

14

4.3 A List Class

Page 15: Chapter 4

15

4.3 A List Class

Page 16: Chapter 4

16

4.3 A List Class

● Enumeration The items of a List object may be enumerated with

a simple device called a cursor.● To start, a call is made to the first method.● This sets the cursor at the first item of the list, and returns

that item.● Every subsequent call to the next method moves the

cursor to the next item, and returns that item.● When the cursor is at the end of the list, any subsequent

call to next will return null.

Page 17: Chapter 4

17

4.3 A List Class

● Example that enumerates:

Page 18: Chapter 4

18

4.3 A List Class

● Running times An implementation should be able to access the

last item of the list in O(1) time, so that the add method may be implemented in O(1) time.

Maintain a count of the number of items in the list.● The size method can then simply return this count.

Empty the list in O(1) time. Use a cursor to enumerate a list, so that each of the

enumeration methods first and next may be implemented in O(1) time.

Page 19: Chapter 4

19

4.4 An ExpenseList Class Using List

● An ExpenseList class would support operations for maintaining expenses. Use the generic List class as a component,

implementing all the ExpenseList class methods by reusing code from one or more of the appropriate List class methods.

Every expense will consists of the amount of expense and the item on which the expense was incurred.

Page 20: Chapter 4

20

4.4.1 Expense Class Interface

Page 21: Chapter 4

21

4.4.1 Expense Class Interface

Page 22: Chapter 4

22

4.4.2 Expense Class

Page 23: Chapter 4

23

4.4.2 Expense Class

Page 24: Chapter 4

24

4.4.3 ExpenseList Class Interface

Page 25: Chapter 4

25

4.4.3 ExpenseList Class Interface

Page 26: Chapter 4

26

4.4.4 ExpenseList Class Implementation

Page 27: Chapter 4

27

4.4.4 ExpenseList Class Implementation

● minExpense, and aveExpense scan every expense entry in the list.

Page 28: Chapter 4

28

4.4.4 ExpenseList Class Implementation

● Time requirement is O(n). amountSpentOn involve sequential search.

Page 29: Chapter 4

29

4.4.4 ExpenseList Class Implementation

● Returns a matching Expense object from the expense list.

● If they are different, how come they match?

Page 30: Chapter 4

30

4.4.5 Equality of Objects and Searching

● Rewrite the method by implementing a search in the method.

Page 31: Chapter 4

31

4.4.5 Equality of Objects and Searching

● The notion of equality is defined by the equals method of the exp object. Two expenses are equal if they have the same

amount and item. What if we wanted the equality based only on the

item so if two expenses have the same item with different amount they are equal.

We would need to redefine the equality of expenses in terms of item only.

Page 32: Chapter 4

32

4.4.5 Equality of Objects and Searching

● Class Specialization for Special Equality Define special classes that extend the Expense

class, with the sole aim of implementing special, and different, kinds of expenses.

Page 33: Chapter 4

33

4.4.5 Equality of Objects and Searching

● About Keys

The get method is useful to extract an entire object from the list by matching its key part with a specified key.

Page 34: Chapter 4

34

4.4.5 Equality of Objects and Searching

● Only use the key part, (ex item )and get returns the entire matching entry (including amount), if any. What data structure should be used to store the

items in a list?● Removing items from anywhere in the list.

Leaves holes in the array. Uses more space than necessary. Search times would be greater than O(n).

● If the holes are patched up by compacting the array, we would be doing a lot of data movement within the array.

Page 35: Chapter 4

35

4.5 Linked List

Page 36: Chapter 4

36

4.5 Linked List

● To access the entries of the linked list, a reference to its first entry is all we need. One can access any entry by simply following the

chain of links. When an entry is removed from some place in a

linked list, all that needs to be done is to have its predecessor's link refer to its successor.

Similarly, an entry may be inserted anywhere in the list without having to move other entries over to create space.

Page 37: Chapter 4

37

4.5 Linked List

Page 38: Chapter 4

38

4.5 Linked List

● The biggest drawback of the linked list is its inability to perform random accesses for any entry in a single step.

Page 39: Chapter 4

39

4.5.1 Node

Page 40: Chapter 4

40

4.5.1 Node

● A node is defined in terms of itself: next field of the node class is a reference to another

Node<T> object. Self-referential structure

Page 41: Chapter 4

41

4.5.2 Insertion

● Adding to the beginning of the list.

Page 42: Chapter 4

42

4.5.2 Insertion

● Adding in between two nodes.

Page 43: Chapter 4

43

4.5.2 Insertion

● Adding to the end of the list

Page 44: Chapter 4

44

4.5.3 Deletion

● Deleting the first node, the last node, or in-between node.

Page 45: Chapter 4

45

4.5.3 Deletion

● In both insertion and deletion we assumed the existence of P, a reference to the node just prior to the one to be inserted or deleted.

Page 46: Chapter 4

46

4.5.4 Access

● Stepping through, or traversing, all the entries of a linked list from beginning to end following the chain of references is a useful operation in practice.

Page 47: Chapter 4

47

4.5.4 Access

● Deleting the first occurrence of the string “Carrot”.

Page 48: Chapter 4

48

4.5.4 Access

● We can't delete nextNode unless we have a reference to the node prior to it.

Page 49: Chapter 4

49

4.5.5 Circular Linked List

● It is useful to have instant access to both the first and the last entries of a linked list.

Page 50: Chapter 4

50

4.5.5 Circular Linked List

● Given that L refers to the last entry, the first entry is simply L.next. if L==L.next, that means there is exactly one entry

in the CLL.

Page 51: Chapter 4

51

4.5.5 Circular Linked List

● Insertion Inserting a new node as the first entry?

Will not work if the list is empty.

Page 52: Chapter 4

52

4.5.5 Circular Linked List

● Deletion Deleting the last node. Wee need to have access to the node preceding it. Assume that P is this predecessor node, and that it

has already been located.

Assumes there are at least two nodes in the list. The termination condition is that the scanning

pointer returns to the starting position.

Page 53: Chapter 4

53

4.5.5 Circular Linked List

● Assumes the list is not empty.

Page 54: Chapter 4

54

4.6 A LinkedList class

Page 55: Chapter 4

55

4.6 A LinkedList class

Page 56: Chapter 4

56

4.6 A LinkedList class

● Class methods may be classified according to functionality.

Page 57: Chapter 4

57

4.6 A LinkedList class

● Running times Basic unit time operations that count toward the

running:● Retrieving a reference, as in Node x = p.next● Updating a reference, as in p.next = x● Retrieving or updating the size (number of items) of the

linked list● Comparison between a pair of data items stored in the

linked list.

Page 58: Chapter 4

58

4.6 A LinkedList class

Page 59: Chapter 4

59

4.6 A LinkedList class

Page 60: Chapter 4

60

4.6 A LinkedList class

● Since the Node class is used as an inner class, the generic type parameter T for the LinkedList class is already known to Node, and must not be repeated in the Node class. The fields tail makes it clear that this class is

implemented as a circular linked list.

Page 61: Chapter 4

61

4.6 A LinkedList class

Page 62: Chapter 4

62

4.6 A LinkedList class

● After the insertion, the old entry is at i + 1.

Page 63: Chapter 4

63

4.6 A LinkedList class

Page 64: Chapter 4

64

4.6 A LinkedList class

● We need to be sure that the tail variable has a legal value before the method terminates.

Page 65: Chapter 4

65

4.6 A LinkedList class

Page 66: Chapter 4

66

4.6 A LinkedList class

Page 67: Chapter 4

67

4.6 A LinkedList class

Page 68: Chapter 4

68

4.6 A LinkedList class

Page 69: Chapter 4

69

4.6 A LinkedList class

Page 70: Chapter 4

70

4.6 A LinkedList class

Page 71: Chapter 4

71

4.6 A LinkedList class

● get is identical in logic to the indexOf method except that it returns the matching item if found.

Page 72: Chapter 4

72

4.7 List Class Implementation

● Review Started with the List interface, using it to build an

ExpenseList class. To implement the List class use an array or the

java.util.ArrayList class would not be a good choice. The LinkedList class, on the other hand would be

ideal. The ExpenseList class implementation uses the List

class as a component, the List class is built using the LinkedList class as a compenent.

Page 73: Chapter 4

73

4.7 List Class Implementation

Page 74: Chapter 4

74

4.7 List Class Implementation

Page 75: Chapter 4

75

4.8 Summary

● The unordered list is a linear collection of entries whose relative positions with respect to each other is irrelevant.

● The unordered list is characterized by four main operations: Append Remove Enumerate Search

Page 76: Chapter 4

76

4.8 Summary

● An entry is searched for in a unordered list using sequential search.

● In sequential search, a target is compared against every entry in the list in sequence, one after the other.

● The average number of comparisons for successful search in a data structure is the number of comparisons required.

Page 77: Chapter 4

77

4.8 Summary

● The average number of comparisons for successful search in a data structure is given by the formula:

● In the absence of specific information about probabilities, each element is assumed to e searched with the same probability as any other element, P

i=1/n.

Page 78: Chapter 4

78

4.8 Summary

● The lowest number of comparisons on the average for successful search in a list is obtained if the entries of the list are arranged in decreasing order of search probabilities.

● The notion of the equality of objects is specific to a client, and is implemented in a client class by overriding the default implementation.

● The key part of an object is that which is used as the basis of comparing against another object for equality.

Page 79: Chapter 4

79

4.8 Summary

● A linked list is a linear structure consisting of nodes.

● The linked list's main advantage over an array is that space for nodes is allocated only when needed.

● Inserting a node into or deleting a node from a linked list follows a strict sequence.

● A circular linked list is one in which the last node refers back to the first.

Page 80: Chapter 4

80

4.8 Summary

● An unordered list class may be implemented using a linked list object as component.

● Code reuse by composition is also demonstrated in the implementation of the ExpenseList class, which uses an unordered list component.