Download - Lists
Lists
List Implementations
2
Linked List Review
• Recall from CMSC 201– “A linked list is a linear collection of self-
referential structures, called nodes, connected by pointer links. A linked list is accessed by keeping a pointer to the first node of the list. This pointer to the first node of a list is typically named head. Subsequent nodes are accessed via a link pointer member that is stored in each node.”
3
OO Linked List
• In the OO world, the Linked List (LL) is a class with the public interface described last time.
• The pointer to the first node is a private data member of the LL class
• Other private data members may be defined if needed (tail, number of elements, etc)
4
Class Relationships
• The List class contains a pointer to the first Node
• The Node class contains a Data object as a data member – this is known as “composition” or “aggregation”– The List and Node cannot/should not/will not
know anything about the Data.– The List and Node MUST use only the Data’s
public interface
5
LL diagram
next
Data
next
Data Data
next
Data
head
OtherPrivateData
members
A singly linked list
List Object NodeObject
Data Object
6
Inserting into a Linked List
• If the list is not sorted, why not always insert at the head? What’s the asymptotic performance of this kind of insert?
• If the list is sorted, search to find the proper place in the list, then insert. What’s the asymptotic performance of this insert?
7
next
42
next
63 99
next
10
head
OtherPrivateData
members
next
17
next
42
next
63 99
next
10
head
OtherPrivateData
members
Insert 17 at the head
8
next
42
next
63 99
next
10
head
OtherPrivateData
members
Insert 17 in sorted order
next
42
next
63 99
next
10
head
OtherPrivateData
members
next
17
9
Finding an element
• Starting with the head pointer in the List, follow the “next” links until you find the element in question or get to the end of the list
• What is the worst case asymptotic performance of find( )?
• How is the performance different in a sorted linked list?
10
Pseudo-code for find( )
int find (Data d)
{
NodePtr p = list.head;
while (p not NULL and it’s not the one you want)
go to the next element
If p not NULL, return success;
If p is NULL, return failure
}
11
removal in Linked List
• Find the node that contains the data element you want to remove. Make the node before the node that contains that element point to the node after the node that contains that element (NULL if the last element is being deleted), then free the memory for the node
12
Diagram for remove( )
next
42
next
63 99
next
10
head
OtherPrivateData
members
next
17
To remove( 17 ), make the node with 10 point to the node with 42, then free the memory for the node with 17.
13
Thinking about remove( )• A problem – we can’t use find( ) because we need
to modify the node before the one that has the element we want to delete. Besides, find() doesn’t return a pointer to a node.
• Two solutions– The code in remove( ) keeps track of two pointers – one
used to find the node to be removed and one to “trail behind” and point to the one before the one to be removed
– Write a private method called “findPrevious( )” that is called from remove( ) that finds the node before the one with the specified element and returns a pointer to it
14
remove (cont’d)
• Problem – there may not be a node after the one being removed
• Solution – not a problem. The next pointer of the node before the one being removed will become NULL without any special code
15
remove( ) (cont’d)
• Another problem -- there is no node before the node being removed. This can only occur when we are removing the first node.
• Solution– Special code to handle this case
16
remove( ) (cont’d)
• What do we do if the element we are asked to delete is not in the list?
• What do we do if duplicates are allowed in the list?
17
remove( ) Pseudo-coderemove (Data d) {
if (removing first element)get a pointer to node being removedmodify head pointer to point to 2nd node
elseget a pointer to the node being removedget a pointer to the node before the one to be removed
(special code or findPrevious() )modify next pointer of this “previous” node
free memory of node being removed}
18
Making remove() easier• The code that makes remove( ) somewhat complex
is the code for finding the node that comes before the node you want to remove. You either have an extra pointer or write a special (private) function that we’ve called findPrevious()
• An alternative is to build the necessary pointer into the linked list. This pointer (usually called “prev”) points to the node that comes before this node. Then we can write an alternative version of find( ) (let’s call it findIt() ) that returns a pointer to the node and use “prev” to access the previous node.
19
Double Linked List
next
42
next
63 99
next
10
head
OtherPrivateData
members
next
17
prev prev prev prev
Now when we want to remove( 17 ), we can call findIt( 17 ) and use the “prev” pointer as the one that points to the node before the one we want to remove. Care must be taken to perform the pointer modifications in the correct order.
20
Circular Linked List
Some applications require a more sophisticated list. In this circular list, the “next” pointer of the last node points back to the first node instead of being NULL.
This adds a little complication to find ( ).
next
42
next
63
next
99
next
10
head
OtherPrivateData
members
next
17
21
A Doubly Linked Circular List
The most general kind of list is not only circular, but doubly linked. The “next” pointer of the last node points to the first node and the “prev” pointer of the first node points to the last node.
next
42
next
63
next
99
next
10
head
OtherPrivateData
members
next
17
prev prev prev prev prev