Title: Linked Lists
1Linked Lists
2Types of Linked Lists
- Singly-linked list
- Doubly-linked list
- Singly, circularly-linked list
- Doubly, circularly-linked list
3Singly-linked List
data
head pointer
head node
tail
Some refer to only the last node as the tail
4Doubly-linked List
data
head pointer
5Singly, Circularly-linked List
data
head pointer
6Doubly, Circularly-linked List
data
head pointer
7Singly-linked List in OO
other private data members
data object
list object
nodeobject
May include maximum number of nodes, current
number of nodes, or other items.
8A Look at Common Operations on a Singly-linked
List
- Locating an item
- Inserting an item
- Removing an item
9Locating an Item
Other Private Data members
42
63
99
10
next
next
next
head
10Locating an Item (cont)
- Design Decision
- What do we do if the item is not in the list?
- Performance
- What is the best case performance?
- What is the worst case performance?
- What is the average performance?
- How is the performance different in a sorted
linked list (best, worst, average cases)? - Sorted or unsorted, would the performance be
better if the list were doubly-linked?
11Inserting an Item
Insert 17 at the head
Other Private Data members
42
63
99
10
next
next
next
head
12Inserting an Item (cont)
Insert 17 in sorted order
Other Private Data members
42
63
99
10
next
next
next
head
13Inserting an Item (cont)
In general, with a sorted list, there are three
cases - insert as first node - insert as
last node - insert somewhere in between
Other Private Data members
42
63
99
10
next
next
next
head
14Inserting an Item (cont)
- Design Decision
- What do we do if duplicates are not allowed?
- Performance
- If the list is not maintained in sorted order, it
is most efficient to insert at the head. - What is the asymptotic performance of this kind
of insert? - If we choose to insert at the end of the list,
what is the asymptotic performance?
15Inserting an Item (cont)
- If the list is maintained in sorted order, we
must search to find the proper place in the list,
then insert. - What is the asymptotic performance of this
insert? - Would performance be better if the list were
doubly-linked?
16Removing an Item
Other Private Data members
42
63
99
10
17
next
next
next
head
next
Three cases - remove first item - remove
last item - remove an item in between
17Removing an Item (cont)
How does the algorithm change if we use a
doubly-linked list?
Other Private Data members
42
63
99
10
17
next
next
next
head
next
prev
prev
prev
prev
18Removing an Item (cont)
- Design decisions
- What do we do if the item we are asked to delete
is not in the list? - What do we do if duplicates are allowed in the
list? - Performance
- Would performance be better if the list were
doubly-linked?