Вы находитесь на странице: 1из 182

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

Implementing a Doubly-Linked List


Now consider
Consider
a sorted
a case
list in
of which
100000
you
numbers
need tostored
displayinthese
a linked
list.
numbers
in a descending order.
If youwill
How
want
youtosolve
display
thisthese
problem?
numbers in ascending order,
what
will node
you do?
Each
is linked to the next node in sequence.
Traverse
thethat
list starting
the first
node.
This
means
you can from
traverse
the list
in the forward
direction only.
Such a linked list is called a singly-linked list.
To display the numbers in the descending order, you need to
reverse the linked list.

Implementing a Doubly-Linked List (Contd.)


Algorithm to reverse a
singly-linked list.

1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15

19

21

32

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15

ptr1

ptr2

19

ptr3

21

32

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15

ptr1

ptr2

19

ptr3

21

32

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15

19

ptr1

ptr1

ptr2

ptr3

21

32

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10
ptr1

15

19

ptr2

ptr2

ptr3

21

32

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10
ptr1

15
ptr2

19
ptr3

ptr3

21

32

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10
ptr1

15
ptr2

19
ptr3

21

32

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10
ptr1

15
ptr2

19
ptr3

21

32

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10
ptr1

15
ptr2

19
ptr3

21

32

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10
ptr1

15
ptr2
ptr1

19
ptr3

21

32

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15
ptr2
ptr1

19
ptr3
ptr2

21

32

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15

19
ptr3

ptr1

ptr2

21
ptr3

32

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15

19

21
ptr3

ptr1

ptr2

32

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15

19
ptr1

ptr1

ptr2

21
ptr3

32

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15

19
ptr1
ptr2

21
ptr3
ptr2

32

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15

19
ptr1

21
ptr3
ptr2

32
ptr3

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15

19

21

ptr1

32
ptr3

ptr2

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15

19
ptr1

21
ptr1
ptr2

32
ptr3

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15

19

21
ptr1
ptr2

32
ptr3
ptr2

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL


a.
b.
c.
d.

9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15

19

21
ptr1

32
ptr3
ptr2

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL

ptr3 = NULL

a.
b.
c.
d.
9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15

19

21

32

ptr1
ptr2

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL

ptr3 = NULL

a.
b.
c.
d.
9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)

START

1.

Declare three variables / pointers ptr1,


ptr2, and ptr 3.

2.

If there is only one node in the list:


a.

START
10

15

19

21

32

ptr1
ptr2

Exit.

3.

Mark the first node in the list as ptr1.

4.

Mark the successor of ptr1 as ptr2

5.

Mark the successor of ptr2 as ptr3.

6.

Make the next field of ptr1 point to NULL.

7.

Make the next field of ptr2 point to ptr1.

8.

Repeat until ptr3 becomes NULL

ptr3 = NULL

a.
b.
c.
d.
9.

Set ptr1 = ptr2


Set ptr2 = ptr3
Make ptr3 point to the next node
in its sequence.
Make the next field of ptr2 point to
ptr1.

Make START point to ptr2.

Implementing a Doubly-Linked List (Contd.)


Whatcan
is the
withproblem?
the previous algorithm?
How
youproblem
solve this
You need
to adjust
the
links for
all the
three
variables
whenever
This
problem
can be
solved
if each
node
in the
list holds
the
you visit the
node. node in addition to its next node in
reference
of next
its preceding
sequence.
Disadvantage of this approach:
Consider
the following
list: and time consuming for large lists.
This approach
is inefficient
START
10

15

19

21

23

25

32

Implementing a Doubly-Linked List (Contd.)


You can introduce an additional field in each node of a
singly-linked list, which would hold the address of its
previous node.
Such a type of list is known as a doubly-linked list.

START
10

15

19

21

23

Structure of a doubly-linked list

25

32

Representing a Doubly-Linked List


A Linked list is represented in a program by defining two
classes:
Node class: In a doubly-linked list, each node needs to store:
The information
The address of the next node in sequence
The address of the previous node

//CodeinC#
classNode
{

publicintinfo;

publicNodenext;

publicNodeprev;
}

Representing a Doubly-Linked List (Contd.)

//CodeinC++
classNode
{
public:
intinfo;
Node*next;
Node*prev;
};

Representing a Doubly-Linked List


(Contd.)
DoubleLinkedListclass: This class consists of a set of
operations implemented on a linked list. In addition, it declares
a variable/pointer, START, which will always point to the first
node in the list.
//CodeinC#
classDoubleLinkedList
{
NodeSTART;
DoubleLinkedList(){}
publicvoidaddNode(intelement){}
publicboolsearch(intelement,refNodeprevious,
refNodecurrent){}
publicbooldelNode(intelement){}
publicvoidtraverse(){}
publicvoidrevtraverse(){}
}

Representing a Doubly-Linked List


(Contd.)
//CodeinC++
classDoubleLinkedList
{
Node*START;
public:
DoubleLinkedList();
voidaddNode(intelement);
boolsearch(intelement,Node*previous,Node
*current);
booldelNode(intelement);
voidtraverse();
voidrevtraverse();
};

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.

Traversing a Doubly-Linked List


1.

Mark the first node in the list as


currentNode.

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.

Display the information


contained in the node marked
as currentNode.

4.

Make currentNode point to the


next node in sequence.

Traversing a Doubly-Linked List (Contd.)


1.

Mark the last node in the list as


currentNode.

3.

Display the information


contained in the node marked
as currentNode.

4.

Make currentNode point to the


node preceding it.

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.

Inserting Nodes in a Doubly-Linked List


A node can be inserted at any of the following positions in a
doubly-linked list:
Beginning of the list
Between two nodes in the list
End of the list

Inserting a Node at the Beginning of the List


Write an algorithm to insert a node in the beginning of a
doubly-linked list.

Inserting a Node at the Beginning of the List (Contd.)


Algorithm to insert a node in the
beginning of a doubly-linked list.

START
10

15

17

1.

Allocate memory for the new


node.

2.

Assign value to the data field of


the new node.

3.

Make the next field of the new


node point to the first node in
the list.

4.

Make the prev field of START


point to the new node.

5.

Make the prev field of the new


node point to NULL.

6.

Make START, point to the new


node.

20

Inserting a Node at the Beginning of the List (Contd.)

newnode
START
10
10

15

17

1.

Allocate memory for the new


node.

2.

Assign value to the data field of


the new node.

3.

Make the next field of the new


node point to the first node in
the list.

4.

Make the prev field of START


point to the new node.

5.

Make the prev field of the new


node point to NULL.

6.

Make START, point to the new


node.

20

Inserting a Node at the Beginning of the List (Contd.)

newnode
7

START
10
10

15

17

1.

Allocate memory for the new


node.

2.

Assign value to the data field of


the new node.

3.

Make the next field of the new


node point to the first node in
the list.

4.

Make the prev field of START


point to the new node.

5.

Make the prev field of the new


node point to NULL.

6.

Make START, point to the new


node.

20

Inserting a Node at the Beginning of the List (Contd.)


1.

Allocate memory for the new


node.

2.

Assign value to the data field of


the new node.

3.

Make the next field of the new


node point to the first node in
the list.

4.

Make the prev field of START


point to the new node.

5.

Make the prev field of the new


node point to NULL.

6.

Make START, point to the new


node.

newnode.next = START

newnode
7

START
10
10

15

17

20

Inserting a Node at the Beginning of the List (Contd.)


1.

Allocate memory for the new


node.

2.

Assign value to the data field of


the new node.

3.

Make the next field of the new


node point to the first node in
the list.

4.

Make the prev field of START


point to the new node.

5.

Make the prev field of the new


node point to NULL.

6.

Make START, point to the new


node.

newnode.next = START
START.prev = newnode

newnode
7

START
10
10

15

17

20

Inserting a Node at the Beginning of the List (Contd.)


1.

Allocate memory for the new


node.

2.

Assign value to the data field of


the new node.

3.

Make the next field of the new


node point to the first node in
the list.

4.

Make the prev field of START


point to the new node.

5.

Make the prev field of the new


node point to NULL.

6.

Make START, point to the new


node.

newnode.next = START
START.prev = newnode
newnode.prev = NULL

newnode
7

START
10
10

15

17

20

Inserting a Node at the Beginning of the List (Contd.)


1.

Allocate memory for the new


node.

2.

Assign value to the data field of


the new node.

3.

Make the next field of the new


node point to the first node in
the list.

4.

Make the prev field of START


point to the new node.

5.

Make the prev field of the new


node point to NULL.

6.

Make START, point to the new


node.

newnode.next = START
START.prev = newnode
newnode.prev = NULL
START = newnode

newnode
7

START
10
10

15

17

20
Insertion complete

Inserting a Node Between Two Nodes in the List


Write an algorithm to insert a node between two nodes in a
doubly-linked list.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

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.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 16
START
10
10

15

17

20
2.
3.
4.
5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 16
START
10
10
current

15

17

20
2.
3.
4.
5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 16
START
10
10

15

current
previous = NULL

17

20
2.
3.
4.
5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 16
START
10
10

15

current
previous = NULL

17

20
2.
3.
4.
5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 16
START
10
10

15

previous current
previous = NULL

17

20
2.
3.
4.
5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 16
START
10
10
previous current

15

current

17

20
2.
3.
4.
5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 16
START
10
10
previous

15

current

17

20
2.
3.
4.
5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 16
START
10
10

15

previous previous current

17

20
2.
3.
4.
5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 16
START
10
10

15

17

previous currentcurrent

20
2.
3.
4.
5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 16
START
10
10

15

previous

17
current

20
2.
3.
4.
5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

newnode

Insert 16
START
10
10

15

previous

17
current

20
2.
3.
4.
5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

newnode

Insert 16

16
START
10
10

15

previous

17
current

20
2.
3.
4.
5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

newnode
16
START
10
10

15

previous

17
current

newnode.next = current

20
2.
3.
4.
5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

newnode
16
START
10
10

15

previous

17
current

newnode.next = current
newnode.prev = previous

20
2.
3.
4.
5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

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.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

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.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

What is the problem with this


algorithm?
If current is NULL, then the new
node should be inserted at the
end of the list.
However, in this case, step 6 will
give an error.
This is because, NULL cannot
have a prev field.
Therefore, you need to modify
this algorithm so that you can
also insert a node at the end of
the list.

2.
3.
4.
5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
Make the prev field of current point to
the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert
Refer
23 to the modified algorithm

START

2.
3.
4.

10

15

17

20

5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 23

START

2.
3.
4.

10
10

15

17

20

5.
6.
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 23

START

2.
3.
4.

10
10

15

17

20

5.
6.

current
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 23

START

2.
3.
4.

10
10

15

17

20

5.
6.

current
previous = NULL

7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 23

START

2.
3.
4.

10
10

15

17

20

5.
6.

current
previous = NULL

7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps e and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 23

START

2.
3.
4.

10
10

15

17

20

5.
6.

previous current
previous = NULL

7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 23

START

2.
3.
4.

10
10

15

17

20

5.
6.

previous current

current
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 23

START

2.
3.
4.

10
10

15

17

20

5.
6.

previous

current
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 23

START

2.
3.
4.

10
10

15

17

20

5.
6.

previous previous current


7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 23

START

2.
3.
4.

10
10

15

17

20

5.
6.

previous current

current
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 23

START

2.
3.
4.

10
10

15

17

20

5.
6.

previous

current
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 23

START

2.
3.
4.

10
10

15

17

20

5.
6.

previous

previous current
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 23

START

2.
3.
4.

10
10

15

17

20

5.
6.

previous current

current
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 23

START

2.
3.
4.

10
10

15

17

20

5.
6.

previous

current
7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 23

START

2.
3.
4.

10
10

15

17

20

5.
6.

previous previous current


7.

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Insert 23

START

2.
3.
4.

10
10

15

17

20

5.
6.

previous current
7.

current = NULL

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

newnode
START

2.
3.
4.

10
10

15

17

20

5.
6.

previous
7.

current = NULL

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

newnode
23

START

2.
3.
4.

10
10

15

17

20

5.
6.

previous
7.

current = NULL

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

newnode.next = current

newnode
23

START

2.
3.
4.

10
10

15

17

20

5.
6.

previous
7.

current = NULL

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

newnode.next = current
newnode.prev = previous

newnode
23

START

2.
3.
4.

10
10

15

17

20

5.
6.

previous
7.

current = NULL

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

newnode.next = current
newnode.prev = previous

newnode
23

START

2.
3.
4.

10
10

15

17

20

5.
6.

previous
7.

current = NULL

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node Between Two Nodes in the List (Contd.)


1.

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

Identify the nodes between which the


new node is to be inserted. Mark them
as previous and current respectively. To
locate previous and current, execute the
following steps:
a.
Make current point to the first
node.
b.
Make previous point to NULL.
c.
Repeat steps d and e until
current.info > newnode.info or
current = NULL.
d.
Make previous point to current.
e.
Make current point to the next
node in sequence.
Allocate memory for the new node.
Assign value to the data field of the new
node.
Make the next field of the new node
point to current.
Make the prev field of the new node
point to previous.
If current is not NULL:
a.
Make the prev field of current
point to the new node.
Make the next field of previous point to
the new node.

Inserting a Node at the end of the List


1.

Allocate memory for the new node.

2.

Assign value to the data field of the new


node.

4.

Make the prev field of new node point to


node marked LAST.

5.

Make the next field of the new node


point to NULL.

6.

Mark the new node as LAST.

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.

Deleting Nodes from a Doubly-Linked List


You can delete a node from one of the following places in a
doubly-linked list:
Beginning of the list
Between two nodes in the list
End of the list

Deleting a Node From the Beginning of the List


Write an algorithm to delete a node from the beginning of a
doubly-linked list.

Deleting a Node From the Beginning of the List (Contd.)


Algorithm to delete a node from the
beginning of a doubly-linked list.

1.

Mark the first node in the list as


current.

2.

Make START point to the next


node in sequence.

3.

If START is not NULL: /* If the


node to be deleted is not the
only node in the list */

START
10

a.

15

17

20

4.

Assign NULL to the prev


field of the node marked
as START.

Release the memory of the


node marked as current.

Deleting a Node From the Beginning of the List (Contd.)


1.

Mark the first node in the list as


current.

2.

Make START point to the next


node in sequence.

3.

If START is not NULL: /* If the


node to be deleted is not the
only node in the list */

START
10
10
current

a.

15

17

20

4.

Assign NULL to the prev


field of the node marked
as START.

Release the memory of the


node marked as current.

Deleting a Node From the Beginning of the List (Contd.)

START

START

10
10

15

1.

Mark the first node in the list as


current.

2.

Make START point to the next


node in sequence.

3.

If START is not NULL: /* If the


node to be deleted is not the
only node in the list */
a.

17

current
START = START.next

20

4.

Assign NULL to the prev


field of the node marked
as START.

Release the memory of the


node marked as current.

Deleting a Node From the Beginning of the List (Contd.)


1.

Mark the first node in the list as


current.

2.

Make START point to the next


node in sequence.

3.

If START is not NULL: /* If the


node to be deleted is not the
only node in the list */

START
10
10

15

a.

17

current
START = START.next
START. prev = NULL

20

4.

Assign NULL to the prev


field of the node marked
as START.

Release the memory of the


node marked as current.

Deleting a Node From the Beginning of the List (Contd.)


Delete operation complete

1.

Mark the first node in the list as


current.

2.

Make START point to the next


node in sequence.

3.

If START is not NULL: /* If the


node to be deleted is not the
only node in the list */

START
10

15

a.

17

current
Memory released
START = START.next
START. prev = NULL

20

4.

Assign NULL to the prev


field of the node marked
as START.

Release the memory of the


node marked as current.

Deleting a Node Between Two Nodes in the List


Write an algorithm to delete a node after any other node in a
doubly-linked list.

Deleting a Node Between Two Nodes in the List (Contd.)


1.

Algorithm
Delete 17 to delete a node between
two nodes in a doubly-linked list.
START
10

15

17

20

Mark the node to be deleted as current


and its predecessor as previous. To
locate previous and current, execute
the following steps:
a.

Make previous point to


NULL. // Set previous =
NULL

b.

Make current point to the first


node in the linked list. // Set
current = START

c.

Repeat steps d and e until


either the node is found or
current becomes NULL.

d.

Make previous point to current.

e.

Make current point to the next


node in sequence.

2.

Make the next field of previous point to


the successor of current.

3.

Make the prev field of the successor of


current point to previous.

4.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)


1.

Delete 17

START
10
10

15

17

20

Mark the node to be deleted as current


and its predecessor as previous. To
locate previous and current, execute
the following steps:
a.

Make previous point to


NULL. // Set previous =
NULL

b.

Make current point to the first


node in the linked list. // Set
current = START

c.

Repeat steps d and e until


either the node is found or
current becomes NULL.

d.

Make previous point to current.

e.

Make current point to the next


node in sequence.

2.

Make the next field of previous point to


the successor of current.

3.

Make the prev field of the successor of


current point to previous.

4.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)


1.

START
10

15

17

20

previous = NULL

Mark the node to be deleted as current


and its predecessor as previous. To
locate previous and current, execute
the following steps:
a.

Make previous point to


NULL. // Set previous =
NULL

b.

Make current point to the first


node in the linked list. // Set
current = START

c.

Repeat steps d and e until


either the node is found or
current becomes NULL.

d.

Make previous point to current.

e.

Make current point to the next


node in sequence.

2.

Make the next field of previous point to


the successor of current.

3.

Make the prev field of the successor of


current point to previous.

4.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)


1.

START
10
10

15

17

20

current
previous = NULL

Mark the node to be deleted as current


and its predecessor as previous. To
locate previous and current, execute
the following steps:
a.

Make previous point to


NULL. // Set previous =
NULL

b.

Make current point to the first


node in the linked list. // Set
current = START

c.

Repeat steps d and e until


either the node is found or
current becomes NULL.

d.

Make previous point to current.

e.

Make current point to the next


node in sequence.

2.

Make the next field of previous point to


the successor of current.

3.

Make the prev field of the successor of


current point to previous.

4.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)


1.

START
10
10

15

17

20

current
previous = NULL

Mark the node to be deleted as current


and its predecessor as previous. To
locate previous and current, execute
the following steps:
a.

Make previous point to


NULL. // Set previous =
NULL

b.

Make current point to the first


node in the linked list. // Set
current = START

c.

Repeat steps d and e until


either the node is found or
current becomes NULL.

d.

Make previous point to current.

e.

Make current point to the next


node in sequence.

2.

Make the next field of previous point to


the successor of current.

3.

Make the prev field of the successor of


current point to previous.

4.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)


1.

START
10
10

15

17

20

current
previous
previous = NULL

Mark the node to be deleted as current


and its predecessor as previous. To
locate previous and current, execute
the following steps:
a.

Make previous point to


NULL. // Set previous =
NULL

b.

Make current point to the first


node in the linked list. // Set
current = START

c.

Repeat steps d and e until


either the node is found or
current becomes NULL.

d.

Make previous point to current.

e.

Make current point to the next


node in sequence.

2.

Make the next field of previous point to


the successor of current.

3.

Make the prev field of the successor of


current point to previous.

4.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)


1.

START
10
10
current
previous

15

17

20

current

Mark the node to be deleted as current


and its predecessor as previous. To
locate previous and current, execute
the following steps:
a.

Make previous point to


NULL. // Set previous =
NULL

b.

Make current point to the first


node in the linked list. // Set
current = START

c.

Repeat steps d and e until


either the node is found or
current becomes NULL.

d.

Make previous point to current.

e.

Make current point to the next


node in sequence.

2.

Make the next field of previous point to


the successor of current.

3.

Make the prev field of the successor of


current point to previous.

4.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)


1.

START
10
10

previous

15

17

20

current
previous

Mark the node to be deleted as current


and its predecessor as previous. To
locate previous and current, execute
the following steps:
a.

Make previous point to


NULL. // Set previous =
NULL

b.

Make current point to the first


node in the linked list. // Set
current = START

c.

Repeat steps d and e until


either the node is found or
current becomes NULL.

d.

Make previous point to current.

e.

Make current point to the next


node in sequence.

2.

Make the next field of previous point to


the successor of current.

3.

Make the prev field of the successor of


current point to previous.

4.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)


1.

START
10
10

15

17

20

previous
current current
previous

Mark the node to be deleted as current


and its predecessor as previous. To
locate previous and current, execute
the following steps:
a.

Make previous point to


NULL. // Set previous =
NULL

b.

Make current point to the first


node in the linked list. // Set
current = START

c.

Repeat steps d and e until


either the node is found or
current becomes NULL.

d.

Make previous point to current.

e.

Make current point to the next


node in sequence.

2.

Make the next field of previous point to


the successor of current.

3.

Make the prev field of the successor of


current point to previous.

4.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)


1.

START
10
10

15

17

20

previous current
previous. next = current. next

Mark the node to be deleted as current


and its predecessor as previous. To
locate previous and current, execute
the following steps:
a.

Make previous point to


NULL. // Set previous =
NULL

b.

Make current point to the first


node in the linked list. // Set
current = START

c.

Repeat steps d and e until


either the node is found or
current becomes NULL.

d.

Make previous point to current.

e.

Make current point to the next


node in sequence.

2.

Make the next field of previous point to


the successor of current.

3.

Make the prev field of the successor of


current point to previous.

4.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)


1.

START
10
10

15

17

20

previous current
previous. next = current. next
current.next.prev = previous

Mark the node to be deleted as current


and its predecessor as previous. To
locate previous and current, execute
the following steps:
a.

Make previous point to


NULL. // Set previous =
NULL

b.

Make current point to the first


node in the linked list. // Set
current = START

c.

Repeat steps d and e until


either the node is found or
current becomes NULL.

d.

Make previous point to current.

e.

Make current point to the next


node in sequence.

2.

Make the next field of previous point to


the successor of current.

3.

Make the prev field of the successor of


current point to previous.

4.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)


1.

Deletion complete
START
10
10

15

17

20

previous current
previous. next = current. next
current.next.prev = previous

Mark the node to be deleted as current


and its predecessor as previous. To
locate previous and current, execute
the following steps:
a.

Make previous point to


NULL. // Set previous =
NULL

b.

Make current point to the first


node in the linked list. // Set
current = START

c.

Repeat steps d and e until


either the node is found or
current becomes NULL.

d.

Make previous point to current.

e.

Make current point to the next


node in sequence.

2.

Make the next field of previous point to


the successor of current.

3.

Make the prev field of the successor of


current point to previous.

4.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)


1.

What is the problem with this


algorithm?
If the last node is to be deleted from the
list, then step 3 will give an error.
This is because, the successor of
current is NULL. Therefore, it cannot
have a prev field.
In that case, you need to modify the
algorithm so that the last node can be
deleted from the list by using the same
algorithm.

Mark the node to be deleted as current


and its predecessor as previous. To
locate previous and current, execute
the following steps:
a.

Make previous point to


NULL. // Set previous =
NULL

b.

Make current point to the first


node in the linked list. // Set
current = START

c.

Repeat steps d and e until


either the node is found or
current becomes NULL.

d.

Make previous point to current.

e.

Make current point to the next


node in sequence.

2.

Make the next field of previous point to


the successor of current.

3.

Make the prev field of the successor of


current point to previous.

4.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)


1.

Mark the node to be deleted as current


and its predecessor as previous.

2.

Make the next field of previous point to


the successor of current.

3.

If the successor of current exists:

Refer
Deleteto20the modified algorithm

START
10

a.

15

17

20

4.

Make the prev field of the


successor of current point to
previous.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)


1.

Mark the node to be deleted as current


and its predecessor as previous.

2.

Make the next field of previous point to


the successor of current.

3.

If the successor of current exists:

Delete 20

START
10
10

a.

15

17
previous

20
current

4.

Make the prev field of the


successor of current point to
previous.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)

START
10
10

1.

Mark the node to be deleted as current


and its predecessor as previous.

2.

Make the next field of previous point to


the successor of current.

3.

If the successor of current exists:


a.

15

17
previous

20
current

4.

Make the prev field of the


successor of current point to
previous.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)

START
10
10

1.

Mark the node to be deleted as current


and its predecessor as previous.

2.

Make the next field of previous point to


the successor of current.

3.

If the successor of current exists:


a.

15

17
previous

20
current

4.

Make the prev field of the


successor of current point to
previous.

Release the memory of the node


marked as current.

Deleting a Node Between Two Nodes in the List (Contd.)

START
10
10

1.

Mark the node to be deleted as current


and its predecessor as previous.

2.

Make the next field of previous point to


the successor of current.

3.

If the successor of current exists:


a.

15

17
previous

20

4.

Make the prev field of the


successor of current point to
previous.

Release the memory of the node


marked as current.

current

Delete operation complete

Implementing a Circular Linked List


Let us
suppose
you are developing
an action game in which
You
can
use a singly-linked
list.
a player isthe
given
a set ofneed
n weapons.
However,
weapons
to be displayed in a sequence
Each weapon
on the
screen after a specific time
repeatedly
in aappears
round robin
manner.
period.
Therefore,
once all the weapons have been displayed, the
The player
required
select
pointer
mustis start
againtowith
thethe
firstweapon
weaponwithin
in the10
list.
seconds
or else
weapon
becomes
unusable.
This
requires
thethe
pointer
to be
reinitialized
each time the
th
Onceofthe
weapon
is displayed, the weapon that came
end
thenlist
is reached.
first
is displayed
process
In
this
situation, itagain
wouldand
be the
good
if aftercontinues.
traversing the node
corresponding to the last weapon, the pointer could
automatically
move towill
theyou
firstuse
weapon
in the
Which data structure
to store
the list.
list of
weapons
in this
case?
This
can be
implemented
by using a circular linked list.

Implementing a Circular Linked List (Contd.)


You can implement a circular linked list by linking the last
node back to the first node in the list.
START
10

15

11

23

12

Implementing a Circular Linked List (Contd.)


In a circular linked list, the last node holds the address of
the first node.
LAST
10

15

11

23

12

In a circular linked list, you need to maintain a


variable/pointer, LAST, which always point to the last node.

Representing a Circular Linked List


To represent a circular linked list, you need to declare two
classes, Node and List:
Node class: The declaration of the Node class of a circular
linked list is the same as that of a singly-linked list.
List class: This class consists of a set of operations
implemented on a linked list. These operations are insertion,
deletion, search, and traversal. It also contains the declaration
of a variable/pointer, LAST, that always points to the last node
in the list.

Representing a Circular Linked List (Contd.)


//CodeinC#
classList
{
privateNodeLAST;
List()
{
LAST=NULL;
}
publicvoidaddNode(intelement){}
publicboolsearch(intelement,refNode
previous,refNodecurrent){}
publicbooldelNode(intelement){}
publicvoidtraverse(){}

Representing a Circular Linked List (Contd.)


//CodeinC++
classList
{
Node*LAST;
public:
List()
{
LAST=NULL;
}
voidaddNode(intelement);
boolsearch(intelement,Node*previous,Node
*current);
booldelNode(intelement);
voidtraverse();
};

Traversing a Circular Linked List


1.

Make currentNode point to


the successor of the node
marked as LAST, such that
currentNode points to the
first node in the list.

Write an algorithm
to traverse
circular linked list.
Algorithm
to traverse
a circularalinked
list.
2.

Repeat step 3 and 4 until


currentNode = LAST.

3.

Display the information


contained in the node
marked as currentNode.

4.

Make currentNode point to


the next node in its
sequence.

5.

Display the information


contained in the node
marked as LAST.

Inserting a Node in a Circular Linked List


In a circular linked list, you can insert a node at any of the
following positions:
Beginning of the list
End of the list
Between two node in the list

Inserting a Node at the Beginning of the List


Write an algorithm to insert a node in the beginning of a
circular linked list.

Inserting a Node at the Beginning of the List (Contd.)


Algorithm to insert a node in the
beginning of a circular linked list.

1.

Allocate memory for the new


node.

2.

Assign value to the data field


of the new node.

3.

Make the next field of the


new node point to the
successor of LAST.

4.

Make the next field of LAST


point to the new node.

LAST
10
10

15

17

20

Inserting a Node at the Beginning of the List (Contd.)

newnode

1.

Allocate memory for the new


node.

2.

Assign value to the data field


of the new node.

3.

Make the next field of the


new node point to the
successor of LAST.

4.

Make the next field of LAST


point to the new node.

LAST
10
10

15

17

20

Inserting a Node at the Beginning of the List (Contd.)

newnode
7

1.

Allocate memory for the new


node.

2.

Assign value to the data field


of the new node.

3.

Make the next field of the


new node point to the
successor of LAST.

4.

Make the next field of LAST


point to the new node.

LAST
10
10

15

17

20

Inserting a Node at the Beginning of the List (Contd.)

newnode
7

1.

Allocate memory for the new


node.

2.

Assign value to the data field


of the new node.

3.

Make the next field of the


new node point to the
successor of LAST.

4.

Make the next field of LAST


point to the new node.

LAST
10
10

15

17

newnode. next = LAST. next

20

Inserting a Node at the Beginning of the List (Contd.)

newnode

Insertion complete

1.

Allocate memory for the new


node.

2.

Assign value to the data field


of the new node.

3.

Make the next field of the


new node point to the
successor of LAST.

4.

Make the next field of LAST


point to the new node.

LAST
10
10

15

17

newnode. next = LAST. next


LAST. next =newnode

20

Inserting a Node Between Two Nodes in the List


The algorithm to insert a node between two nodes in
a circular linked list is same as that of a singly-linked
list.
However, the algorithm to search for the nodes
between which the new node is to be inserted is a bit
different in a circular linked list.

Inserting a Node Between Two Nodes in the List (Contd.)


Algorithm
Insert
16 to locate the nodes
between which the new node is
to be inserted.

1.

Make current point to the first node.

2.

Make previous point to NULL.

3.

Repeat steps 4 and 5 until current.info >


newnode.info or previous = LAST.

4.

Make previous point to current.

5.

Make current point to the next node in


sequence.

LAST
10

15

17

20

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Make current point to the first node.

2.

Make previous point to NULL.

3.

Repeat steps 4 and 5 until current.info >


newnode.info or previous = LAST.

4.

Make previous point to current.

5.

Make current point to the next node in


sequence.

LAST
10
10
current

15

17

20

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Make current point to the first node.

2.

Make previous point to NULL.

3.

Repeat steps 4 and 5 until current.info >


newnode.info or previous = LAST.

4.

Make previous point to current.

5.

Make current point to the next node in


sequence.

LAST
10
10

15

current
previous = NULL

17

20

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Make current point to the first node.

2.

Make previous point to NULL.

3.

Repeat steps 4 and 5 until current.info >


newnode.info or previous = LAST.

4.

Make previous point to current.

5.

Make current point to the next node in


sequence.

LAST
10
10

15

current
previous = NULL

17

20

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Make current point to the first node.

2.

Make previous point to NULL.

3.

Repeat steps 4 and 5 until current.info >


newnode.info or previous = LAST.

4.

Make previous point to current.

5.

Make current point to the next node in


sequence.

LAST
10
10

15

previous current
previous = NULL

17

20

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Make current point to the first node.

2.

Make previous point to NULL.

3.

Repeat steps 4 and 5 until current.info >


newnode.info or previous = LAST.

4.

Make previous point to current.

5.

Make current point to the next node in


sequence.

LAST
10
10
previous current

15
current

17

20

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Make current point to the first node.

2.

Make previous point to NULL.

3.

Repeat steps 4 and 5 until current.info >


newnode.info or previous = LAST.

4.

Make previous point to current.

5.

Make current point to the next node in


sequence.

LAST
10
10
previous

15
previous current

17

20

Inserting a Node Between Two Nodes in the List (Contd.)


1.

Make current point to the first node.

2.

Make previous point to NULL.

3.

Repeat steps 4 and 5 until current.info >


newnode.info or previous = LAST.

4.

Make previous point to current.

5.

Make current point to the next node in


sequence.

Nodes located
LAST
10
10

15

17

previous currentcurrent

20

Inserting a Node at the End of the List


Write an algorithm to insert a node at the end of a circular
linked list.

Inserting a Node at the End of the List (Contd.)


Let us insert
a nodea after
Algorithm
to insert
nodenode
at the20
end of the list

1.

Allocate memory for the new


node.

2.

Assign value to the data field


of the new node.

3.

Make the next field of the


new node point to the
successor of LAST.

4.

Make the next field of LAST


point to the new node.

5.

Mark LAST point to the new


node.

LAST
10

15

17

20

Inserting a Node at the End of the List (Contd.)


Let us insert a node after node 20
newnode

1.

Allocate memory for the new


node.

2.

Assign value to the data field


of the new node.

3.

Make the next field of the


new node point to the
successor of LAST.

4.

Make the next field of LAST


point to the new node.

5.

Mark LAST point to the new


node.

LAST
10
10

15

17

20

Inserting a Node at the End of the List (Contd.)

newnode

1.

Allocate memory for the new


node.

2.

Assign value to the data field


of the new node.

3.

Make the next field of the


new node point to the
successor of LAST.

4.

Make the next field of LAST


point to the new node.

5.

Mark LAST point to the new


node.

LAST
24
10
10

15

17

20

Inserting a Node at the End of the List (Contd.)

newnode

1.

Allocate memory for the new


node.

2.

Assign value to the data field


of the new node.

3.

Make the next field of the


new node point to the
successor of LAST.

4.

Make the next field of LAST


point to the new node.

5.

Mark LAST point to the new


node.

LAST
24
10
10

15

17

20

newnode.next = LAST. next

Inserting a Node at the End of the List (Contd.)

newnode

1.

Allocate memory for the new


node.

2.

Assign value to the data field


of the new node.

3.

Make the next field of the


new node point to the
successor of LAST.

4.

Make the next field of LAST


point to the new node.

5.

Mark LAST point to the new


node.

LAST
24
10
10

15

17

20

newnode.next = LAST. next


LAST. next = newnode

Inserting a Node at the End of the List (Contd.)


Insertion complete
newnode

1.

Allocate memory for the new


node.

2.

Assign value to the data field


of the new node.

3.

Make the next field of the


new node point to the
successor of LAST.

4.

Make the next field of LAST


point to the new node.

5.

Mark LAST point to the new


node.

LAST
24
10
10

15

17

20

newnode.next = LAST. next


LAST. next = newnode
LAST = newnode

Deleting a Node from a Circular Linked List


You can delete a node from any of the following places in a
circular linked list:
Beginning of the list
End of the list
Between two nodes in the list

Deleting a Node From the Beginning of the List


Write an algorithm to delete a node from the beginning of a
circular linked list.

Deleting a Node From the Beginning of the List (Contd.)


1.

Algorithm for deleting a node from the


beginning of a circular linked list.

LAST
10

15

17

If the node to be deleted is


the only node in the
list:
// If LAST points to
itself
a.
b.

Mark LAST as NULL


Exit

2.

Make current point to the


successor of LAST

3.

Make the next field of LAST


point to the successor of
current

4.

Release the memory for the


node marked as current

20

Deleting a Node From the Beginning of the List (Contd.)


1.

If the node to be deleted is


the only node in the
list:
// If LAST points to
itself
a.
b.

LAST
10
10

15

17

Mark LAST as NULL


Exit

2.

Make current point to the


successor of LAST

3.

Make the next field of LAST


point to the successor of
current

4.

Release the memory for the


node marked as current

20

Deleting a Node From the Beginning of the List (Contd.)


1.

If the node to be deleted is


the only node in the
list:
// If LAST points to
itself
a.
b.

LAST
10
10
current

15

17

Mark LAST as NULL


Exit

2.

Make current point to the


successor of LAST

3.

Make the next field of LAST


point to the successor of
current

4.

Release the memory for the


node marked as current

20

Deleting a Node From the Beginning of the List (Contd.)


1.

If the node to be deleted is


the only node in the
list:
// If LAST points to
itself
a.
b.

LAST
10
10

15

17

2.

Make current point to the


successor of LAST

3.

Make the next field of LAST


point to the successor of
current

4.

Release the memory for the


node marked as current

20

current

LAST. next = current.next

Mark LAST as NULL


Exit

Deleting a Node From the Beginning of the List (Contd.)


1.

Deletion Complete

If the node to be deleted is


the only node in the
list:
// If LAST points to
itself
a.
b.

LAST
10

15

17

2.

Make current point to the


successor of LAST

3.

Make the next field of LAST


point to the successor of
current

4.

Release the memory for the


node marked as current

20

current

LAST. next = current.next

Mark LAST as NULL


Exit

Deleting a Node Between Two Nodes in the List


Delete operation in between two nodes in a circular linked
list is same as that of a singly-linked list.
However, the algorithm to locate the node to be deleted is a
bit different in a circular linked list.

Deleting a Node Between Two Nodes in the List


Delete 17 for locating the node to be
Algorithm
deleted from a circular linked list.

LAST
10

15

17

20

1.

Make previous point to


the successor of LAST

2.

Make current point to the


successor of LAST

3.

Repeat steps 4 and 5


until either the node is
found, or previous =
LAST

4.

Make previous point to


current

5.

Make current point to the


next node in sequence

Deleting a Node Between Two Nodes in the List


Delete 17

LAST
10
10

15

previous

17

20

1.

Make previous point to


the successor of LAST

2.

Make current point to the


successor of LAST

3.

Repeat steps 4 and 5


until either the node is
found, or previous =
LAST

4.

Make previous point to


current

5.

Make current point to the


next node in sequence

Deleting a Node Between Two Nodes in the List


Delete 17

LAST
10
10

15

current previous

17

20

1.

Make previous point to


the successor of LAST

2.

Make current point to the


successor of LAST

3.

Repeat steps 4 and 5


until either the node is
found, or previous =
LAST

4.

Make previous point to


current

5.

Make current point to the


next node in sequence

Deleting a Node Between Two Nodes in the List


Delete 17

LAST
10
10

15

current previous

17

20

1.

Make previous point to


the successor of LAST

2.

Make current point to the


successor of LAST

3.

Repeat steps 4 and 5


until either the node is
found, or previous =
LAST

4.

Make previous point to


current

5.

Make current point to the


next node in sequence

Deleting a Node Between Two Nodes in the List


Delete 17

LAST
10
10

15

current previous

17

20

1.

Make previous point to


the successor of LAST

2.

Make current point to the


successor of LAST

3.

Repeat steps 4 and 5


until either the node is
found, or previous =
LAST

4.

Make previous point to


current

5.

Make current point to the


next node in sequence

Deleting a Node Between Two Nodes in the List


Delete 17

LAST
10
10

15

current previous current

17

20

1.

Make previous point to


the successor of LAST

2.

Make current point to the


successor of LAST

3.

Repeat steps 4 and 5


until either the node is
found, or previous =
LAST

4.

Make previous point to


current

5.

Make current point to the


next node in sequence

Deleting a Node Between Two Nodes in the List


Delete 17

LAST
10
10

15

previous current

17

20

1.

Make previous point to


the successor of LAST

2.

Make current point to the


successor of LAST

3.

Repeat steps 4 and 5


until either the node is
found, or previous =
LAST

4.

Make previous point to


current

5.

Make current point to the


next node in sequence

Deleting a Node Between Two Nodes in the List


Delete 17

LAST
10
10

15

previous
previous current

17

20

1.

Make previous point to


the successor of LAST

2.

Make current point to the


successor of LAST

3.

Repeat steps 4 and 5


until either the node is
found, or previous =
LAST

4.

Make previous point to


current

5.

Make current point to the


next node in sequence

Deleting a Node Between Two Nodes in the List


Once you locate the node to be
deleted along with its predecessor,
the process of deletion is same as
that of a singly-linked list.
LAST
10
10

15

17

previous current current

Nodes located

20

1.

Make previous point to


the successor of LAST

2.

Make current point to the


successor of LAST

3.

Repeat steps 4 and 5


until either the node is
found, or previous =
LAST

4.

Make previous point to


current

5.

Make current point to the


next node in sequence

Deleting a Node From the End of the List


Write an algorithm to delete a node from the end of a
circular linked list.

Deleting a Node From the End of the List (Contd.)


Let
Algorithm
us perform
for deleting
a delete
a operation
node fromon
thethe
endnode
of a circular
linkedcircular
list. list.
last
of the given

1.

Make current point to LAST.

2.

Mark the predecessor of LAST


as previous. To locate the
predecessor of LAST, execute
the following steps:

LAST
10

15

17

20

a.

Make previous point to


the successor of LAST.

b.

Repeat step c until the


successor of previous
becomes LAST.

c.

Make previous point to


the next node in its
sequence.

3.

Make the next field of previous


point to the successor of LAST.

4.

Mark previous as LAST.

5.

Release the memory for the


node marked as current.

Deleting a Node From the End of the List (Contd.)


1.

Make current point to LAST.

2.

Mark the predecessor of LAST


as previous. To locate the
predecessor of LAST, execute
the following steps:

LAST
10
10

15

17

20
current

a.

Make previous point to


the successor of LAST.

b.

Repeat step c until the


successor of previous
becomes LAST.

c.

Make previous point to


the next node in its
sequence.

3.

Make the next field of previous


point to the successor of LAST.

4.

Mark previous as LAST.

5.

Release the memory for the


node marked as current.

Deleting a Node From the End of the List (Contd.)


1.

Make current point to LAST.

2.

Mark the predecessor of LAST


as previous. To locate the
predecessor of LAST, execute
the following steps:

LAST
10
10
previous

15

17

20
current

a.

Make previous point to


the successor of LAST.

b.

Repeat step c until the


successor of previous
becomes LAST.

c.

Make previous point to


the next node in its
sequence.

3.

Make the next field of previous


point to the successor of LAST.

4.

Mark previous as LAST.

5.

Release the memory for the


node marked as current.

Deleting a Node From the End of the List (Contd.)


1.

Make current point to LAST.

2.

Mark the predecessor of LAST


as previous. To locate the
predecessor of LAST, execute
the following steps:

LAST
10
10
previous

15

17

20
current

a.

Make previous point to


the successor of LAST.

b.

Repeat step c until the


successor of previous
becomes LAST.

c.

Make previous point to


the next node in its
sequence.

3.

Make the next field of previous


point to the successor of LAST.

4.

Mark previous as LAST.

5.

Release the memory for the


node marked as current.

Deleting a Node From the End of the List (Contd.)


1.

Make current point to LAST.

2.

Mark the predecessor of LAST


as previous. To locate the
predecessor of LAST, execute
the following steps:

LAST
10
10

15

previous previous

17

20
current

a.

Make previous point to


the successor of LAST.

b.

Repeat step c until the


successor of previous
becomes LAST.

c.

Make previous point to


the next node in its
sequence.

3.

Make the next field of previous


point to the successor of LAST.

4.

Mark previous as LAST.

5.

Release the memory for the


node marked as current.

Deleting a Node From the End of the List (Contd.)


1.

Make current point to LAST.

2.

Mark the predecessor of LAST


as previous. To locate the
predecessor of LAST, execute
the following steps:

LAST
10
10

15
previous

17

20
current

a.

Make previous point to


the successor of LAST.

b.

Repeat step c until the


successor of previous
becomes LAST.

c.

Make previous point to


the next node in its
sequence.

3.

Make the next field of previous


point to the successor of LAST.

4.

Mark previous as LAST.

5.

Release the memory for the


node marked as current.

Deleting a Node From the End of the List (Contd.)


1.

Make current point to LAST.

2.

Mark the predecessor of LAST


as previous. To locate the
predecessor of LAST, execute
the following steps:

LAST
10
10

15

17

previous previous

20
current

a.

Make previous point to


the successor of LAST.

b.

Repeat step c until the


successor of previous
becomes LAST.

c.

Make previous point to


the next node in its
sequence.

3.

Make the next field of previous


point to the successor of LAST.

4.

Mark previous as LAST.

5.

Release the memory for the


node marked as current.

Deleting a Node From the End of the List (Contd.)


1.

Make current point to LAST.

2.

Mark the predecessor of LAST


as previous. To locate the
predecessor of LAST, execute
the following steps:

LAST
10
10

15

17
previous

20
current

previous.next = LAST.next

a.

Make previous point to


the successor of LAST.

b.

Repeat step c until the


successor of previous
becomes LAST.

c.

Make previous point to


the next node in its
sequence.

3.

Make the next field of previous


point to the successor of LAST.

4.

Mark previous as LAST.

5.

Release the memory for the


node marked as current.

Deleting a Node From the End of the List (Contd.)

LAST
10
10

15

17
previous

Make current point to LAST.

2.

Mark the predecessor of LAST


as previous. To locate the
predecessor of LAST, execute
the following steps:

LAST
20
current

previous.next = LAST.next
LAST = previous

1.

a.

Make previous point to


the successor of LAST.

b.

Repeat step c until the


successor of previous
becomes LAST.

c.

Make previous point to


the next node in its
sequence.

3.

Make the next field of previous


point to the successor of LAST.

4.

Mark previous as LAST.

5.

Release the memory for the


node marked as current.

Deleting a Node From the End of the List (Contd.)

LAST
10
10

15

Make current point to LAST.

2.

Mark the predecessor of LAST


as previous. To locate the
predecessor of LAST, execute
the following steps:
a.

Make previous point to


the successor of LAST.

b.

Repeat step c until the


successor of previous
becomes LAST.

c.

Make previous point to


the next node in its
sequence.

Memory released
20

17
previous

1.

current

previous.next = LAST.next
LAST = previous

Delete operation complete

3.

Make the next field of previous


point to the successor of LAST.

4.

Mark previous as LAST.

5.

Release the memory for the


node marked as current.

Applications of Linked Lists


Linked lists provide an efficient mechanism for storing data.
They are used to solve many programming problems easily.
They form the basic foundation for implementing various
other data structures such as stacks, queues, and binary
trees.

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.

File System in Operating System


Linked lists are used to implement the internals of a file
system.
A file is divided into blocks, which are scattered randomly on
the disk.
When a new file is created, a new block of memory is
allocated to it.
The new block may not be contiguous to the block that was
earlier allocated.
Therefore, each block also contains the address of the next
allocated block, forming a linked list.

Adding Polynomials Using Linked Lists


Linked lists can be used to implement various arithmetic
operations on polynomial expressions.
In the polynomial expression, 4x5 + 5x4 + 2x3 + 3x2 + 7x,
each occurrence of variable x is attached to two values:
Coefficient
Exponent

Adding Polynomials Using Linked Lists (Contd.)


Each node holds three pieces of information:
Coefficient
Exponent
Address of the next node in sequence

910

coefficient
exponent
address of the next node

Adding Polynomials Using Linked Lists (Contd.)


Let us now perform addition of two polynomial expressions.

Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x

Polynomial 2: 9x6 + 6x4 + 3x2

Adding Polynomials Using Linked Lists (Contd.)

Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x

Polynomial 2: 9x6 + 6x4 + 3x2

Read the first node of Polynomial 1 and the first node of


Polynomial 2.
Compare the exponent values of the two nodes.
The node read from Polynomial 2 has a greater exponent
value (6>5).
Add the node with exponent value 6 to the list for
Polynomial 3.
9 10
6
Polynomial 3: 9x6

Adding Polynomials Using Linked Lists (Contd.)

Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x

Polynomial 2: 9x6 + 6x4 + 3x2

Read the next node from Polynomial 2.


Compare the exponent value of current node of Polynomial
1 with that of Polynomial 2.
The node read from Polynomial 1 has a greater exponent
value (5>4). Therefore, add the node with the exponent
value 5 to the list for Polynomial 3.
9 10
6

4 10
5

Polynomial 3: 9x6 + 4x5

Adding Polynomials Using Linked Lists (Contd.)

Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x

Polynomial 2: 9x6 + 6x4 + 3x2

Read the next node from Polynomial 1.


Compare the exponent value of the current node of
Polynomial 1 with that of Polynomial 2. Both the exponents
are equal, that is, 4.
Add the coefficients of both the nodes. The resultant node
now has the coefficient value 11 (6+5).
Add the resultant node to the list for Polynomial 3.
9 10
6

4 10
5

11 10
4

Polynomial 3: 9x6 + 4x5 + 11x4

Adding Polynomials Using Linked Lists (Contd.)

Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x

Polynomial 2: 9x6 + 6x4 + 3x2

Read the next node from both the lists.


Compare the exponent value of the current node of
Polynomial 1 with that of Polynomial 2.
The node read from Polynomial 1 has a greater exponent
value (3>2).
Add the node with the exponent value 3 to the list for
Polynomial 3.
9 10
6

4 10
5

11 10
4

2 10
3

Polynomial 3: 9x6 + 4x5 + 11x4 + 2x3

Adding Polynomials Using Linked Lists (Contd.)

Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x

Polynomial 2: 9x6 + 6x4 + 3x2

Read the next node from Polynomial 1.


Compare the exponent value of the current node of
Polynomial 1 with that of Polynomial 2. Both the exponents
are equal, that is, 2.
Add the coefficients of both the nodes. The resultant node
now has the coefficient value 6 (3+3).
Add the resultant node to the list for Polynomial 3.
9 10
6

4 10
5

11 10
4

2 10
3

Polynomial 3: 9x6 + 4x5 + 11x4 + 2x3 + 6x2

6 10
2

Adding Polynomials Using Linked Lists (Contd.)

Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x

Polynomial 2: 9x6 + 6x4 + 3x2

Now there are no more nodes in Polynomial 2.


Therefore, add the remaining nodes of Polynomial 1 to the
resultant list.

9 10
6

4 10
5

11 10
4

2 10
3

6 10
2

Polynomial 3: 9x6 + 4x5 + 11x4 + 2x3 + 6x2 + 7x

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.

Doubly-linked list enables you to traverse the list in forward as


well as backward direction.
A singly-linked list can be made circular by making the last
node in the list point back to the first 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.

Вам также может понравиться