0% found this document useful (0 votes)
4 views71 pages

W6L2-Doubly Linked List

This document provides a comprehensive guide on implementing and managing doubly linked lists, including algorithms for traversing, inserting, and deleting nodes. It explains the structure of a doubly linked list, which contains references to both the next and previous nodes, allowing for efficient traversal in both directions. The document includes detailed algorithms for inserting nodes at the beginning, end, and between existing nodes in the list.

Uploaded by

vineetkr013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views71 pages

W6L2-Doubly Linked List

This document provides a comprehensive guide on implementing and managing doubly linked lists, including algorithms for traversing, inserting, and deleting nodes. It explains the structure of a doubly linked list, which contains references to both the next and previous nodes, allowing for efficient traversal in both directions. The document includes detailed algorithms for inserting nodes at the beginning, end, and between existing nodes in the list.

Uploaded by

vineetkr013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 71

Doubly Linked Lists

Objectives

In this chapter, you will learn to:


Implement a doubly-linked list
Traversing
Insertion
Deletion
Implementing a Doubly-Linked List

Consider a sorted list of 100000 numbers stored in a linked


list.
If you want to display these numbers in ascending order,
what will you do?
Traverse the list starting from the first node.
Now consider a case in which you need to display these
numbers in a descending order.
How will you solve this problem?
Each node is linked to the next node in sequence.
This means that you can 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.)

What is the problem with the previous algorithm?


You need to adjust the links whenever you visit the next node.
Disadvantage of this approach:
This approach is inefficient and time consuming for large lists.

START

10 15 19 21 23 25 32

How can you solve this problem?


This problem can be solved if each node in the list holds the
reference of its preceding node in addition to its next node in
sequence.
Consider the following list:
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 25 32

Structure of a doubly-linked list


struct node
{
struct node *prev;
int info;
struct node *next;
};
Representing a Doubly-Linked List

A doubly linked list is represented in a program by defining


two addresses :
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
Traversing a Doubly-Linked List

1. Mark the first node in


Write an algorithm to traverse a doubly the list as
linked list in the forward direction. currentNode.

Algorithm to traverse a doubly 1. Repeat steps 3 and 4


linked list in the forward direction. until currentNode
becomes NULL.
Algorithm fwdtraversing()
{ 1. Display the
information contained
1.curr = start in the node marked
2.while(curr != null) as currentNode.

2.1 Print curr -> info 1. Make currentNode


point to the next node
2.2 curr = curr -> next in sequence.
}
Traversing a Doubly-Linked List (Contd.)

Write an algorithm to traverse a doubly linked list in the


backward direction.
1. Mark the last node in
Algorithm to traverse a doubly the list as
linked list in the backward currentNode.
direction.
Algorithm bwddtraversing() 1. Repeat steps 3 and 4
until currentNode
{ becomes NULL.
1.curr = last 1. Display the
2.while(curr != null) information contained
in the node marked
2.1 Print curr -> info as currentNode.
2.2 curr = curr -> prev
1. Make currentNode
} point to the node
preceding it.
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.
1. Allocate memory for
Inserting a Node at the the new node.

Beginning of the List (Contd.) 1. Assign value to the


data field of the new
Algorithm to insert a node in the node.
beginning of a doubly-linked list.
1. Make the next field of
the new node point to
the first node in the
list.

1. Make the prev field of


START point to the
new node.
START
1. Make the prev field of
10 15 17 20 the new node point to
NULL.

1. Make START, point


to the new node.
Inserting a Node at the Beginning of the List (Contd.)
1. Allocate memory for the new
node.

1. Assign value to the data field of


the new node.

1. Make the next field of the new


node point to the first node in
the list.

1. Make the prev field of START


point to the new node.

new1 1. Make the prev field of the new


node point to NULL.

START 1. Make START, point to the new


node.

10
10 15 17 20
Inserting a Node at the Beginning of the List (Contd.)
1. Allocate memory for the new
node.

1. Assign value to the data field of


the new node.

1. Make the next field of the new


node point to the first node in
the list.

1. Make the prev field of START


point to the new node.

new1 1. Make the prev field of the new


node point to NULL.

START 1. Make START, point to the new


7 node.

10
10 15 17 20
Inserting a Node at the Beginning of the List (Contd.)
1. Allocate memory for the new
node.
new1 -> next = START
1. Assign value to the data field of
the new node.

1. Make the next field of the new


node point to the first node in
the list.

1. Make the prev field of START


point to the new node.

new1 1. Make the prev field of the new


node point to NULL.

START 1. Make START, point to the new


7 node.

10
10 15 17 20
Inserting a Node at the Beginning of the List (Contd.)
1. Allocate memory for the new
node.
new1 -> next = START
1. Assign value to the data field of
START -> prev = new1 the new node.

1. Make the next field of the new


node point to the first node in
the list.

1. Make the prev field of START


point to the new node.

new1 1. Make the prev field of the new


node point to NULL.

START 1. Make START, point to the new


7 node.

10
10 15 17 20
Inserting a Node at the Beginning of the List (Contd.)
new1 -> next = START 1. Allocate memory for the new
node.

1. Assign value to the data field of


START -> prev = new1 the new node.

new1 -> prev = NULL 1. Make the next field of the new
node point to the first node in
the list.

1. Make the prev field of START


point to the new node.

new1 1. Make the prev field of the new


node point to NULL.

START 1. Make START, point to the new


7 node.

10
10 15 17 20
Inserting a Node at the Beginning of the List (Contd.)
1. Allocate memory for the new
node.
new1 -> next = START
1. Assign value to the data field of
START -> prev = new1 the new node.

new1 -> prev = NULL 1. Make the next field of the new
node point to the first node in
START = new1 the list.

1. Make the prev field of START


point to the new node.

new1 1. Make the prev field of the new


node point to NULL.

START 1. Make START, point to the new


7 node.

10
10 15 17 20

Insertion complete
Inserting a Node at the Beginning of the List (Contd.)
1. Allocate memory for the new
node.
new1 -> next = START
1. Assign value to the data field of
START -> prev = new1 the new node.

new1 -> prev = NULL 1. Make the next field of the new
node point to the first node in
START = new1 the list.

1. Make the prev field of START


point to the new node.

START 1. Make the prev field of the new


node point to NULL.

1. Make START, point to the new


7 node.

10
10 15 17 20

Insertion complete
ALGORITHM TO INSERT NODE AT BEGINING

Start=NULL
Algorithm InsertAtBEG()
{
1. Create node [(new1=(struct node*) malloc(sizeof(struct node))]
2. Enter data [new1 -> info = data]
3. If (Start == NULL)
3.1 new1 -> next = NULL
3.2 new1 -> prev = NULL
3.3 Last=new1
3.4 Start=new1
else
3.1 new1 -> next = Start
3.2 Start -> prev = new1
3.3 new1 -> Prev = NULL
3.4 Start = new1
}
Inserting a Node At the End in the List

Write an algorithm to insert a node at the end in a doubly-


linked list.
1. Allocate memory for the
new node.

Inserting a Node at the end of the List 1. Assign value to the data
field of the new node.
Algorithm to insert a node at the
end of a doubly-linked list 1. Make the next field of the
node marked as LAST
point to the new node.
Write an algorithm to insert a node at the
end of a doubly-linked list that contains a 1. Make the prev field of new
variable, LAST, to keep track of the last node node point to node marked
of the list. LAST.

1. Make the next field of the


new node point to NULL.

START 1. Mark the new node as


LAST.

10 15 17 20
Inserting a Node at the end of the List
1. Allocate memory for the new node.

1. Assign value to the data field of the new


node.

1. Make the next field of the node marked


as LAST point to the new node.

1. Make the prev field of new node point to


node marked LAST.

1. Make the next field of the new node


point to NULL.

1. Mark the new node as LAST.

START LAST
new1

10 15 17 20
Inserting a Node at the end of the List
1. Allocate memory for the new node.

1. Assign value to the data field of the new


node.

1. Make the next field of the node marked


as LAST point to the new node.

1. Make the prev field of new node point to


node marked LAST.

1. Make the next field of the new node


point to NULL.

1. Mark the new node as LAST.

START LAST
new1

10 15 17 20
23
Inserting a Node at the end of the List
1. Allocate memory for the new node.

1. Assign value to the data field of the new


node.

1. Make the next field of the node marked


as LAST point to the new node.

1. Make the prev field of new node point to


node marked LAST.

1. Make the next field of the new node


point to NULL.

1. Mark the new node as LAST.

START LAST new1

10 15 17 20 23
Inserting a Node at the end of the List
1. Allocate memory for the new node.

1. Assign value to the data field of the new


node.

1. Make the next field of the node marked


as LAST point to the new node.

1. Make the prev field of new node point to


node marked LAST.

1. Make the next field of the new node


point to NULL.

1. Mark the new node as LAST.

START LAST new1

10 15 17 20 23
Inserting a Node at the end of the List
1. Allocate memory for the new node.

1. Assign value to the data field of the new


node.

1. Make the next field of the node marked


as LAST point to the new node.

1. Make the prev field of new node point to


node marked LAST.

1. Make the next field of the new node


point to NULL.

1. Mark the new node as LAST.

START LAST new1

10 15 17 20 23
Inserting a Node at the end of the List
1. Allocate memory for the new node.

1. Assign value to the data field of the new


node.

1. Make the next field of the node marked


as LAST point to the new node.

1. Make the prev field of new node point to


node marked LAST.

1. Make the next field of the new node


point to NULL.

1. Mark the new node as LAST.

START LAST

10 15 17 20 23
ALGORITHM TO INSERT NODE AT END

Start=NULL
Algorithm InsertAtEnd()
{
1. Create node [(new1 = (struct node*) malloc(sizeof(struct node))]
2. Enter data [new1 -> info =data]
3.if(Start == NULL)
3.1 new1 -> next = NULL
3.2 new1 -> prev = NULL
3.3 Last=new1
3.4 Start=new1
else
3.1 Last -> next = new1
3.2 new1 -> prev = Last
3.3 new1 -> next = NULL
3.4 Last = new1
}
Inserting a Node Between Two Nodes in the List

Write an algorithm to insert a node at specific position in a


doubly-linked list.
1. Allocate memory for the new node.

2. Assign value to the data field of the new


Inserting a Node Between node.

Two Nodes in the List (Contd.)3. Identify the nodes after which the new node
is to be inserted. Mark it as previous
a. Make previous node point to the first
Insert 16 node and set count=1
b. Repeat step c and step d until count
START becomes equal to location-1
c. Count=count+1.
d. Make previous point to next node in
10 15 17 20 sequence

4. Make the next field of the new node point to


the next of previous node
Write an algorithm to insert a
node at the particular 5. Make the prev field of newnode point to the
position in a doubly-linked previous node.
list.
6. Make the prev field of the successor node of
current point to the new node.

7. Make the next field of previous point to


the new node.
1. Allocate memory for the new node.

Inserting a Node Between 2. Assign value to the data field of the new
node.
Two Nodes in the List (Contd.) 3. Identify the nodes after which the new
node is to be inserted. Mark it as
previous
a. Make previous node point to the
first node and set count=1
Insert 16 at LOC = 3 b. Repeat step c and step d until
count becomes equal to
location-1
new1 c. Count=count+1.
d. Make previous point to next
node in sequence

START 4. Make the next field of the new node


point to the next of previous node

5. Make the prev field of newnode point to


10
10 15 17 20 the previous node.

6. Make the prev field of the successor


node of current point to the new node.

7. Make the next field of previous point to


the new node.
1. Allocate memory for the new node.

Inserting a Node Between 2. Assign value to the data field of the new
node.
Two Nodes in the List (Contd.) 3. Identify the nodes after which the new
node is to be inserted. Mark it as
previous
a. Make previous node point to the
first node and set count=1
b. Repeat step c and step d until
Insert 16 at LOC = 3 count becomes equal to
location-1
c. Count=count+1.
new1 d. Make previous point to next
node in sequence

16 4. Make the next field of the new node


START point to the next of previous node

5. Make the prev field of newnode point to


the previous node.
10
10 15 17 20
6. Make the prev field of the successor
node of current point to the new node.

7. Make the next field of previous point to


the new node.
Inserting a Node Between
1. Allocate memory for the new node.

Two Nodes in the List (Contd.) 2. Assign value to the data field of the new
Insert 16 at LOC = 3 node.

3. Identify the nodes after which the new


new1 node is to be inserted. Mark it as
previous
a. Make previous node point to the
first node and set count=1
16 b. Repeat step c and step d until
START count becomes equal to
location-1
c. Count=count+1.
d. Make previous point to next
node in sequence
10
10 15 17 20

4. Make the next field of the new node


point to the next of previous node

5. Make the prev field of newnode point to


the previous node.

6. Make the prev field of the successor


node of current point to the new node.

7. Make the next field of previous point to


the new node.
Inserting a Node Between 1. Allocate memory for the new node.

Two Nodes in the List (Contd.) 2. Assign value to the data field of the new
node.
Insert 16 3. Identify the nodes after which the new
new1 node is to be inserted. Mark it as
previous
a. Make previous node point to the
first node and set count=1
16 b. Repeat step c and step d until
START count becomes equal to
location-1
c. Count=count+1.
d. Make previous point to next
node in sequence
10
10 15 17 20

4. Make the next field of the new node


point to the next of previous node
previous
5. Make the prev field of newnode point to
the previous node.
Count=1
6. Make the prev field of the successor
node of current point to the new node.

7. Make the next field of previous point to


the new node.
Inserting a Node Between
1. Allocate memory for the new node.

Two Nodes in the List (Contd.) 2. Assign value to the data field of the new
node.
Insert 16 3. Identify the nodes after which the new
new1 node is to be inserted. Mark it as
previous
a. Make previous node point to the
first node and set count=1
16 b. Repeat step c and step d until
START count becomes equal to
location-1
c. Count=count+1.
d. Make previous point to next
node in sequence
10
10 15 17 20

4. Make the next field of the new node


point to the next of previous node
previous
5. Make the prev field of newnode point to
the previous node.
Count=1
6. Make the prev field of the successor
node of current point to the new node.

7. Make the next field of previous point to


the new node.
Inserting a Node Between 1. Allocate memory for the new node.

Two Nodes in the List (Contd.) 2. Assign value to the data field of the new
node.

3. Identify the nodes after which the new

Insert 16 new1 node is to be inserted. Mark it as


previous
a. Make previous node point to the
first node and set count=1
16 b. Repeat step c and step d until
START count becomes equal to
location-1
c. Count=count+1.
d. Make previous point to next
node in sequence
10
10 15 17 20

4. Make the next field of the new node


point to the next of previous node
previous
5. Make the prev field of newnode point to
the previous node.

Count=2 6. Make the prev field of the successor


node of current point to the new node.

7. Make the next field of previous point to


the new node.
Inserting a Node Between 1. Allocate memory for the new node.

Two Nodes in the List (Contd.) 2. Assign value to the data field of the new
node.

3. Identify the nodes after which the new

Insert 16 new1 node is to be inserted. Mark it as


previous
a. Make previous node point to the
first node and set count=1
16 b. Repeat step c and step d until
START count becomes equal to
location-1
c. Count=count+1.
d. Make previous point to next
node in sequence
10
10 15 17 20

4. Make the next field of the new node


point to the next of previous node
previous
5. Make the prev field of newnode point to
the previous node.

Nodes Located 6. Make the prev field of the successor


node of current point to the new node.

7. Make the next field of previous point to


the new node.
Inserting a Node Between
1. Allocate memory for the new node.

Two Nodes in the List (Contd.) 2. Assign value to the data field of the new
node.

3. Identify the nodes after which the new


new1 node is to be inserted. Mark it as
previous
a. Make previous node point to the
first node and set count=1
16 b. Repeat step c and step d until
START count becomes equal to
location-1
c. Count=count+1.
d. Make previous point to next
node in sequence
10
10 15 17 20

4. Make the next field of the new node


point to the next of previous node
previous
5. Make the prev field of newnode point to
the previous node.
new1 -> next = previous ->next
6. Make the prev field of the successor
node of current point to the new node.

7. Make the next field of previous point to


the new node.
Inserting a Node Between
1. Allocate memory for the new node.

Two Nodes in the List (Contd.) 2. Assign value to the data field of the new
node.

3. Identify the nodes after which the new


new1 node is to be inserted. Mark it as
previous
a. Make previous node point to the
first node and set count=1
16 b. Repeat step c and step d until
START count becomes equal to
location-1
c. Count=count+1.
d. Make previous point to next
node in sequence
10
10 15 17 20

4. Make the next field of the new node


point to the next of previous node
previous
5. Make the prev field of newnode point to
the previous node.

6. Make the prev field of the successor


new1 -> prev = previous node of current point to the new node.

7. Make the next field of previous point to


the new node.
Inserting a Node Between
1. Allocate memory for the new node.

Two Nodes in the List (Contd.) 2. Assign value to the data field of the new
node.

3. Identify the nodes after which the new


newnode node is to be inserted. Mark it as
previous
a. Make previous node point to the
first node and set count=1
16 b. Repeat step c and step d until
START count becomes equal to
location-1
c. Count=count+1.
d. Make previous point to next
node in sequence
10
10 15 17 20

4. Make the next field of the new node


point to the next of previous node
previous
5. Make the prev field of newnode point to
the previous node.

6. Make the prev field of the successor


node of current point to the new node.
previous -> next -> prev = new1 7. Make the next field of previous point to
the new node.
Inserting a Node Between
1. Allocate memory for the new node.
Two Nodes in the List (Contd.) 2. Assign value to the data field of the new
node.

Insertion complete 3. Identify the nodes after which the new


newnode node is to be inserted. Mark it as
previous
a. Make previous node point to the
first node and set count=1
16 b. Repeat step c and step d until
START count becomes equal to
location-1
c. Count=count+1.
d. Make previous point to next
node in sequence
10
10 15 17 20

4. Make the next field of the new node


point to the next of previous node
previous
5. Make the prev field of newnode point to
the previous node.

6. Make the prev field of the successor


node of current point to the new node.

7. Make the next field of previous point to


previous -> next = new1 the new node.
Algorithm InsertAtSpec() ALGORITHM TO INSERT NODE At PARTICULAR POSITION
{
1. Create node [(new1=(struct node*) malloc(sizeof(struct node))]
2. Enter Data and Location
3. new1->info=Data
4. If (Location == 1)
4.1 new1 -> next = Start
4.2 Start -> prev = new1
4.3 new1 -> Prev = NULL
4.4 Start = new1
Else
4.1 Previous = Start
4.2 Count = 1
4.3 While( Count <= Location - 1 && Previous !=NULL)
4.3.1 Previous = Previous -> next
4.3.2 Count++
4.4 new1 -> prev = Previous
4.5 IF ( Previous -> next == NULL)
4.5.1 new1 -> next = NULL;
4.5.2 Previous -> next = new1
4.5.3 Last = new1
Else
4.5.1 new1 -> next = Previous -> next
4.5.2 Previous -> next -> prev = new1
4.5.3 Previous -> next = new1
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.)

1. Mark the first node in


Algorithm to delete a node from the the list as current.
beginning of a doubly-linked list.
1. Make START point to
the next node in
sequence.

START 1. If START is not NULL: /*


If the node to be
deleted is not the only
10 15 17 20 node in the list */

a. Assign NULL to the


prev field of the
node marked as
START.

1. 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.

1. Make START point to the next


node in sequence.

1. If START is not NULL: /* If the


node to be deleted is not the
only node in the list */
START a. Assign NULL to the prev
field of the node marked
as START.
10
10 15 17 20 1. Release the memory of the node
marked as current.

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

1. Mark the first node in the list as


current.

1. Make START point to the next


node in sequence.

1. If START is not NULL: /* If the


node to be deleted is not the
only node in the list */
START START a. Assign NULL to the prev
field of the node marked
as START.
10
10 15 17 20 1. Release the memory of the node
marked as current.

current

START = START -> next


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

1. Mark the first node in the list as


current.

1. Make START point to the next


node in sequence.

1. If START is not NULL: /* If the


node to be deleted is not the
only node in the list */
START a. Assign NULL to the prev
field of the node marked
as START.
10
10 15 17 20 1. Release the memory of the node
marked as current.

current

START -> prev = NULL


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

1. Mark the first node in the list as


Delete operation complete current.

1. Make START point to the next


node in sequence.

1. If START is not NULL: /* If the


node to be deleted is not the
only node in the list */
START a. Assign NULL to the prev
field of the node marked
as START.
10 15 17 20 1. Release the memory of the node
marked as current.

current
Memory released
ALGORITHM TO DELETE A NODE FROM THE BEGINING
Algorithm DeleteAtBeg()
{
1. If (Start == NULL)
1.1 Print "underflow“
else
1.1 Current = Start
1.2 Start = Start -> next
1.3 Start -> prev = NULL
1.3 Current -> next = NULL
1.4 Current -> prev = NULL
1.4 Release the memory [ free (Current) ]

}
Data Structures
Deleting a Node Fromand
End Algorithms

ALGORITHM TO DELETE A NODE FROM THE END

Delete 20 1. Mark the current as


previous node to Last
node.
2. Make the next field of
START LAST current point to the
null.
3. Release the memory
10 15 17 20 for the node marked
as Last.
4. Make Last point to
the current node

Ver. 1.0 Session 8


Data Structures and Algorithms
Deleting a Node From End (Contd.)

1. Mark the current as


Delete 20 previous node to Last
node.
2. Make the next field of
current point to the
START LAST null.
3. Release the memory
for the node marked
10
10 15 17 20 as Last.
4. Make Last point to
the current node
current

Ver. 1.0 Session 8


Data Structures and Algorithms
Deleting a Node From End (Contd.)

1. Mark the current as


previous node to Last
node.
2. Make the next field of
START LAST current point to the
null.
3. Release the memory
10
10 15 17 20 for the node marked
as Last.
4. Make Last point to
the current node
current

Ver. 1.0 Session 8


Data Structures and Algorithms
Deleting a Node From End(Contd.)

1. Mark the current as


previous node to Last
node.
2. Make the next field of
START LAST current point to the
null.
3. Release the memory
10
10 15 17 20 for the node marked
as Last.
4. Make Last point to
the current node
current

Ver. 1.0 Session 8


Data Structures and Algorithms
Deleting a Node From End (Contd.)

1. Mark the current as


previous node to Last
node.
2. Make the next field of
START LAST current point to the
null.
3. Release the memory
10
10 15 17 20 for the node marked
as Last.
4. Make Last point to
the current node
current

Delete operation complete

Ver. 1.0 Session 8


ALGORITHM TO DELETE A NODE FROM THE END
Data Structures and Algorithms
Algorithm DeleteAtEnd()
{
1. If (Start == NULL)
1.1 Print "underflow“
else If (Start -> next == NULL )
1.1 Release the memory [ free (Start) ]
1.2 Start = NULL
1.3 Last = NULL
else
1.1 Current = Last -> prev
1.3 Current -> next = NULL
1.4 Last -> prev = NULL
1.4 Release the memory [ free (Last) ]
1.5 Last = Current
}

Ver. 1.0 Session 8


Deleting a Node Between Two Nodes in the List

Write an algorithm to delete a Particular node in a doubly-


linked list.
1. Mark the node to be deleted as current
and its predecessor as previous. To
locate previous and current, execute
the following steps:
Algorithm to delete a node between
two nodes in a doubly-linked list. a. Make previous point to
NULL. // Set previous = NULL

a. Make current point to the first


node in the linked list. // Set
Delete 17 current = START
START
a. Repeat steps d and e until either
the node is found or current
10 15 17 20 becomes NULL.

a. Make previous point to current.

a. Make current point to the next


node in sequence.

1. Make the next field of previous point to


the successor of current.

1. Make the prev field of the successor of


current point to previous.

1. 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. To
locate previous and current, execute
Delete 17 the following steps:

a. Make previous point to


NULL. // Set previous =
NULL
START
a. Make current point to the first
node in the linked list. // Set
current = START
10
10 15 17 20
a. Repeat steps d and e until
either the node is found or
current becomes NULL.

a. Make previous point to current.

a. Make current point to the next


node in sequence.

1. Make the next field of previous point to


the successor of current.

1. Make the prev field of the successor of


current point to previous.

1. 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. To
locate previous and current, execute
the following steps:

a. Make previous point to


NULL. // Set previous =
NULL
START
a. Make current point to the first
node in the linked list. // Set
current = START
10 15 17 20
a. Repeat steps d and e until
either the node is found or
current becomes NULL.

a. Make previous point to current.

previous = NULL a. Make current point to the next


node in sequence.

1. Make the next field of previous point to


the successor of current.

1. Make the prev field of the successor of


current point to previous.

1. 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. To
locate previous and current, execute
the following steps:

a. Make previous point to


NULL. // Set previous =
NULL
START
a. Make current point to the first
node in the linked list. // Set
current = START
10
10 15 17 20
a. Repeat steps d and e until
either the node is found or
current becomes NULL.
current
a. Make previous point to current.

previous = NULL a. Make current point to the next


node in sequence.

1. Make the next field of previous point to


the successor of current.

1. Make the prev field of the successor of


current point to previous.

1. 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. To
locate previous and current, execute
the following steps:

a. Make previous point to


NULL. // Set previous =
NULL
START
a. Make current point to the first
node in the linked list. // Set
current = START
10
10 15 17 20
a. Repeat steps d and e until
either the node is found or
current becomes NULL.
current
a. Make previous point to current.

previous = NULL a. Make current point to the next


node in sequence.

1. Make the next field of previous point to


the successor of current.

1. Make the prev field of the successor of


current point to previous.

1. 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. To
locate previous and current, execute
the following steps:

a. Make previous point to


NULL. // Set previous =
NULL
START
a. Make current point to the first
node in the linked list. // Set
current = START
10
10 15 17 20
a. Repeat steps d and e until
either the node is found or
current becomes NULL.
current
a. Make previous point to current.
previous
previous = NULL a. Make current point to the next
node in sequence.

1. Make the next field of previous point to


the successor of current.

1. Make the prev field of the successor of


current point to previous.

1. 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. To
locate previous and current, execute
the following steps:

a. Make previous point to


NULL. // Set previous =
NULL
START
a. Make current point to the first
node in the linked list. // Set
current = START
10
10 15 17 20
a. Repeat steps d and e until
either the node is found or
current becomes NULL.
current current
a. Make previous point to current.
previous
a. Make current point to the next
node in sequence.

1. Make the next field of previous point to


the successor of current.

1. Make the prev field of the successor of


current point to previous.

1. 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. To
locate previous and current, execute
the following steps:

a. Make previous point to


NULL. // Set previous =
NULL
START
a. Make current point to the first
node in the linked list. // Set
current = START
10
10 15 17 20
a. Repeat steps d and e until
either the node is found or
current becomes NULL.
current
a. Make previous point to current.
previous previous
a. Make current point to the next
node in sequence.

1. Make the next field of previous point to


the successor of current.

1. Make the prev field of the successor of


current point to previous.

1. 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. To
locate previous and current, execute
the following steps:

a. Make previous point to


NULL. // Set previous =
NULL
START
a. Make current point to the first
node in the linked list. // Set
current = START
10
10 15 17 20
a. Repeat steps d and e until
either the node is found or
current becomes NULL.
previous
current current
a. Make previous point to current.
previous
a. Make current point to the next
node in sequence.

1. Make the next field of previous point to


the successor of current.

1. Make the prev field of the successor of


current point to previous.

1. 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. To
locate previous and current, execute
the following steps:

a. Make previous point to


NULL. // Set previous =
NULL
START
a. Make current point to the first
node in the linked list. // Set
current = START
10
10 15 17 20
a. Repeat steps d and e until
either the node is found or
current becomes NULL.
previous current
a. Make previous point to current.

a. Make current point to the next


node in sequence.

1. Make the next field of previous point to


the successor of current.

1. Make the prev field of the successor of


current point to previous.

1. 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. To
locate previous and current, execute
the following steps:

a. Make previous point to


NULL. // Set previous =
NULL
START
a. Make current point to the first
node in the linked list. // Set
current = START
10
10 15 17 20
a. Repeat steps d and e until
either the node is found or
current becomes NULL.
previous current
a. Make previous point to current.

a. Make current point to the next


node in sequence.

1. Make the next field of previous point to


the successor of current.

1. Make the prev field of the successor of


current point to previous.

1. 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. To
locate previous and current, execute
the following steps:
Deletion complete
a. Make previous point to
NULL. // Set previous =
NULL
START
a. Make current point to the first
node in the linked list. // Set
current = START
10
10 15 17 20
a. Repeat steps d and e until
either the node is found or
current becomes NULL.
previous current
a. Make previous point to current.

a. Make current point to the next


node in sequence.

current -> next -> prev = previous 1. Make the next field of previous point to
the successor of current.

1. Make the prev field of the successor of


current point to previous.

1. 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. To
locate previous and current, execute
the following steps:
Deletion complete
a. Make previous point to
NULL. // Set previous =
NULL
START
a. Make current point to the first
node in the linked list. // Set
current = START
10
10 15 17 20
a. Repeat steps d and e until
either the node is found or
current becomes NULL.
previous current
a. Make previous point to current.

a. Make current point to the next


node in sequence.

current -> next -> prev = previous 1. Make the next field of previous point to
the successor of current.
Deletion Completed
1. Make the prev field of the successor of
current point to previous.

1. Release the memory of the node


marked as current.
ALGORITHM TO DELETE A NODE FROM THE SPECIFIC POSITION
Algorithm DeleteAtSpec()
1. Enter the Location
2. Current = Start
3. Previous = NULL
4. If (Start == 0)
4.1 Print "underflow“
else If ( Location == 1)
4.1 Start = Start -> next
4.2 Start -> prev = NULL
4.3 Current -> next = NULL
4.4 Release the memory [ free (Current) ]
else
4.1 for (i=1; i<Location; i++)
4.1.1 Previous = Current
4.1.2 Current = Current -> next
4.2 if ( Current -> next == NULL)
4.2.1 Previous -> next = NULL
4.2.2 Last = Previous
else
4.2.1 Previous -> next = Current -> next
4.2.2 Current -> next -> prev = Previous
4.3 Current -> next = NULL
4.4 Current -> prev = NULL
4.5 Release the memory [ free (Current) ]
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy