Linked List in Data Structures 4
Linked List in Data Structures 4
LECTURE NOTES
MODULE- III
LINKED LIST
[SINGLY LINKED LIST]
2
Before we Start….. ASET
• Email: ngirdhar@amity.edu
X Batch_Lab: v5t3ftw
Y Batch_Lab: tugcd3b
Theory_Lectures: dogxhmu
3
Course Content
Module 1: Introduction to Data Structures
Module 4: Trees
Learning Objectives
Definition of DLL
Representation DLL
Operations on DLL
7
Doubly Linked List
Introduction to Doubly Linked List
• In a single linked list, every node has a link to its next node in the sequence.
• So, we can traverse from one node to another node only in one direction
and we can not traverse back.
• We can solve this kind of problem by using a double linked list.
• A double linked list can be defined as follows
Introduction to Doubly Linked List
• Double linked list is a sequence of elements in which every element has links to its previous
element and next element in the sequence.
• In a double linked list, every node has a link to its previous node and next node. So, we can
traverse forward by using the next field and can traverse backward by using the previous field.
• Every node in a double linked list contains three fields and they are shown in the following
figure
Introduction to Doubly Linked List
• Here, 'link1' field is used to store the address of the previous node in the
sequence, 'link2' field is used to store the address of the next node in the sequence
and 'data' field is used to store the actual value of that node.
Doubly Linked List
• Doubly linked list is a complex type of linked list in which a node contains a pointer to
the previous as well as the next node in the sequence.
• Therefore, in a doubly linked list, a node consists of three parts: node data, pointer to
the next node in sequence (next pointer) , pointer to the previous node (previous
pointer).
//Node in DLL
struct node
{
struct node *prev;
int data;
struct node *next;
}
• The prev part of the first node and the next part of the last node will
always contain null indicating end in each direction.
• Unlike singly linked list, we could traverse only in both direction in DLL
Memory Representation of a Doubly Linked List
Note-
• Searching-Comparing each node data with the item to be searched and return the
location of the item in the list if the item found else return null.
• Traversing- Visiting each node of the list at least once in order to perform some specific
operation like searching, sorting, display, etc.
Doubly Linked List Operations
Inserting at first
Inserting at last
Inserting at mid
Insertion in DLL at beginning
Inserting At Beginning of the list
Step 1 - Create a newNode with given value and newNode → previous as NULL.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, assign NULL to newNode → next and newNode to head.
Step 4 - If it is not Empty then, assign head to newNode → next and newNode to head.
Insertion in doubly linked list at the end
Inserting At End of the list
Step 1 - Create a newNode with given value and newNode → next as NULL.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty, then assign NULL to newNode → previous and newNode to head.
Step 4 - If it is not Empty, then, define a node pointer temp and initialize with head.
Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (until temp →next is equal to NULL).
Step 6 - Assign newNode to temp → next and temp to newNode → previous.
Insertion in doubly linked list after Specified node
Inserting At Specific location in the list
Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, assign NULL to both newNode → previous &
newNode → next and set newNode to head.
Step 4 - If it is not Empty then, define two node pointers temp1 & temp2 and initialize temp1 with head.
Step 5 - Keep moving the temp1 to its next node until it reaches to the node after which we want to insert the newNode
(until temp1 → data is equal to location, here location is the node value after which we want to insert the
newNode).
Step 6 - Every time check whether temp1 is reached to the last node. If it is reached to the last node then display 'Given
node is not found in the list!!! Insertion not possible!!!' and terminate the function. Otherwise move
the temp1 to next node.
Step 7 - Assign temp1 → next to temp2,
newNode to temp1 → next,
temp1 to newNode → previous,
temp2 to newNode → next and
newNode to temp2 → previous.
Inserting At Specific location in the list
Doubly Linked List Operations
At first we need to find the previous and the next node of that
particular node.
Then we need to point the next pointer of the previous node to the
next node of the particular node we want to delete.
Deletion at beginning
Ptr=head
Head=ptrnext
Ptr1prev=null
Deletion from Beginning of the list
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4 - Check whether list is having only one node (temp → previous is equal to temp → next)
Step 5 - If it is TRUE, then set head to NULL and delete temp (Setting Empty list conditions)
Step 6 - If it is FALSE, then assign temp → next to head, NULL to head → previous and delete temp.
Deletion in doubly linked list at the end
Deletion from End of the list
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty, then display 'List is Empty!!! Deletion is not possible' and terminate the function.
Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4 - Check whether list has only one Node (temp → previous and temp → next both are NULL)
Step 5 - If it is TRUE, then assign NULL to head and delete temp. And terminate from the function.
(Setting Empty list condition)
Step 6 - If it is FALSE, then keep moving temp until it reaches to the last node in the list. (until temp →
next is equal to NULL)
Phonebook
In phonebook the names are sorted in
ascending order. If we want to add a new
contact then the memory for the new contact
is created by linked list.
44
Recommended Reading
Textbooks:
• Yashwant Kanetkar,”Data Structure using C”, BPB Publication, 5th Edition ,2011
• A.Tannenbaum,Y. Lanhgsam and A.J. Augenstein ,” Data Structures Using C And C++ “,Prentice Hall of
India,2nd Edition,2009.
• Jean-Paul Tremblay, P.G Sorenson, “An Introduction to Data Structures with applications”, Mcgraw-Hill
,2nd Edition ,1984.
Reference Book:
• Robert L Kruse, “Data Structure and Program Design in C”, Prentice Hall (1991).
• Noel Kalicharan ,“Data Structure in C” ,Ist Edition Create space publisher, 2008.
• Mark Allen Weiss,“Data Structure and algorithm Analysis in C”,2nd Edition AddisonWesley,1996.
• E. Balagurusamy, “Problem Solving through C language”, TMH publication, Fourth Edition, 2008.
• R.S Salaria ,“Data Structures & Algorithms using C”,Khanna Publication,4th Edition,2009
• E.Horowitz and S.Sahni,”Fundamentals of Data Structures in C “,2nd Edition, Universities
Press,2008.
Thank you !