Title: Linked List
1Chapter 17
2Objective
- Linked lists
- basic ideas header nodes and iterator classes
- Implementation details
- doubly linked lists
- circular linked lists
3arrayname
- Arrays
- contiguous
- direct access of elements
- insertion / deletion difficult
- Linked Lists
- noncontiguous
- must scan for element
- insertion /deletion easy
4Iterating through a data structure
a
for (int i 0 i lt length i) coutltlt
ai for (ListNode p theList.first p !
null p p.next) coutltlt p.data
5Adding an element
A0
A1
A2
first
last
class ListNode Object data ListNode
next At any point, we can add a new last item
x by doing this Last-gtnext new
ListNode() Last Last-gtnext Last-gtdata
x Last-gtnext null
6A0
A1
A2
first
last
class ListNode Object data ListNode
next At any point, we can add a new last item
x by doing this Last-gtnext new
ListNode() Last Last-gtnext Last-gtdata
x Last-gtnext null
7A0
A1
A2
first
last
class ListNode Object data ListNode
next At any point, we can add a new last item
x by doing this Last-gtnext new
ListNode() last last-gtnext Last-gtdata
x Last-gtnext null
8A0
A1
A2
x
first
last
class ListNode Object data ListNode
next At any point, we can add a new last item
x by doing this last-gtnext new
ListNode() last last-gtnext Last-gtdata
x Last-gtnext null
9A0
A1
A2
x
first
last
class ListNode Object data ListNode
next At any point, we can add a new last item
x by doing this Last-gtnext new
ListNode() last last-gtnext Last-gtdata
x Last-gtnext null
10Inserting an element
A0
A1
A2
current
first
last
class ListNode Object element ListNode
next At any point, we can add a new last item
x by doing this tmp new ListNode() Tmp-gtele
ment x Tmp-gtnext current-gtnext Current-gtnext
tmp
11A0
A1
A2
current
first
last
class ListNode Object element ListNode
next At any point, we can add a new last item
x by doing this tmp new ListNode() Tmp-gtele
ment x Tmp-gtnext current-gtnext Current-gtnext
tmp
tmp
12A0
A1
A2
current
x
first
last
class ListNode Object element ListNode
next At any point, we can add a new last item
x by doing this tmp new ListNode() Tmp-gtele
ment x Tmp-gtnext current-gtnext Current-gtnext
tmp
tmp
13A0
A1
A2
current
x
first
last
class ListNode Object element ListNode
next At any point, we can add a new last item
x by doing this tmp new ListNode() Tmp-gtele
ment x Tmp-gtnext current.next current-gtnext
tmp
tmp
14A0
A1
A2
current
x
first
last
class ListNode Object element ListNode
next At any point, we can add a new last item
x by doing this tmp new ListNode() Tmp-gtele
ment x Tmp-gtnext current-gtnext Current-gtnext
tmp
tmp
15Simplified version
- Current-gtnext new ListNode(x, current-gtnext)
16Deleting an element
A0
A1
A2
current
last
class ListNode Object element ListNode
next Current-gtnext current-gtnext-gtnext
17Deleting an element
A0
A1
A2
current
last
class ListNode Object element ListNode
next Current-gtnext current-gtnext-gtnext
Memory leak!
18Delete a Node
- Node deletedNode current-gtnext
- Current-gtnext current-gtnext-gtnext
- Delete deletedNode
19Header Nodes
a
b
c
header
Header nodes allow us to avoid special cases in
the code such as insertion of the first element
and removal of the last element. The header node
holds no data but serves to satisfy the
requirement that every node have a previous
node. Not necessarily a standard implementation.
20C implementation
- We have a list.
- This list consists of listNodes.
- In order to access these listNodes, we need an
iterator. - Code online
21Doubly Linked Lists
a
b
c
head
tail
Consider how hard it is to back up in a singly
linked list. Also consider text editor example
class DoubleListNode Object element
ListNode next ListNode prev
22Empty Doubly Linked List
c
head
tail
// constructor DoubleList() head new
DoubleListNode () tail new DoubleListNode
() head-gtnext tail tail-gtprev
head
23Inserting into a Doubly Linked List
a
c
head
tail
current
newNode new DoublyLinkedListNode() newNode-gtprev
current newNode-gtnext current-gtnext newNode
-gtprev-gtnext newNode newNode-gtnext-gtprev
newNode current newNode
24Inserting into a Doubly Linked List
a
c
b
head
tail
current
newNode new DoublyLinkedListNode() newNode-gtprev
current newNode-gtnext current-gtnext newNode
-gtprev-gtnext newNode newNode-gtnext-gtprev
newNode current newNode
25Inserting into a Doubly Linked List
a
c
b
head
tail
current
newNode new DoublyLinkedListNode() newNode-gtprev
current newNode-gtnext current-gtnext newNode
-gtprev-gtnext newNode newNode-gtnext-gtprev
newNode current newNode
26Inserting into a Doubly Linked List
a
c
b
head
tail
current
newNode new DoublyLinkedListNode() newNode-gtprev
current newNode-gtnext current-gtnext newNode
-gtprev-gtnext newNode newNode-gtnext-gtprev
newNode current newNode
27Inserting into a Doubly Linked List
a
c
b
head
tail
current
newNode new DoublyLinkedListNode() newNode-gtprev
current newNode-gtnext current-gtnext newNode
-gtprev-gtnext newNode newNode-gtnext-gtprev
newNode current newNode
28Inserting into a Doubly Linked List
a
c
b
head
tail
current
newNode new DoublyLinkedListNode() newNode-gtprev
current newNode-gtnext current-gtnext newNode
-gtprev-gtnext newNode newNode-gtnext-gtprev
newNode current newNode
29Inserting into a Doubly Linked List
a
c
b
head
tail
current
newNode new DoublyLinkedListNode() newNode-gtprev
current newNode-gtnext current-gtnext newNode
-gtprev-gtnext newNode newNode-gtnext-gtprev
newNode current newNode
30Deleting an element from a double linked list
a
c
b
head
current
- oldNodecurrent
- oldNode-gtprev-gtnext oldNode-gtnext
- oldNode-gtnext-gtprev oldNode-gtprev
- current oldNode-gtprev
- delete oldNode
31Deleting an element from a double linked list
a
c
b
head
current
oldNode
oldNodecurrent oldNode-gtprev-gtnext
oldNode-gtnext oldNode-gtnext-gtprev
oldNode-gtprev current oldNode-gtprev delete
oldNode
32Deleting an element from a double linked list
a
c
b
head
current
oldNode
oldNodecurrent oldNode-gtprev-gtnext
oldNode-gtnext oldNode-gtnext-gtprev
oldNode-gtprev current oldNode-gtprev delete
oldNode
33Deleting an element from a double linked list
a
c
b
head
current
oldNode
oldNodecurrent oldNode-gtprev-gtnext
oldNode-gtnext oldNode-gtnext-gtprev
oldNode-gtprev current oldNode-gtprev delete
oldNode
34Deleting an element from a double linked list
a
c
b
head
current
oldNode
oldNodecurrent oldNode-gtprev-gtnext
oldNode-gtnext oldNode-gtnext-gtprev
oldNode-gtprev current oldNode-gtprev delete
oldNode
35Deleting an element from a double linked list
a
c
head
current
oldNodecurrent oldNode-gtprev-gtnext
oldNode-gtnext oldNode-gtnext-gtprev
oldNode-gtprev current oldNode-gtprev delete
oldNode
36Circular Linked lists
a
b
c
d
first
37Sorted Linked List
A sorted link list is one in which items are in
sorted order. It can be derived from a list
class. code
38Common errors (Page 599 )
- Splicing in nodes incorrectly when performing
insertion - Forgetting incomplete class declaration
- Calling delete at wrong time during remove()
- More errors on page 599 are given
39In class exercises
- Question 17.7 from the book.
- Suppose that you have a pointer to a node in
singly linked list that guaranteed not to be the
last node in the list. You do not have pointers
to any other nodes (except by following links).
Describe an O(1) algorithm that logically removes
the value stored in such a node from the linked
list, maintaining the integrity of the linked
list (Hint Involve the next node)
40In class exercises
- Question 17.3 from the book.
- Write an algorithm for printing a singly linked
list in reverse. (Dont use recursion).