Download presentation
Presentation is loading. Please wait.
Published byLiani Dharmawijaya Modified over 6 years ago
1
Chapter 12 Sorted Lists and their Implementations
CS Data Structures Mehmet H Gunes Modified from authors’ slides
2
Specifying the ADT Sorted List
ADT Sorted list is a container of items Determines and maintains order of its entries by their values. For simplicity, we will allow sorted list to contain duplicate items © 2017 Pearson Education, Hoboken, NJ. All rights reserved
3
Specifying the ADT Sorted List
UML diagram for the ADT sorted list © 2017 Pearson Education, Hoboken, NJ. All rights reserved
4
Interface Template for the ADT Sorted List
A C++ interface for sorted lists © 2017 Pearson Education, Hoboken, NJ. All rights reserved
5
Interface Template for the ADT Sorted List
A C++ interface for sorted lists © 2017 Pearson Education, Hoboken, NJ. All rights reserved
6
Interface Template for the ADT Sorted List
A C++ interface for sorted lists © 2017 Pearson Education, Hoboken, NJ. All rights reserved
7
Interface Template for the ADT Sorted List
A C++ interface for sorted lists © 2017 Pearson Education, Hoboken, NJ. All rights reserved
8
Using the Sorted List Operations
ADT sorted list can Add, remove, locate an entry Given the entry as an argument Operations same as ADT list operations getEntry (by position) remove (by position) clear getLength isEmpty Note: not possible to add or replace entry by position © 2017 Pearson Education, Hoboken, NJ. All rights reserved
9
Link-Based Implementation
Option for different ways to implement Array Chain of linked nodes Instance of a vector Instance of ADT list We first consider a chain of linked nodes © 2017 Pearson Education, Hoboken, NJ. All rights reserved
10
The Header File The header file for the class LinkedSortedList
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
11
The Header File The header file for the class LinkedSortedList
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
12
The Implementation File
Copy constructor calls private method copyChain © 2017 Pearson Education, Hoboken, NJ. All rights reserved
13
The Implementation File
Private method copyChain © 2017 Pearson Education, Hoboken, NJ. All rights reserved
14
The Implementation File
Places to insert strings into a sorted chain of linked nodes © 2017 Pearson Education, Hoboken, NJ. All rights reserved
15
The Implementation File
Private method getNodeBefore © 2017 Pearson Education, Hoboken, NJ. All rights reserved
16
Efficiency of the Link-Based Implementation
Depends on efficiency of method getNodeBefore Locates insertion point by traversing chain of nodes Traversal is O(n) © 2017 Pearson Education, Hoboken, NJ. All rights reserved
17
Implementations That Use the ADT List
Avoid duplication of effort Reuse portions of list’s implementation Use one of three techniques Containment Public inheritance Private inheritance © 2017 Pearson Education, Hoboken, NJ. All rights reserved
18
Containment An instance of a sorted list that contains a list of its entries © 2017 Pearson Education, Hoboken, NJ. All rights reserved
19
Containment SortedListHasA is composed of an instance of the class LinkedList © 2017 Pearson Education, Hoboken, NJ. All rights reserved
20
Containment The header file for the class SortedListHasA
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
21
Containment The header file for the class SortedListHasA
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
22
Containment Methods © 2017 Pearson Education, Hoboken, NJ. All rights reserved
23
Containment Methods © 2017 Pearson Education, Hoboken, NJ. All rights reserved
24
Containment Methods © 2017 Pearson Education, Hoboken, NJ. All rights reserved
25
Containment Methods © 2017 Pearson Education, Hoboken, NJ. All rights reserved
26
Invoke corresponding list method
Containment Method removeSorted calls getPosition Method returns false if not found Other methods isEmpty getLength remove clear getEntry Invoke corresponding list method © 2017 Pearson Education, Hoboken, NJ. All rights reserved
27
Containment The worst-case efficiencies of the ADT sorted list operations when implemented using an instance of the ADT list © 2017 Pearson Education, Hoboken, NJ. All rights reserved
28
Public Inheritance Most operations for ADT list are almost the same as … Corresponding operations for ADT sorted list We use an is-a relationship © 2017 Pearson Education, Hoboken, NJ. All rights reserved
29
Public Inheritance SortedListIsA as a descendant of LinkedList
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
30
Public Inheritance A header file for the class SortedListIsA
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
31
Public Inheritance A header file for the class SortedListIsA
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
32
Public Inheritance Methods
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
33
Public Inheritance Methods
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
34
Public Inheritance Method insertSorted
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
35
Public Inheritance Method removeSorted
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
36
Public Inheritance Method getPosition
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
37
Public Inheritance Method insert overridden to always return false Prevents insertions into a sorted list by position © 2017 Pearson Education, Hoboken, NJ. All rights reserved
38
Public Inheritance Possible that an is-a relationship does not exist
In that case do not use public inheritance Private inheritance enables use of methods of a base class Without giving client access to them © 2017 Pearson Education, Hoboken, NJ. All rights reserved
39
Public Inheritance The header file for the class SortedListAsA
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
40
Public Inheritance Implementation can use Example Public methods
Protected methods Example © 2017 Pearson Education, Hoboken, NJ. All rights reserved
41
Public Inheritance The SortedListAsA class implemented in terms of the LinkedList class © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.