0% found this document useful (0 votes)
18 views

Linked List5

Uploaded by

aThArvA SiNgh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Linked List5

Uploaded by

aThArvA SiNgh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

APEX INSTITUTE OF TECHNOLOGY

Bachelor of Engineering (Computer Science &


Engineering)

Subject: Data Structure


Subject Code: 23CSH-241
Chapter: Linked List
Subject Coordinator:
Ms. Upasana Tiwari
(E15791)

Linked List DISCOVER . LEARN .


Lecture No. 2.1.5 EMPOWER
Index
• Operations on doubly linked list

2 2
DOUBLY LINKED LIST

• Doubly linked list is a linked list in which each node in a


doubly linked list consists of three parts, one is data part
and other two are address parts.
• The data part contains the data or information. Except
the first and the last node, one address part contains the
address of the next node in the list and other address
part contains the address of the previous node in the list.
• Doubly linked lists require more memory space than the
singly linked list because of the one extra address part.
But in doubly linked list the traversal can be done in both
direction .So here movement of any node to any node is
possible.
Example:

Node of Doubly linked list

Example of a doubly linked list


Operations:
• Now in the following sections, we are going to
discussed three basic operations on doubly linked
list which are
• Insertion of a new node
• Deletion of a node
• Traversing the linked list.
Insertion of a new node

Here we will discuss insertion of a new node into a


doubly linked list
• At the first position.
• At the last position.
• At the position which is given by the user.
In the following algorithms two parameters are used.
“dhead” is used to point the first node and element is
used to store the data of the new node to be inserted in
to the doubly linked list.
• ADDRESSNEXT(dnode) means the address part of a
node pointed by the pointer “dnode” which points
the next node in the doubly linked list.
• ADDRESSPREVIOUS(dnode) means the address part
of a node pointed by the pointer “dnode” which
points the previous node in the doubly linked list.
• DATA(dnode) means the data part of a node
pointed by the pointer “dnode” of a doubly linked
list.
Algorithm for inserting new node at the
first position into a doubly linked list:
Insert_first(dhead, element)
Step 1. ALLOCATE MEMORY FOR newnode
Step 2. ADDRESSNEXT(newnode) = NULL
Step 3. ADDRESSPREVIOUS(newnode) = NULL
Step 4. DATA(newnode) = element
Step 5. IF dhead == NULL
Step 6. dhead = newnode
Step 7. END OF IF
Step 8. ELSE
Step 9. ADDRESSNEXT(newnode) = dhead
Step 10. ADDRESSPREVIOUS(dhead) = newnode
Step 11. dhead = newnode
Step 12. END OF ELSE
Example:

Example for insertion of a new node into the first


position in a doubly linked list
Algorithm for inserting new node at the
last position into a doubly linked list:
insert_last(dhead, element)
Step 1. ALLOCATE MEMORY FOR newnode
Step 2. ADDRESSPREVIOUS(newnode) = NULL
Step 3. ADDRESSNEXT(newnode) = NULL
Step 4. DATA(newnode) = element
Step 5. IF dhead == NULL
Step 6. dhead = newnode
Step 7. END OF IF
Step 8. ELSE
Step 9. temp = dhead
Step 10. WHILE ADDRESSNEXT(temp) != NULL
Continue with Example
Step 11. temp = ADDRESSNEXT(temp)
Step 12. ADDRESSPERVIOUS(newnode) = temp
Step 13. ADDRESSNEXT(temp) = newnode
Step 14. END OF WHILE
Step 15. END OF ELSE

Example for insertion of a new node into the last position in a


doubly linked list
Algorithm for inserting new node at a
position which is given by the user into
a doubly linked list:
insert_at_p(dhead,element,pos)
Step 1. count = 1
Step 2. ALLOCATE MEMORY FOR newnode
Step 3. DATA(newnode) = element
Step 4. ADDRESSNEXT(newnode) = NULL
Step 5: ADDRESSPREVIOUS(newnode) = NULL
Step 6. IF pos<=0 OR (pos >1 AND dhead=NULL)
Step 7. PRINT “Wrong input for position”
Step 8. END OF IF
Step 9. ELSE
Step 10. IF pos == 1
Continue..
Step 11. ADDRESSNEXT(newnode) = dhead
Step 12. ADDRESSPREVIOUS(dhead) = newnode
Step 13. dhead = newnode
Step 14. END OF IF
Step 15. temp1 = dhead
Step 16. WHILE count<pos AND ADDRESSNEXT(temp1)
= NULL
Step 17. temp2 = temp1
Step 18. temp1 = ADDRESSNEXT(temp1)
Step 19. count = count + 1
Step 20. END OF WHILE
Continue..
Step 21. IF count == pos
Step 22. ADDRESSNEXT(newnode) = temp1
Step 23. ADDRESSPREVIOUS(newnode) = temp2
Step 24. ADDRESSPREVIOUS(temp1) = newnode
Step 25. ADDRESSNEXT(temp2) = newnode
Step 26. END OF IF
Step 27. ELSE IF count == pos-1
Step 28. ADDRESSNEXT(temp1) = newnode
Step 29. ADDRESSPREVIOUS(newnode) = temp1
Step 30. END OF ELSE IF
Step 31. ELSE
Step 32. PRINT “Wrong input for position”
Step 33. END OF ELSE
Step 34. END OF ELSE
Example:

Example for insertion of a new node into the 3rd


position in a doubly linked list
Deletion of a Node from a
Doubly Linked List
• Here we will discuss deletion of
• The first node
• The last node
• The node whose position is given by the user from a doubly linked
list.
• ADDRESSNEXT(dnode) means the address part of a node
pointed by the pointer “dnode” which points the next node
in the doubly linked list.
• ADDRESSPREVIOUS(dnode) means the address part of a
node pointed by the pointer “dnode” which points the
previous node in the doubly linked list.
• “temp” is a pointer to point any node of a singly linked list.
Algorithm for deletion of
the fist node:
delet_first(dhead)
Step 1. IF dhead == NULL
Step 2. PRINT “ The linked list is empty”
Step 3. END OF IF
Step 4. ELSE
Step 5. temp = dhead
Step 6. dhead = ADDRESSNEXT(dhead)
Step 7. IF dhead != NULL
Step 8. ADDRESSPREVIOUS(dhead) = NULL
Step 9. END OF IF
Step 10. DEALLOCATE MEMORY FOR temp
Step 11. END OF ELSE
Example:

Example for deletion of the first node from a doubly


linked list
Algorithm for deletion of
the last node:
delet_last(dhead)
Step 1. IF dhead == NULL
Step 2. PRINT “ The linked is empty”
Step 3. END OF IF
Step 4. ELSE
Step 5. temp = dhead
Step 6. WHILE ADDRESSNEXT(temp) != NULL
Step 7. temp = ADDRESSNEXT(temp)
Step 8. END OF WHILE
Step 9. IF temp == dhead
Step 10. dhead = NULL
Step 11. END OF IF
Continue with Example
Step 12. ELSE
Step 13. ADDRESSNEXT(ADDRESPRVIOUS(temp))
= NULL
Step 14. END OF ELSE
Step 15. DEALLOCATE MEMORY FOR temp
Step 16. END OF ELSE

Example for deletion of the last node from a doubly linked list
Algorithm for deletion of the node
whose position is given by the user:
delet_p(dhead,pos)
Step 1. count = 1
Step 2. IF dhead == NULL
Step 3. PRINT “ The linked list is empty”
Step 4. END OF IF
Step 5. ELSE
Step 6. temp = dhead
Step 7. IF pos == 1
Step 8. dhead = ADDRESSNEXT(dhead)
Step 9. IF dhead != NULL
Step 10. ADDRESSPREVIOUS(dhead) = NULL
Step 11. END OF IF
Continue..
Step 12. DEALLOCATE MEMORY FOR temp
Step 13. END OF IF
Step 14. WHILE count < pos AND ADDRESSNEXT(temp)! = NULL
Step 15. temp = ADDRESSNEXT(temp)
Step 16. count = count+1
Step 17. END OF WHILE
Step 18. IF pos == count
Step 19.
ADDRESSNEXT(ADDRESPRVIOUS(temp))=ADDRESSNEXT(temp)
Step 20. ADDRESPRVIOUS(ADDRESSNEXT(temp))
= ADDRESSPREVIOUS(temp)
Continue with Example
Step 21. DEALLOCATE MEMORY FOR temp
Step 22. END OF IF
Step 23. ELSE
Step 24. PRINT “ Wrong input for position”
Step 25. END OF ELSE
Step 26. END OF ELSE

Example for deletion of the 3rd node from a doubly linked list
Traversal of Nodes in
Doubly Linked List
In a doubly linked list the traversal of nodes can be
done sequentially in both directions
• from the first node to the last node and
• from the last node to first node.
Algorithm for traversing
nodes in a doubly linked
list:
traverse(dhead)
Step 1. temp = dhead
Step 2. IF dhead == NULL
Step 3. PRINT “The linked list is empty”
Step 4. ELSE
Step 5. /* Traversal in the forward direction */
Step 6. WHILE temp != NULL
Step 7. temp = ADDRESSNEXT(temp)
Step 8. END OF WHILE
Step 9. /* Traversal in the backward direction */
Step 10. WHILE temp !=NULL
Step 11. temp = ADDRESSPREVIOUS(temp)
Step 12. END OF WHILE
Step 13. END OF ELSE
References
WEB LINKS
• https://www.geeksforgeeks.org/data-structures/
• https://www.javatpoint.com/data-structure-tutoria
l
• https://www.tutorialspoint.com/data_structures_al
gorithms/index.htm
VIDEO LINK
• https://www.youtube.com/watch?v=AT14lCXuMKI
&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU
Research Paper
• https://books.google.co.in/books?id=S-tXjl1hsUYC
THANK YOU

27

You might also like