Академический Документы
Профессиональный Документы
Культура Документы
1.
2.
START
10
15
19
21
32
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
15
ptr1
ptr2
19
ptr3
21
32
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
15
ptr1
ptr2
19
ptr3
21
32
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
15
19
ptr1
ptr1
ptr2
ptr3
21
32
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
ptr1
15
19
ptr2
ptr2
ptr3
21
32
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
ptr1
15
ptr2
19
ptr3
ptr3
21
32
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
ptr1
15
ptr2
19
ptr3
21
32
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
ptr1
15
ptr2
19
ptr3
21
32
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
ptr1
15
ptr2
19
ptr3
21
32
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
ptr1
15
ptr2
ptr1
19
ptr3
21
32
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
15
ptr2
ptr1
19
ptr3
ptr2
21
32
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
15
19
ptr3
ptr1
ptr2
21
ptr3
32
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
15
19
21
ptr3
ptr1
ptr2
32
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
15
19
ptr1
ptr1
ptr2
21
ptr3
32
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
15
19
ptr1
ptr2
21
ptr3
ptr2
32
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
15
19
ptr1
21
ptr3
ptr2
32
ptr3
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
15
19
21
ptr1
32
ptr3
ptr2
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
15
19
ptr1
21
ptr1
ptr2
32
ptr3
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
15
19
21
ptr1
ptr2
32
ptr3
ptr2
Exit.
3.
4.
5.
6.
7.
8.
9.
2.
START
10
15
19
21
ptr1
32
ptr3
ptr2
Exit.
3.
4.
5.
6.
7.
8.
ptr3 = NULL
a.
b.
c.
d.
9.
2.
START
10
15
19
21
32
ptr1
ptr2
Exit.
3.
4.
5.
6.
7.
8.
ptr3 = NULL
a.
b.
c.
d.
9.
START
1.
2.
START
10
15
19
21
32
ptr1
ptr2
Exit.
3.
4.
5.
6.
7.
8.
ptr3 = NULL
a.
b.
c.
d.
9.
15
19
21
23
25
32
START
10
15
19
21
23
25
32
//CodeinC#
classNode
{
publicintinfo;
publicNodenext;
publicNodeprev;
}
//CodeinC++
classNode
{
public:
intinfo;
Node*next;
Node*prev;
};
Just a minute
How does the representation of a node in a doubly-linked
list differ from that of a singly-linked list?
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.
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.
3.
4.
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.
START
10
15
17
1.
2.
3.
4.
5.
6.
20
newnode
START
10
10
15
17
1.
2.
3.
4.
5.
6.
20
newnode
7
START
10
10
15
17
1.
2.
3.
4.
5.
6.
20
2.
3.
4.
5.
6.
newnode.next = START
newnode
7
START
10
10
15
17
20
2.
3.
4.
5.
6.
newnode.next = START
START.prev = newnode
newnode
7
START
10
10
15
17
20
2.
3.
4.
5.
6.
newnode.next = START
START.prev = newnode
newnode.prev = NULL
newnode
7
START
10
10
15
17
20
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
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.
Insert 16
START
10
10
15
17
20
2.
3.
4.
5.
6.
7.
Insert 16
START
10
10
current
15
17
20
2.
3.
4.
5.
6.
7.
Insert 16
START
10
10
15
current
previous = NULL
17
20
2.
3.
4.
5.
6.
7.
Insert 16
START
10
10
15
current
previous = NULL
17
20
2.
3.
4.
5.
6.
7.
Insert 16
START
10
10
15
previous current
previous = NULL
17
20
2.
3.
4.
5.
6.
7.
Insert 16
START
10
10
previous current
15
current
17
20
2.
3.
4.
5.
6.
7.
Insert 16
START
10
10
previous
15
current
17
20
2.
3.
4.
5.
6.
7.
Insert 16
START
10
10
15
17
20
2.
3.
4.
5.
6.
7.
Insert 16
START
10
10
15
17
previous currentcurrent
20
2.
3.
4.
5.
6.
7.
Insert 16
START
10
10
15
previous
17
current
20
2.
3.
4.
5.
6.
7.
newnode
Insert 16
START
10
10
15
previous
17
current
20
2.
3.
4.
5.
6.
7.
newnode
Insert 16
16
START
10
10
15
previous
17
current
20
2.
3.
4.
5.
6.
7.
newnode
16
START
10
10
15
previous
17
current
newnode.next = current
20
2.
3.
4.
5.
6.
7.
newnode
16
START
10
10
15
previous
17
current
newnode.next = current
newnode.prev = previous
20
2.
3.
4.
5.
6.
7.
newnode
16
START
10
10
15
previous
17
current
newnode.next = current
newnode.prev = previous
current.prev = newnode
20
2.
3.
4.
5.
6.
7.
Insertion complete
newnode
16
START
10
10
15
previous
17
current
newnode.next = current
newnode.prev = previous
current.prev = newnode
previous.next = newnode
20
2.
3.
4.
5.
6.
7.
2.
3.
4.
5.
6.
7.
Insert
Refer
23 to the modified algorithm
START
2.
3.
4.
10
15
17
20
5.
6.
7.
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
7.
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
current
7.
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
current
previous = NULL
7.
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
current
previous = NULL
7.
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous current
previous = NULL
7.
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous current
current
7.
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
current
7.
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous current
current
7.
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
current
7.
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
previous current
7.
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous current
current
7.
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
current
7.
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
Insert 23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous current
7.
current = NULL
newnode
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
7.
current = NULL
newnode
23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
7.
current = NULL
newnode.next = current
newnode
23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
7.
current = NULL
newnode.next = current
newnode.prev = previous
newnode
23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
7.
current = NULL
newnode.next = current
newnode.prev = previous
newnode
23
START
2.
3.
4.
10
10
15
17
20
5.
6.
previous
7.
current = NULL
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
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.
1.
2.
3.
START
10
a.
15
17
20
4.
2.
3.
START
10
10
current
a.
15
17
20
4.
START
START
10
10
15
1.
2.
3.
17
current
START = START.next
20
4.
2.
3.
START
10
10
15
a.
17
current
START = START.next
START. prev = NULL
20
4.
1.
2.
3.
START
10
15
a.
17
current
Memory released
START = START.next
START. prev = NULL
20
4.
Algorithm
Delete 17 to delete a node between
two nodes in a doubly-linked list.
START
10
15
17
20
b.
c.
d.
e.
2.
3.
4.
Delete 17
START
10
10
15
17
20
b.
c.
d.
e.
2.
3.
4.
START
10
15
17
20
previous = NULL
b.
c.
d.
e.
2.
3.
4.
START
10
10
15
17
20
current
previous = NULL
b.
c.
d.
e.
2.
3.
4.
START
10
10
15
17
20
current
previous = NULL
b.
c.
d.
e.
2.
3.
4.
START
10
10
15
17
20
current
previous
previous = NULL
b.
c.
d.
e.
2.
3.
4.
START
10
10
current
previous
15
17
20
current
b.
c.
d.
e.
2.
3.
4.
START
10
10
previous
15
17
20
current
previous
b.
c.
d.
e.
2.
3.
4.
START
10
10
15
17
20
previous
current current
previous
b.
c.
d.
e.
2.
3.
4.
START
10
10
15
17
20
previous current
previous. next = current. next
b.
c.
d.
e.
2.
3.
4.
START
10
10
15
17
20
previous current
previous. next = current. next
current.next.prev = previous
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
b.
c.
d.
e.
2.
3.
4.
b.
c.
d.
e.
2.
3.
4.
2.
3.
Refer
Deleteto20the modified algorithm
START
10
a.
15
17
20
4.
2.
3.
Delete 20
START
10
10
a.
15
17
previous
20
current
4.
START
10
10
1.
2.
3.
15
17
previous
20
current
4.
START
10
10
1.
2.
3.
15
17
previous
20
current
4.
START
10
10
1.
2.
3.
15
17
previous
20
4.
current
15
11
23
12
15
11
23
12
Write an algorithm
to traverse
circular linked list.
Algorithm
to traverse
a circularalinked
list.
2.
3.
4.
5.
1.
2.
3.
4.
LAST
10
10
15
17
20
newnode
1.
2.
3.
4.
LAST
10
10
15
17
20
newnode
7
1.
2.
3.
4.
LAST
10
10
15
17
20
newnode
7
1.
2.
3.
4.
LAST
10
10
15
17
20
newnode
Insertion complete
1.
2.
3.
4.
LAST
10
10
15
17
20
1.
2.
3.
4.
5.
LAST
10
15
17
20
2.
3.
4.
5.
LAST
10
10
current
15
17
20
2.
3.
4.
5.
LAST
10
10
15
current
previous = NULL
17
20
2.
3.
4.
5.
LAST
10
10
15
current
previous = NULL
17
20
2.
3.
4.
5.
LAST
10
10
15
previous current
previous = NULL
17
20
2.
3.
4.
5.
LAST
10
10
previous current
15
current
17
20
2.
3.
4.
5.
LAST
10
10
previous
15
previous current
17
20
2.
3.
4.
5.
Nodes located
LAST
10
10
15
17
previous currentcurrent
20
1.
2.
3.
4.
5.
LAST
10
15
17
20
1.
2.
3.
4.
5.
LAST
10
10
15
17
20
newnode
1.
2.
3.
4.
5.
LAST
24
10
10
15
17
20
newnode
1.
2.
3.
4.
5.
LAST
24
10
10
15
17
20
newnode
1.
2.
3.
4.
5.
LAST
24
10
10
15
17
20
1.
2.
3.
4.
5.
LAST
24
10
10
15
17
20
LAST
10
15
17
2.
3.
4.
20
LAST
10
10
15
17
2.
3.
4.
20
LAST
10
10
current
15
17
2.
3.
4.
20
LAST
10
10
15
17
2.
3.
4.
20
current
Deletion Complete
LAST
10
15
17
2.
3.
4.
20
current
LAST
10
15
17
20
1.
2.
3.
4.
5.
LAST
10
10
15
previous
17
20
1.
2.
3.
4.
5.
LAST
10
10
15
current previous
17
20
1.
2.
3.
4.
5.
LAST
10
10
15
current previous
17
20
1.
2.
3.
4.
5.
LAST
10
10
15
current previous
17
20
1.
2.
3.
4.
5.
LAST
10
10
15
17
20
1.
2.
3.
4.
5.
LAST
10
10
15
previous current
17
20
1.
2.
3.
4.
5.
LAST
10
10
15
previous
previous current
17
20
1.
2.
3.
4.
5.
15
17
Nodes located
20
1.
2.
3.
4.
5.
1.
2.
LAST
10
15
17
20
a.
b.
c.
3.
4.
5.
2.
LAST
10
10
15
17
20
current
a.
b.
c.
3.
4.
5.
2.
LAST
10
10
previous
15
17
20
current
a.
b.
c.
3.
4.
5.
2.
LAST
10
10
previous
15
17
20
current
a.
b.
c.
3.
4.
5.
2.
LAST
10
10
15
previous previous
17
20
current
a.
b.
c.
3.
4.
5.
2.
LAST
10
10
15
previous
17
20
current
a.
b.
c.
3.
4.
5.
2.
LAST
10
10
15
17
previous previous
20
current
a.
b.
c.
3.
4.
5.
2.
LAST
10
10
15
17
previous
20
current
previous.next = LAST.next
a.
b.
c.
3.
4.
5.
LAST
10
10
15
17
previous
2.
LAST
20
current
previous.next = LAST.next
LAST = previous
1.
a.
b.
c.
3.
4.
5.
LAST
10
10
15
2.
b.
c.
Memory released
20
17
previous
1.
current
previous.next = LAST.next
LAST = previous
3.
4.
5.
Gaming Applications
Linked lists are implemented in various gaming applications.
Consider a game in which the player protects himself from the
enemy by firing bullets.
Whenever a bullet is fired, its details (size, color, and
coordinates) need to be stored somewhere.
The details of the bullets are stored in a linked list, because the
total number of bullets to be fired are not known in advance.
The coordinates of the bullets stored in a node are updated on
a regular basis to indicate that the bullet is moving forward.
The same is then rendered on the screen.
910
coefficient
exponent
address of the next node
4 10
5
4 10
5
11 10
4
4 10
5
11 10
4
2 10
3
4 10
5
11 10
4
2 10
3
6 10
2
9 10
6
4 10
5
11 10
4
2 10
3
6 10
2
7 10
1
Summary
In this session, you learned that:
In a doubly-linked list, each node needs to store:
The information.
The address of the next node in the list.
The address of the previous node in the list.
Summary (Contd.)
Linked lists offer various applications. For example:
They form the basis of various other data structures such as
stacks and queues, and binary trees.
They are used in various gaming applications.
They are used to implement internals of file system internals in
various operating systems.
They offer a simple and convenient mechanism to perform
various arithmetic operations on polynomial expressions.