38
Chapter 8 Lists

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

Embed Size (px)

Citation preview

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

Chapter 8

Lists

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

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-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

Page 3: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-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

Page 4: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-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

Page 5: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-5

figure 8.1 A conceptual view of an ordered list

Page 6: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-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

Page 7: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-7

figure 8.2 A conceptual view of an unordered list

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

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-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

Page 9: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-9

figure 8.3 A conceptual view of an indexed list

Page 10: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-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

Page 11: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-11

figure 8.4 The common operations on a list

Page 12: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-12

figure 8.5 The operation particular to an ordered list

Page 13: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-13

figure 8.6 The operations particular to an unordered list

Page 14: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-14

figure 8.7 The operations particular to an indexed list

Page 15: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-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

• See ListADT.java (page 223)• See OrderedListADT.java (page 224)• See UnorderedListADT.java (page 224)• See IndexedListADT.java (page 225)

Page 16: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

figure 8.8 The various list interfaces

Page 17: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-17

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 agains the second worst team, etc.

Page 18: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-18

figure 8.9 Bowling league team names and number of wins

Page 19: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-19

figure 8.10 Sample tournament layout for a bowling league tournament

Page 20: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-20

Tournament Maker

• A class that represents a team will be Comparable, based on their number of wins

• See Tournament.java (page 228)• See Team.java (page 230)• See TournamentMaker.java (page 231)

Page 21: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

figure 8.11 UML description of the Tournament class

Page 22: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-22

The Josephus Problem

• In a Josephus problem, a set of elements are arranged in a circle

• Starting with a particular element, every ith 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?

• See Josephus.java (page 235)

Page 23: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-23

figure 8.12 UML description of the Josephus program

Page 24: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-24

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

Page 25: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-25

figure 8.13 An array implementation of a list

Page 26: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-26

The remove Operation//-----------------------------------------------------------------

// Removes and returns the specified element.

//-----------------------------------------------------------------

public Object remove (Object element)

{

Object result;

int index = find (element);

if (index == NOT_FOUND)

throw new NoSuchElementException("list");

result = list[index];

// shift the appropriate elements

for (int scan=index; scan < rear; scan++)

list[scan] = list[scan+1];

rear--;

list[rear] = null;

return result;

}

Page 27: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-27

The find Method

//-----------------------------------------------------------------

// Returns the array index of the specified element, or the

// constant NOT_FOUND if it is not found.

//-----------------------------------------------------------------

private int find (Object target)

{

int scan = 0, result = NOT_FOUND;

boolean found = false;

if (! isEmpty())

while (! found && scan < rear)

if (target.equals(list[scan])

found = true;

else

scan++;

if (found)

result = scan;

return result;

}

Page 28: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-28

The contains Operation

//-----------------------------------------------------------------

// Returns true if this list contains the specified element.

//-----------------------------------------------------------------

public boolean contains (Object target)

{

return (find(target) != NOT_FOUND;

}

Page 29: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-29

The add Operation (ordered list)

//-----------------------------------------------------------------

// Adds the specified Comparable element to the list, keeping

// the elements in sorted order.

//-----------------------------------------------------------------

public void add (Comparable element)

{

if (size() == list.length)

expandCapacity();

int scan = 0;

while (scan < rear && element.compareTo(list[scan]) > 0)

scan++;

for (int scan2=rear; scan2 > scan; scan2--)

list[scan2] = list[scan2-1]

list[scan] = element;

rear++;

}

Page 30: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-30

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

Page 31: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-31

The remove Operation

//-----------------------------------------------------------------

// Removes the first instance of the specified element from the

// list if it is found in the list and returns a reference to it.

// Throws an EmptyListException if the list is empty. Throws a

// NoSuchElementException if the specified element is not found

// on the list.

//-----------------------------------------------------------------

public Object remove (Object targetElement) throws

EmptyCollectionException, ElementNotFoundException

{

if (isEmpty())

throw new EmptyCollectionException ("List");

boolean found = false;

LinearNode previous = null;

LinearNode current = head;

Page 32: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-32

remove Continued

while (current != null && !found)

if (targetElement.equals (current.getElement()))

found = true;

else

{

previous = current;

current = current.getNext();

}

if (!found)

throw new ElementNotFoundException ("List");

if (size() == 1)

head = tail = null;

else

if (current.equals (head))

head = current.getNext();

else

Page 33: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-33

remove Continued

if (current.equals (tail))

{

tail = previous;

tail.setNext(null);

}

else

previous.setNext(current.getNext());

count--;

return current.getElement();

}

Page 34: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-34

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

• See DoubleNode.java (page 245)

Page 35: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-35

figure 8.14 A doubly linked list

Page 36: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-36

The remove Operation revisited

//-----------------------------------------------------------------

// Removes and returns the specified element.

//-----------------------------------------------------------------

public Object remove (Object targetElement) throws

ElementNotFoundException

{

Object result;

DoubleNode nodeptr = find (element);

if (nodeptr == null)

throw new ElementNotFoundException ("list");

result = nodeptr.getElement();

// check to see if front or rear

if (nodeprt == front)

result = this.removeFirst();

Page 37: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-37

The remove Operation revisited

else

if (nodeptr == rear)

result = this.removeLast();

else

{

nodeptr.getNext().setPrevious(nodeptr.getPrevious());

nodeptr.getPrevious().setNext(nodeptr.getNext());

count--;

}

return result;

}

Page 38: Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-38

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