Title: Linked List
1Linked List
2- Singly Linked List
- Doubly Linked List
- Circular Doubly Linked List
- MiniList
3Singly Linked List
4- Singly Linked List
- ? A collection of nodes.
- ? The first node is pointed to by a pointer
called front. - ? Each node except the last has pointer
pointing to a unique successor. - ? The last node has pointer value NULL.
- Example (a) Single linked list with 4 integer
value nodes.
NULL
7
5
3
10
front
(b) Empty list
nodeltTgt front NULL
front ? NULL
5- The Node Class (1)
- Node
- -- nodeValue
- -- next pointer
nodeValue
next
P
6- The Node Class (2)
- template lttypename Tgt
- class node
-
- public
- T nodeValue // data held by the node
- nodeltTgt next // next node in the list
- // default constructor with no initial
value - node() next(NULL)
-
- // constructor. initialize nodeValue and
next - node(const T item, nodeltTgt nextNode
NULL) - nodeValue(item), next(nextNode)
-
-
7- Use the Node Class (1)
- nodeltintgt front
- front new nodeltintgt(10)
NULL
10
front
Int n front ? nodeValue front ? nodeValue 5
8- Create a singly linked list
9- Begin from a empty linked list - insert one node
at the front - Step 1 allocate the new node and attach it
to the front node - nodeltTgt newNode new
nodeltTgt (item, front) - Step 2 update front pointer to point at the
new node - front newNode
10- Building a Linked List by Repeatedly Inserting at
the Front of List - nodeltintgt front NULL, newNode
- for (int i 1 i lt5 I)
- newNode new nodeltintgt (i,
front) - front newNode
-
-
11- Insert a new node before an intermediate node
curr in the list -
null
curr
front
prev
Step 1 The pointer field for newNode must link
to curr, newNode ? next
curr Step 2 The pointer field for prev must
link to newNode prev ? next
newNode
12- Traverse a singly linked list
13- Tranverse a Single Linked List
-
NULL
7
5
3
10
front
template lttypename Tgt void writeLinkedList(nodeltTgt
front, const string separator " ") //
front points at first node. curr moves through
the list nodeltTgt curr curr front
// set curr to front of the list while
(curr ! NULL) // continue until and of list
// output node value and move to the next
node cout ltlt curr-gtnodeValue ltlt separator
curr curr-gtnext
14- Delete node from a singly linked list
15- Erasing at the Front of a Linked List
- if (front ! NULL)
- nodeltTgt p front
- front front ? next
- delete p
-
16- Removing a Node with Specific Value --
Illustration - initial state
- nodeltTgt curr front
- nodeltTgt prev NULL
Case 1 the match is identified at the first node.
front
NULL
target
prev
curr
front front ? next delete curr
Case 2 the match occurs at some intermediate
node.
target
curr
prev
prev ? next curr ? next delete curr
17- Removing a Node with Specific Value code.
- Template lttypename Tgt
- Void eraseValue(nodeltTgt front, const T
target) - nodeltTgt curr front, prev NULL
- bool foundItem false
- while (curr ! NULL !foundItem)
- if (curr -gt nodeValue target)
- if (prev NULL)
- front front -gt next
- else
- prev-gt next curr -gt
next - delete curr
- foundItem true
-
- else
- prev curr
18Exercise on Singly Linked List
- 1) Write a piece of code to create the following
singly linked list - 2) Given the above singly linked list, write a
piece of code to delete node with value 6.
19Doubly Linked List
20dnode object
prev
next
The node of doubly linked list has two pointer
fields, which specify the address of both the
previous node and the next node in the list.
21- Dnode Objects.
- template lttypename Tgt
- class dnode
- public
- T nodeValue // data value of the
node - dnodeltTgt prev // previous node in the
list - dnodeltTgt next // next node in
the list - dnode()
- next this // the next node is the current
node - prev this // the previous node is the
current node -
- dnode(const T value)
nodeValue(value) () - next this // the next node is the current
node - prev this // the previous node is the
current node -
22- Inserting a Node at a Position.
- The insert algorithm allocates a new node
and adds it to the list immediately before the
node pointed to by the pointer curr
dnodeltTgt prevNode curr ? prev dnodeltTgt
newNode new dnodeltTgt (item)
next
prev
curr
prevNode curr ? prev
1
3
2
4
item
next
prev
newNode
Step 1 newNode ? prev
prevNode Step 2 newNode ? next
curr Step 3 prevNode ? next
newNode Step 4 curr ? prev
newNode
23- Deleting a Node at a Position
- The delete algorithm
- dnodeltTgt prevNode curr ? prev, succNode
curr ? next
1
prev
next
2
succNode curr ? next
curr
prevNode curr ? prev
Step 1 prevNode ? next succNode Step
2 succNode ? prev prevNode Stpe 3
delete curr
24Circular Doubly Linked List
25- Circular Doubly Linked Lists
3
2
4
9
header
Sentinel node header is used to identify the
first and last node of the circular doubly linked
list. first node is pointed to by header ?
next last node is pointed to by header ?
prev.
26- Create a Circular Doubly Linked List
27- 1. Create a empty Circular Doubly Linked List
- dnodeltTgt header new denodeltTgt
-
prev next
header
Empty circular doubly linked list ? header ? next
header
or header ? prev header
28- Insert an new node into an empty List
- First create a new node
- dnodeltTgt newNode new denodeltTgt(value)
-
The inserted note will be the first node, as well
as the last node in the doubly linked list.
- As the first node, its prev pointer should
point to header, newNode-gtprev header
- As the last node, its next pointer should
point to header. newNode-gtnext header
- As the first node, it should be pointed to by
the headers next pointer header -gt next
newNode - As the last node, it should be
pointed to by the headers previous pointer
header -gt prev newNode
29- Insert more node at the front end
- First create a new node
- dnodeltTgt newNode new denodeltTgt(value)
-
The inserted note will be the first node, as well
as the last node in the doubly linked list.
- As the first node, its prev pointer should
point to header, newNode-gtprev header
- its next pointer should point to the old
first node. newNode-gtnext header -gt
next - As the first node, it should be pointed
to by the headers next pointer header -gt
next newNode - it should be pointed to by
the old first nodes previous pointer
newNode -gtnext -gt prev newNode
30- Insert more node at the back end (in class
exercise) - First create a new node?
-
The inserted note will be the first node, as well
as the last node in the doubly linked list.
- As the last node, its prev pointer should
point to who?, - As the last node, its
next pointer should point to who? - As
the last node, it should be pointed to by whos
next? - As the last node, it should be
pointed to by whos prev?
31- Now, in order to make the insertion operation
general, we want to implement a function called
insert() to insert a node containing a given
value before any given node. - template lttypename Tgt
- denodeltTgt insert(dnodeltTgt curr, const T
item) - //step0 create the new node
- dnodeltTgt newNode new dnodeltTgt(item)
- //step1 make this new nodes next pointer
point to the node pointed to by curr - newNode-gtnext curr
- //step2 make this new nodes prev pointer
point to the previous node - dnodeltTgt prev curr-gtprev
- newNode-gtprev prev
- //step3 make the previous nodes next
pointer point to the new node - prev-next newNode
- //step4 make the next nodes previous
pointer point to the new node - curr-gtprev newNode
-
- return newNode
-
32- Now, once you have this function, you can always
insert a node at the front end by calling - dnodeltTgt newNode insert(header-gtnext,
value) - And you can always insert a node at the back end
by calling - dnodeltTgt newNode insert(header,
value) - Finally, you can always insert a node before any
node (pointed to by curr) -
- dnodeltTgt newNode insert(curr,
value)
33- In-class exercise 2
- Please write code to create a circular doubly
linked list to store the following integer in
order - 1,2,3,4,5,6,7,8,9,10
34- Transverse a Circular Doubly Linked List
35- Transverse a Circular Doubly Linked List
- void writeDLinkedList(dnodeltTgt header, const
string separator " ") -
- // header points at first dnode. p moves
through the list - dnodeltTgt p header-gtnext
- while (p ! header) // continue until end
of list -
- // output dnode value and move to the next
dnode - cout ltlt p-gtnodeValue ltlt separator
- p p-gtnext
-
-
-
3
2
4
9
header
36- Erase nodes from circular doubly linked list
37- Deleting a Node at a Position
- The delete algorithm
- dnodeltTgt prevNode curr ? prev, succNode
curr ? next
1
prev
next
2
succNode curr ? next
curr
prevNode curr ? prev
Step 1 prevNode ? next succNode Step
2 succNode ? prev prevNode Stpe 3
delete curr
38- Now, in order to make the erase operation
general, we want to implement a function called
erase() to erase a node - template lttypename Tgt
- void erase(dnodeltTgt curr)
- dnodeltTgt prevNode curr-gtprev
- dnodeltTgt succNode curr-gtnext
-
- prevNode-gtnext succNode
- succNode-gtprev prevNode
- delete curr
-
39miniList
40- MiniList Private data members
- template lttypename Tgt
- class miniList
- public
-
- private
- dnodeltTgt header
- int listSize
- dnodeltTgt dinsert(dnodeltTgt curr, const T
item) - void derase(dnodeltTgt curr)
-
-
-
-
41- template lttypename Tgt
- dnodeltTgt miniListltTgtdinsert(dnodeltTgt curr,
const T item) - dnodeltTgt newNode new dnodeltTgt(item)
- newNode-gtprev curr-gtprev
- newNode-gtnext curr
- curr-gtprev-gtnext newNode
- curr-gtprev newNode
- return newNode
-
42- template lttypename Tgt
- void miniListltTgtderase(dnodeltTgt curr)
-
- // unlink the node from the list
- curr-gtprev-gtnext curr-gtnext
- curr-gtnext-gtprev curr-gtprev
- // delete the node
- delete curr
-
43- MiniList Constructors
- miniList()
- // constructor. create an empty list
-
- miniList(int n, const T item
T()) - // constructor. build a list with n elements,
all having - // the value item
-
- miniList(T first, T last)
- // constructor. build a list whose data comes
from the - // pointer range first, last)
44Constructor 1
- template lttypename Tgt
- miniListltTgtminiList() listSize(0)
-
- // create an empty list
- header new dnodeltTgt
45Constructor 2
- template lttypename Tgt
- miniListltTgtminiList(int n, const T value)
listSize(n) -
- int i
- // create an empty list
- header new dnodeltTgt
- // insert n copies of value at the front of the
list - for (i0i lt ni)
- dinsert(header-gtnext, value)
-
46Constructor 3
- template lttypename Tgt
- miniListltTgtminiList(T first, T last)
listSize(0) -
- T curr first
- // create an empty list
- header new dnodeltTgt
- // insert the values in the range first, last)
at the - // back of the list. increment listSize in each
iteration - while (curr ! last)
-
- dinsert(header, curr)
- curr
- listSize
-
-
47- MiniList Copy Constructor
- template lttypename Tgt
- miniListltTgtminiList(const miniListltTgt obj)
listSize(obj.listSize) -
- header new dnodeltTgt
- dnodeltTgt curr obj.header-gtnext, end
obj.header -
- while (curr ! end)
-
- dinsert(header, curr-gtnodeValue)
- curr curr-gtnext
-
48- MiniList push_front()
- template lttypename Tgt
- void miniListltTgtpush_front(const T item)
-
- // insert at the front
- dinsert(header-gtnext, item)
- // increment the list size
- listSize
49- MiniList pop_back()
- template lttypename Tgt
- void miniListltTgtpop_back()
-
- // erase the back
- derase(header-gtprev)
- // decrement the list size
- listSize--
-
- template lttypename Tgt
- void miniListltTgtderase(dnodeltTgt curr)
-
- // unlink the node from the list
- curr-gtprev-gtnext curr-gtnext
- curr-gtnext-gtprev curr-gtprev
50- MiniList Iterators (1)
- template lttypename Tgt
- class miniList
- public
-
- include d_liter.h
51- MiniList Iterators (2)
- class iterator
-
- public
- iterator()
- bool operator
- bool operator!
- T operator ()
- iterator operator ()
- iterator operator (int)
- iterator operator-- ()
- iterator operator-- (int)
- private
- dnodeltTgt nodePtr
- iterator(dnodeltTgt p) nodePtr(p)
-
52- MiniList Iterators (3)
- T operator ()
-
- return nodePtr-gtnodeValue
-
- template lttypename Tgt
- miniListltTgtiterator miniListltTgtbegin()
-
- // private iterator constructor builds an
iterator object - // from the dnode pointer
- return iterator(header-gtnext)
53- MiniList Iterators (4)
- // prefix increment. move forward one node
- iterator operator ()
- // move to the successor of nodePtr
- nodePtr nodePtr-gtnext
- return this // return new iterator
value -
- // postfix increment. move forward one node
- iterator operator (int)
- // save the current value of the iterator
- iterator tmp this
- // move to the successor of nodePtr
- nodePtr nodePtr-gtnext
- return tmp // return original iterator
value -
54- MiniList begin()
- template lttypename TgtminiListltTgtiterator
miniListltTgtbegin() // private iterator
constructor builds an iterator object // from
the dnode pointer return iterator(header-gtnext)
- MiniList end()
- template lttypename TgtminiListltTgtiterator
miniListltTgtend() return iterator(header)