W6L2-Doubly Linked List
W6L2-Doubly Linked List
Objectives
START
10 15 19 21 23 25 32
10 15 19 21 23 25 32
10
10 15 17 20
Inserting a Node at the Beginning of the List (Contd.)
1. Allocate memory for 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.
new1 -> next = START
1. Assign value to the data field of
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.
new1 -> next = START
1. Assign value to the data field of
START -> prev = new1 the new 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.
new1 -> prev = NULL 1. Make the next field of the new
node point to the first node in
the list.
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.
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.
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
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.
10 15 17 20
Inserting a Node at the end of the List
1. Allocate memory for the new node.
START LAST
new1
10 15 17 20
Inserting a Node at the end of the List
1. Allocate memory for the new node.
START LAST
new1
10 15 17 20
23
Inserting a Node at the end of the List
1. Allocate memory for the new node.
10 15 17 20 23
Inserting a Node at the end of the List
1. Allocate memory for the new node.
10 15 17 20 23
Inserting a Node at the end of the List
1. Allocate memory for the new node.
10 15 17 20 23
Inserting a Node at the end of the List
1. Allocate memory for the new node.
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
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
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
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
Two Nodes in the List (Contd.) 2. Assign value to the data field of the new
Insert 16 at LOC = 3 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
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
Two Nodes in the List (Contd.) 2. Assign value to the data field of the new
node.
Two Nodes in the List (Contd.) 2. Assign value to the data field of the new
node.
Two Nodes in the List (Contd.) 2. Assign value to the data field of the new
node.
Two Nodes in the List (Contd.) 2. Assign value to the data field of the new
node.
Two Nodes in the List (Contd.) 2. Assign value to the data field of the new
node.
current
Deleting a Node From the Beginning of the List (Contd.)
current
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
current -> next -> prev = previous 1. Make the next field of previous point to
the successor of current.
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.