Professional Documents
Culture Documents
Chapter 13
Chapter 13
Chapter 13
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
2
Specifications for the ADT Sorted List
Data
• A collection of objects in sorted order, same data type
• The number of objects in the collection
Operations
• Add a new entry Note:
Note:aasorted
sortedlist
listwill
willnot
not
let
letyou
youadd
addororreplace
replacean an
• Remove an entry entry
entryby
byposition
position
• Get the position 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 3
A Linked Implementation
Outline of the class
public class SortedLinkedList implements SortedListInterface
{ private Node firstNode; // reference to first node of chain
private int length; // number of entries in sorted list
public SortedLinkedList()
{ firstNode = null;
length = 0;
} // end default constructor
< Implementations of the sorted list operations go here. >
...
private class Node
{
private Object data;
private Node next;
< Constructors >
...
< Accessor and mutator methods: getData, setData, getNextNode, setNextNode
...
} // end Node
} // end SortedLinkedList 4
The Method add
5
The Method add
Recursive algorithm
if ( (currentNode = = null) or
newEntry.compareTo(currentNode.getData()) <= 0)
{
currentNode = new Node(newEntry, currentNode)
}
else
Recursively add newEntry to the chain beginning at
currentNode.getNextNode()
6
The Method add
13
An Implementation That Uses the ADT List
15
Efficiency Issues
ADT List Operation Array Linked
getEntry(givenPosition) O(1) O(n)
add(newPosition, newEntry) O(n) O(n)
remove(givenPosition) O(n) O(n)
contains(anEntry) O(n) O(n)
display() O(n) O(n)
clear(),getLength(),isEmpty(), O(1) O(1)
isFull()