Professional Documents
Culture Documents
06 - DS and Algorithm - Session - 08.pps
06 - DS and Algorithm - Session - 08.pps
06 - DS and Algorithm - Session - 08.pps
Objectives
In this chapter, you will learn to:
Implement a doubly-linked list
Implement a circular linked list
Apply linked lists to solve programming problems
Ver. 1.0
Session 8
Ver. 1.0
Session 8
1.
2.
START
10
15
19
21
32
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
15
ptr1
ptr2
19
ptr3
21
32
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
15
ptr1
ptr2
19
ptr3
21
32
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
15
19
ptr1
ptr1
ptr2
ptr3
21
32
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
ptr1
15
19
ptr2
ptr2
ptr3
21
32
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
ptr1
15
ptr2
19
ptr3
ptr3
21
32
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
ptr1
15
ptr2
19
ptr3
21
32
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
ptr1
15
ptr2
19
ptr3
21
32
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
ptr1
15
ptr2
19
ptr3
21
32
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
ptr1
15
ptr2
ptr1
19
ptr3
21
32
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
15
ptr2
ptr1
19
ptr3
ptr2
21
32
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
15
19
ptr3
ptr1
ptr2
21
ptr3
32
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
15
19
21
ptr3
ptr1
ptr2
32
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
15
19
ptr1
ptr1
ptr2
21
ptr3
32
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
15
19
ptr1
ptr2
21
ptr3
ptr2
32
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
15
19
ptr1
21
ptr3
ptr2
32
ptr3
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
15
19
21
ptr1
32
ptr3
ptr2
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
15
19
ptr1
21
ptr1
ptr2
32
ptr3
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
15
19
21
ptr1
ptr2
32
ptr3
ptr2
3.
4.
5.
6.
7.
8.
9.
Ver. 1.0
Exit.
2.
START
10
15
19
21
ptr1
32
ptr3
ptr2
3.
4.
5.
6.
7.
8.
ptr3 = NULL
a.
b.
c.
d.
9.
Ver. 1.0
Exit.
2.
START
10
15
19
21
32
ptr1
ptr2
3.
4.
5.
6.
7.
8.
ptr3 = NULL
a.
b.
c.
d.
9.
Ver. 1.0
Exit.
START
1.
2.
START
10
15
19
21
32
ptr1
ptr2
3.
4.
5.
6.
7.
8.
ptr3 = NULL
a.
b.
c.
d.
9.
Ver. 1.0
Exit.
Ver. 1.0
15
19
21
23
25
32
Session 8
START
10
15
19
21
23
25
32
Ver. 1.0
Session 8
//CodeinC#
classNode
{
publicintinfo;
publicNodenext;
publicNodeprev;
}
Ver. 1.0
Session 8
//CodeinC++
classNode
{
public:
intinfo;
Node*next;
Node*prev;
};
Ver. 1.0
Session 8
Ver. 1.0
Session 8
Ver. 1.0
Session 8
Answer:
Unlike singly-linked list, in which each node stores the address
of only the next node in the list, each node in a doubly-linked
list holds the address of its previous node also.
Ver. 1.0
Session 8
Write an algorithm
to traverse
Algorithm
to traverse
a doubly a doubly linked list in the
2. Repeat steps 3 and 4 until
forwardlist
direction.
linked
in the forward direction.
currentNode becomes NULL.
Ver. 1.0
3.
4.
Session 8
3.
4.
Write an algorithm
to traverse
Algorithm
to traverse
a doubly a doubly linked list in the
backward
direction.
linked
list in
the backward
2. Repeat steps 3 and 4 until
currentNode becomes NULL.
direction.
Ver. 1.0
Session 8
Ver. 1.0
Session 8
Ver. 1.0
Session 8
START
10
Ver. 1.0
15
17
1.
2.
3.
4.
5.
6.
20
Session 8
newnode
START
10
10
Ver. 1.0
15
17
1.
2.
3.
4.
5.
6.
20
Session 8
newnode
7
START
10
10
Ver. 1.0
15
17
1.
2.
3.
4.
5.
6.
20
Session 8
2.
3.
4.
5.
6.
newnode.next = START
newnode
7
START
10
10
Ver. 1.0
15
17
20
Session 8
2.
3.
4.
5.
6.
newnode.next = START
START.prev = newnode
newnode
7
START
10
10
Ver. 1.0
15
17
20
Session 8
2.
3.
4.
5.
6.
newnode.next = START
START.prev = newnode
newnode.prev = NULL
newnode
7
START
10
10
Ver. 1.0
15
17
20
Session 8
2.
3.
4.
5.
6.
newnode.next = START
START.prev = newnode
newnode.prev = NULL
START = newnode
newnode
7
START
10
10
15
17
20
Insertion complete
Ver. 1.0
Session 8
Ver. 1.0
Session 8
Insert
Algorithm
16
to insert a node between
two nodes in a doubly-linked list.
START
10
15
17
20
2.
3.
4.
5.
6.
7.
Ver. 1.0
Session 8
Insert 16
START
10
10
15
17
20
2.
3.
4.
5.
6.
7.
Ver. 1.0
Session 8
Insert 16
START
10
10
current
15
17
20
2.
3.
4.
5.
6.
7.
Ver. 1.0
Session 8
Insert 16
START
10
10
15
current
previous = NULL
17
20
2.
3.
4.
5.
6.
7.
Ver. 1.0
Session 8
Insert 16
START
10
10
15
current
previous = NULL
17
20
2.
3.
4.
5.
6.
7.
Ver. 1.0
Session 8
Insert 16
START
10
10
15
previous current
previous = NULL
17
20
2.
3.
4.
5.
6.
7.
Ver. 1.0
Session 8
Insert 16
START
10
10
previous current
15
current
17
20
2.
3.
4.
5.
6.
7.
Ver. 1.0
Session 8
Insert 16
START
10
10
previous
15
current
17
20
2.
3.
4.
5.
6.
7.
Ver. 1.0
Session 8
Insert 16
START
10
10
15
17
20
2.
3.
4.
5.
6.
7.
Ver. 1.0
Session 8
Insert 16
START
10
10
15
17
previous currentcurrent
20
2.
3.
4.
5.
6.
7.
Ver. 1.0
Session 8
Insert 16
START
10
10
15
previous
17
current
20
2.
3.
4.
5.
6.
7.
Ver. 1.0
Session 8
newnode
Insert 16
START
10
10
15
previous
17
current
20
2.
3.
4.
5.
6.
7.
Ver. 1.0
Session 8
newnode
Insert 16
16
START
10
10
15
previous
17
current
20
2.
3.
4.
5.
6.
7.
Ver. 1.0
Session 8
newnode
16
START
10
10
15
previous
17
current
newnode.next = current
20
2.
3.
4.
5.
6.
7.
Ver. 1.0
Session 8
newnode
16
START
10
10
15
previous
17
current
newnode.next = current
newnode.prev = previous
20
2.
3.
4.
5.
6.
7.
Ver. 1.0
Session 8
newnode
16
START
10
10
15
previous
17
current
newnode.next = current
newnode.prev = previous
current.prev = newnode
Ver. 1.0
20
2.
3.
4.
5.
6.
7.
Session 8
Insertion complete
newnode
16
START
10
10
15
previous
17
current
newnode.next = current
newnode.prev = previous
current.prev = newnode
previous.next = newnode
Ver. 1.0
20
2.
3.
4.
5.
6.
7.
Session 8
Ver. 1.0
2.
3.
4.
5.
6.
7.
Session 8
Insert
Refer
23 to the modified algorithm
START
2.
3.
4.
10
15
17
20
5.
6.
7.
Ver. 1.0
Session 8
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
7.
Ver. 1.0
Session 8
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
current
7.
Ver. 1.0
Session 8
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
current
previous = NULL
Ver. 1.0
7.
Session 8
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
current
previous = NULL
Ver. 1.0
7.
Session 8
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous current
previous = NULL
Ver. 1.0
7.
Session 8
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous current
current
7.
Ver. 1.0
Session 8
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
current
7.
Ver. 1.0
Session 8
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
Ver. 1.0
Session 8
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous current
current
7.
Ver. 1.0
Session 8
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
current
7.
Ver. 1.0
Session 8
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
previous current
7.
Ver. 1.0
Session 8
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous current
current
7.
Ver. 1.0
Session 8
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
current
7.
Ver. 1.0
Session 8
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
Ver. 1.0
Session 8
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous current
7.
current = NULL
Ver. 1.0
Session 8
newnode
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
7.
current = NULL
Ver. 1.0
Session 8
newnode
23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
7.
current = NULL
Ver. 1.0
Session 8
newnode.next = current
newnode
23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
7.
current = NULL
Ver. 1.0
Session 8
newnode.next = current
newnode.prev = previous
newnode
23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
7.
current = NULL
Ver. 1.0
Session 8
newnode.next = current
newnode.prev = previous
newnode
23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
7.
current = NULL
Ver. 1.0
Session 8
newnode.next = current
newnode.prev = previous
previous.next = newnode
newnode
23
START
2.
3.
4.
10
10
15
17
20
5.
6.
Insertion complete
previous
7.
current = NULL
Ver. 1.0
Session 8
2.
4.
5.
6.
Write an algorithm
tonode
insertatathe
node at the end of a
Algorithm
to insert a
doubly-linked
list that contains
a variable,
to ofkeep
end
of a doubly-linked
list
3.
MakeLAST,
the next field
the node marked
as
LAST
point
to
the
new
node.
track of the last node of the list.
Ver. 1.0
Session 8
Ver. 1.0
Session 8
Ver. 1.0
Session 8
1.
2.
3.
START
10
Ver. 1.0
a.
15
17
20
4.
Session 8
2.
3.
START
10
10
a.
15
17
20
4.
current
Ver. 1.0
Session 8
START
START
10
10
15
1.
2.
3.
17
20
4.
current
START = START.next
Ver. 1.0
Session 8
2.
3.
START
10
10
15
a.
17
20
4.
current
START = START.next
START. prev = NULL
Ver. 1.0
Session 8
1.
2.
3.
START
10
15
a.
17
20
4.
current
Memory released
START = START.next
START. prev = NULL
Ver. 1.0
Session 8
Ver. 1.0
Session 8
Algorithm
Delete 17 to delete a node between
two nodes in a doubly-linked list.
START
10
Ver. 1.0
15
17
20
b.
c.
d.
e.
2.
3.
4.
Delete 17
START
10
10
Ver. 1.0
15
17
20
b.
c.
d.
e.
2.
3.
4.
START
10
15
17
20
previous = NULL
Ver. 1.0
b.
c.
d.
e.
2.
3.
4.
START
10
10
15
17
20
current
previous = NULL
Ver. 1.0
b.
c.
d.
e.
2.
3.
4.
START
10
10
15
17
20
current
previous = NULL
Ver. 1.0
b.
c.
d.
e.
2.
3.
4.
START
10
10
15
17
20
current
previous
previous = NULL
Ver. 1.0
b.
c.
d.
e.
2.
3.
4.
START
10
10
current
previous
Ver. 1.0
15
17
20
current
b.
c.
d.
e.
2.
3.
4.
START
10
10
previous
Ver. 1.0
15
17
20
current
previous
b.
c.
d.
e.
2.
3.
4.
START
10
10
15
17
20
previous
current current
previous
Ver. 1.0
b.
c.
d.
e.
2.
3.
4.
START
10
10
15
17
20
previous current
previous. next = current. next
Ver. 1.0
b.
c.
d.
e.
2.
3.
4.
START
10
10
15
17
20
previous current
previous. next = current. next
current.next.prev = previous
Ver. 1.0
b.
c.
d.
e.
2.
3.
4.
Deletion complete
START
10
10
15
17
20
previous current
previous. next = current. next
current.next.prev = previous
Ver. 1.0
b.
c.
d.
e.
2.
3.
4.
Ver. 1.0
b.
c.
d.
e.
2.
3.
4.
2.
3.
Refer
Deleteto20the modified algorithm
START
10
Ver. 1.0
a.
15
17
20
4.
Session 8
2.
3.
Delete 20
START
10
10
a.
15
17
previous
Ver. 1.0
20
4.
current
Session 8
START
10
10
2.
3.
15
17
previous
Ver. 1.0
1.
20
4.
current
Session 8
START
10
10
2.
3.
15
17
previous
Ver. 1.0
1.
20
4.
current
Session 8
START
10
10
1.
2.
3.
15
17
previous
20
4.
current
Ver. 1.0
Session 8
Ver. 1.0
Session 8
Ver. 1.0
15
11
23
12
Session 8
15
11
23
12
Ver. 1.0
Session 8
Ver. 1.0
Session 8
Ver. 1.0
Session 8
Ver. 1.0
Session 8
Write an algorithm
to traverse
circular linked list.
Algorithm
to traverse
a circularalinked
list.
Ver. 1.0
2.
3.
4.
5.
Session 8
Ver. 1.0
Session 8
Ver. 1.0
Session 8
1.
2.
3.
4.
LAST
10
10
Ver. 1.0
15
17
20
Session 8
newnode
1.
2.
3.
4.
LAST
10
10
Ver. 1.0
15
17
20
Session 8
newnode
7
2.
3.
4.
LAST
10
10
Ver. 1.0
1.
15
17
20
Session 8
newnode
7
1.
2.
3.
4.
LAST
10
10
15
17
20
Ver. 1.0
Session 8
newnode
Insertion complete
1.
2.
3.
4.
LAST
10
10
15
17
20
Ver. 1.0
Session 8
Ver. 1.0
Session 8
1.
2.
3.
4.
5.
LAST
10
Ver. 1.0
15
17
20
Session 8
2.
3.
4.
5.
LAST
10
10
15
17
20
current
Ver. 1.0
Session 8
2.
3.
4.
5.
LAST
10
10
15
17
20
current
previous = NULL
Ver. 1.0
Session 8
2.
3.
4.
5.
LAST
10
10
15
17
20
current
previous = NULL
Ver. 1.0
Session 8
2.
3.
4.
5.
LAST
10
10
15
17
20
previous current
previous = NULL
Ver. 1.0
Session 8
2.
3.
4.
5.
LAST
10
10
previous current
Ver. 1.0
15
17
20
current
Session 8
2.
3.
4.
5.
LAST
10
10
previous
Ver. 1.0
15
17
20
previous current
Session 8
2.
3.
4.
5.
Nodes located
LAST
10
10
15
17
20
previous currentcurrent
Ver. 1.0
Session 8
Ver. 1.0
Session 8
1.
2.
3.
4.
5.
LAST
10
Ver. 1.0
15
17
20
Session 8
1.
2.
3.
4.
5.
LAST
10
10
Ver. 1.0
15
17
20
Session 8
newnode
1.
2.
3.
4.
5.
LAST
24
10
10
Ver. 1.0
15
17
20
Session 8
newnode
1.
2.
3.
4.
5.
LAST
24
10
10
15
17
20
Ver. 1.0
Session 8
newnode
1.
2.
3.
4.
5.
LAST
24
10
10
15
17
20
Ver. 1.0
Session 8
1.
2.
3.
4.
5.
LAST
24
10
10
15
17
20
Session 8
Ver. 1.0
Session 8
Ver. 1.0
Session 8
LAST
10
Ver. 1.0
15
17
2.
3.
4.
20
Session 8
LAST
10
10
Ver. 1.0
15
17
2.
3.
4.
20
Session 8
LAST
10
10
current
Ver. 1.0
15
17
2.
3.
4.
20
Session 8
LAST
10
10
15
17
2.
3.
4.
20
current
Ver. 1.0
Session 8
Deletion Complete
LAST
10
15
17
2.
3.
4.
20
current
Ver. 1.0
Session 8
Ver. 1.0
Session 8
LAST
10
Ver. 1.0
15
17
20
1.
2.
3.
4.
5.
Session 8
LAST
10
10
15
17
20
1.
2.
3.
4.
5.
previous
Ver. 1.0
Session 8
LAST
10
10
15
17
20
1.
2.
3.
4.
5.
current previous
Ver. 1.0
Session 8
LAST
10
10
15
17
20
1.
2.
3.
4.
5.
current previous
Ver. 1.0
Session 8
LAST
10
10
15
17
20
1.
2.
3.
4.
5.
current previous
Ver. 1.0
Session 8
LAST
10
10
15
17
20
1.
2.
3.
4.
5.
Ver. 1.0
Session 8
LAST
10
10
15
17
20
1.
2.
3.
4.
5.
previous current
Ver. 1.0
Session 8
LAST
10
10
15
17
20
1.
2.
3.
4.
5.
previous
previous current
Ver. 1.0
Session 8
15
17
20
1.
2.
3.
4.
5.
Nodes located
Ver. 1.0
Session 8
Ver. 1.0
Session 8
1.
2.
LAST
10
Ver. 1.0
15
17
20
a.
b.
c.
3.
4.
5.
Session 8
2.
LAST
10
10
15
17
20
current
Ver. 1.0
a.
b.
c.
3.
4.
5.
Session 8
2.
LAST
10
10
previous
Ver. 1.0
15
17
20
current
a.
b.
c.
3.
4.
5.
Session 8
2.
LAST
10
10
previous
Ver. 1.0
15
17
20
current
a.
b.
c.
3.
4.
5.
Session 8
2.
LAST
10
10
15
previous previous
Ver. 1.0
17
20
current
a.
b.
c.
3.
4.
5.
Session 8
2.
LAST
10
10
15
previous
Ver. 1.0
17
20
current
a.
b.
c.
3.
4.
5.
Session 8
2.
LAST
10
10
15
17
previous previous
Ver. 1.0
20
current
a.
b.
c.
3.
4.
5.
Session 8
2.
LAST
10
10
15
17
previous
20
current
previous.next = LAST.next
Ver. 1.0
a.
b.
c.
3.
4.
5.
Session 8
LAST
10
10
15
17
previous
Ver. 1.0
2.
LAST
20
current
previous.next = LAST.next
LAST = previous
1.
a.
b.
c.
3.
4.
5.
Session 8
LAST
10
10
15
2.
20
current
previous.next = LAST.next
LAST = previous
a.
b.
c.
Memory released
17
previous
1.
3.
4.
5.
Session 8
Ver. 1.0
Session 8
Ver. 1.0
Session 8
Ver. 1.0
Session 8
Ver. 1.0
Session 8
910
coefficient
exponent
address of the next node
Ver. 1.0
Session 8
Ver. 1.0
Session 8
Session 8
4 10
5
Session 8
4 10
5
11 10
4
Session 8
4 10
5
11 10
4
2 10
3
Session 8
4 10
5
11 10
4
2 10
3
6 10
2
Session 8
9 10
6
4 10
5
11 10
4
2 10
3
6 10
2
7 10
1
Session 8
Ver. 1.0
Session 8
Ver. 1.0
Session 8