0% found this document useful (0 votes)
28 views

Chapter 14

The document discusses using inheritance to implement an efficient sorted list data structure. It proposes inheriting from an existing linked list base class (LList) to access protected methods for manipulating the list's nodes. The sorted list class (SortedList) would override the add method to first find the correct insertion point using a binary search, then leverage the base class methods to efficiently insert the new node. This achieves O(n) time complexity for the add operation, improving over a naive implementation of O(n^2). Protected access is necessary to maintain efficiency while inheriting common linked list functionality.

Uploaded by

gitu583
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Chapter 14

The document discusses using inheritance to implement an efficient sorted list data structure. It proposes inheriting from an existing linked list base class (LList) to access protected methods for manipulating the list's nodes. The sorted list class (SortedList) would override the add method to first find the correct insertion point using a binary search, then leverage the base class methods to efficiently insert the new node. This achieves O(n) time complexity for the add operation, improving over a naive implementation of O(n^2). Protected access is necessary to maintain efficiency while inheriting common linked list functionality.

Uploaded by

gitu583
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Inheritance and Lists

Chapter 14
Chapter Contents
Using Inheritance to Implement a Sorted
List
Designing a Base Class
An Efficient Implementation of a Sorted List
• The Method add

2
Using Inheritance to Implement a
Sorted List
Have a revised SortedList method which
inherits from LList
public class SortedList extends LList
implements SortedListInterface
{ public boolean add(Comparable newEntry)
{
int newPosition = Math.abs(getPosition(newEntry));
return super.add(newPosition, newEntry);
} // end add
< Implementations of remove(anEntry) and getPosition(anEntry) go here. >
...
} // end SortedList

3
Using Inheritance to Implement a
Sorted List
Problem
• It inherits two methods which, if used could destroy the
order of the entries in a sorted list
public boolean add(int newPosition, Object newEntry);
public boolean replace(int givenPosition, Object newEntry);

Possible solutions
• Declare sorted list as an instance of
SortedListInterface
• Implement the add and replace within SortedList,
but have them return false
• Implement them but have them throw an exception
4
Designing a Base Class

Fig. 14-1 A derived class of the class LList cannot


access or change anything that is private within LList 5
Protected Access

You can access a protected method


or data field of a given class by
name only …
• Within its own class definition
• Within a class derived from that class
• Within any class in the same package
as that class

6
Changes to LList
Declare firstNode and length to be protected
Provide protected methods
• setLength
• incrementLength
• decrementLength
Make getNodeAt protected
Make classNode and its constructors protected
Add protected methods
• setData
• getData
• setNextNode
• getNextNode
7
Designing a Base Class

Fig. 14-2 Access available to a class derived


8
from the class LinkedListBase
Efficient Implementation of a Sorted
List
The class LinkedListBase enables faster
manipulation of the list's underlying data
structure
We want the class to extend
LinkedListBase
public class SortedList extends LinkedListBase
implements SortedListInterface

9
The Method add
public boolean add(Comparable newEntry)
{ Node newNode = new Node(newEntry);
Node nodeBefore = getNodeBefore(newEntry);
if (isEmpty() || nodeBefore == null)
{ newNode.setNextNode(getFirstNode());
setFirstNode(newNode);
}
else
{ Node nodeAfter = nodeBefore.getNextNode();
newNode.setNextNode(nodeAfter);
nodeBefore.setNextNode(newNode);
} // end if
incrementLength();
return true;
} // end add
10
Efficiency
getPosition is O(n2)
Improved add method is O(n)
The class designer should use
inheritance and maintain efficiency
• Requires that base class provided
protected access to underlying data
structure

11

You might also like