180 likes | 584 Views
Sorted Lists. Chapter 13. Slides by Steve Armstrong LeTourneau University Longview, TX ã 2007, Prentice Hall. Chapter Contents. Specifications for the ADT Sorted List Using the ADT Sorted List A Linked Implementation The Method add The Efficiency of the Linked Implementation
E N D
Sorted Lists Chapter 13 Slides by Steve ArmstrongLeTourneau UniversityLongview, TX ã 2007,Prentice Hall
Chapter Contents • Specifications for the ADT Sorted List • Using the ADT Sorted List • A Linked Implementation • The Method add • The Efficiency of the Linked Implementation • An Implementation that Uses the ADT List • Efficiency Issues Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Specifications for the ADT Sorted List 2 • Data • A collection of objects in sorted order, same data type • The number of objects in the collection • Operations • Add a new entry * • Remove an entry * • Getposition of the entry * • Check if a certain value is contained in the list • Clear the list • Return the length of the list • Check if list is empty or full • Display the list Note: a sorted list will not let you add or replace an entry by position Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Specifications for the ADT Sorted List • View SortedListInterface • Note methods • add • remove • getPosition • Other methods from Segment 4.10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
A Linked Implementation 7 • View linked implementation of ADT sorted list • Note • addmethod • If list is in ascending order, insert new entry just before first entry not smaller than new entry • Private getNodeBefore • Need two nodes – node before and node after where new node goes Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
The Method add Fig. 13-1 Insertion points of names into a sorted chain of linked nodes. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
The Method add 12 Fig. 13-2 Recursively adding Luke to a sorted chain of names Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
The Method add 13 Fig. 13-3 Recursively adding a node at the beginning of the chain … continued → Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
The Method add Fig. 13-3 (ctd) Recursively adding a node at the beginning of the chain. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
The Method add 15 Fig. 13-4 Recursively adding a node between existing nodes in a chain … continued → Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
The Method add Fig. 13-4 (ctd) Recursively adding a node between existing nodes in a chain. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Efficiency of the Linked Implementation 18 Fig. 13-5 The worst-case efficiencies of the operations on the ADT sorted list for two implementations Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
An Implementation That Uses the ADT List 19 • View class SortedList • Note • Use of generic type T • Implements interface SortedListInterface • Assume class LList from chapter 6 & 7 is implementation of interface ListInterface Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
An Implementation That Uses the ADT List Fig. 13-6 An instance of a sorted list that contains a list of its entries. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
An Implementation That Uses the ADT List Fig. 13-7 A sorted list in which Jamie belongs after Carlos but before Sarah. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Efficiency Issues 26 Fig. 13-8 The worst-case efficiencies of selected ADT list operations for array-based and linked implementations Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Efficiency Issues Fig. 13-9 The worst-case efficiencies of the ADT sorted list operations when implemented using an instance of the ADT LIST Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Efficiency Issues • When you use an instance of an ADT list to represent entries in ADT sorted list • Must use the list's operations to access sorted lists entries • Do not access them directly • Direct access leads to inefficient implementation of sorted list • Underlying list uses a chain of linked nodes to store entries Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X