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

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Topic 12 The List ADT. 9-2 Objectives Examine list processing and various ordering techniques Define a list abstract data type Examine various list implementations.
Chapter 24 Lists, Stacks, and Queues
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
1 Lists A List ADT Types of Lists Lists in Java Collections API Using ordered lists – Tournament Maker Using indexed lists – Josephus Problem Implementing.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
1 Lists A List ADT Types of Lists Lists in Java Collections API Using ordered lists – Tournament Maker Using indexed lists – Josephus Problem Implementing.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
CHAPTER 3 COLLECTIONS Abstract Data Types. 2 A data type consists of a set of values or elements, called its domain, and a set of operators acting on.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Chapter 12: Data Structures
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L16 (Chapter 22) Java Collections.
Chapter 3 Collections. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 3-2 Chapter Objectives Define the concept and terminology related.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Chapter 19 Java Data Structures
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Chapter 5 Ordered List. Overview ● Linear collection of entries  All the entries are arranged in ascending or descending order of keys.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Lists Based on content from: Java Foundations, 3rd Edition.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Chapter 18 Java Collections Framework
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 12: Multi-way Search Trees Java Software Structures: Designing.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Page 1 – Spring 2010Steffen Vissing Andersen Software Development with UML and Java 2 SDJ I2, Spring 2010 Agenda – Week 8 Linked List (a reference based.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
List Interface and Linked List Mrs. Furman March 25, 2010.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Collections Using Generics in Collections. 2 Chapter Objectives Define the concept and terminology related to collections Explore the basic structure.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
The List ADT.
Chapter 4 Linked Structures.
Chapter 9 Polymorphism.
Lists A List ADT Types of Lists Using ordered lists – Tournament Maker
Top Ten Words that Almost Rhyme with “Peas”
structures and their relationships." - Linus Torvalds
Java Software Structures: John Lewis & Joseph Chase
Copyright ©2012 by Pearson Education, Inc. All rights reserved
structures and their relationships." - Linus Torvalds
Arrays.
Presentation transcript:

Chapter 8 Lists

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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