Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
106 views

Linked List in Data Structures 4

The document discusses linked lists and specifically doubly linked lists. It begins with an introduction to doubly linked lists, explaining that each node contains pointers to both the previous and next nodes, allowing traversal in both directions. It then covers key topics like the memory representation of doubly linked lists, common operations like insertion, deletion and searching, and how those operations are performed. The objectives are to teach students about doubly linked lists, their implementation and applications.

Uploaded by

INDIAN REMIX
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

Linked List in Data Structures 4

The document discusses linked lists and specifically doubly linked lists. It begins with an introduction to doubly linked lists, explaining that each node contains pointers to both the previous and next nodes, allowing traversal in both directions. It then covers key topics like the memory representation of doubly linked lists, common operations like insertion, deletion and searching, and how those operations are performed. The objectives are to teach students about doubly linked lists, their implementation and applications.

Uploaded by

INDIAN REMIX
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

DATA STRUCTURES USING C [CSIT124]

LECTURE NOTES
MODULE- III
LINKED LIST
[SINGLY LINKED LIST]

By Dr. Nancy Girdhar


In this session we will talk… ASET

• Course outline and overview


– What is your ideas on Data Structures
– What are we going to study
– Why we will study this
– What are we going to learn
• Quizzes and assignments
• And about you all………

2
Before we Start….. ASET

• Email: ngirdhar@amity.edu

• Google Classroom Codes:

X Batch_Lab: v5t3ftw

Y Batch_Lab: tugcd3b

Theory_Lectures: dogxhmu
3
Course Content
Module 1: Introduction to Data Structures

Module 2: Stacks and Queues

Module 3: Programming with Linked Lists

Module 4: Trees

Module 5: Searching and Sorting

Module 6: Graph and their Applications


Module III: Programming with Linked Lists

• Introduction to Singly linked lists: Representation of linked lists in


memory, Traversing, Searching, Insertion into, Deletion from linked
list, Garbage collection and compaction, doubly linked list,
operations on doubly linked list, circular linked list, operations on
circular linked list, generalized list.

• Applications of Linked List-Polynomial representation using linked


list and basic operation. Stack and queue implementation using
linked list.
6

Learning Objectives​

 Impart in-depth knowledge of data structure and its implementation


in computer programs.
 Make students understand the concepts of Stack linear data
structure.
 Make students understand the applications of Stack.
Contents to be Covered​
Doubly Linked List

Definition of DLL

 Representation DLL

 Operations on DLL

 Uses of Linked List

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-

• DLL consumes more space for


every node and therefore, causes
more expansive basic operations
such as insertion and deletion.

• Easy to manipulate the elements of


the list since the list maintains
pointers in both the directions
Important Points to be Remembered
• In double linked list, the first node must be always pointed by head.
• Always the previous field of the first node must be NULL.
• Always the next field of the last node must be NULL.

Operations on Double Linked List


• In a double linked list, we perform the following operations...
• Insertion
• Deletion
• Display
Node Structure of a Doubly linked list
Operations on DLL
• Insertion-Adding the node into the linked list.
• at beginning
• at end
• after specified node
• Deletion- Removing a node
• at beginning
• at end
• the node having given data- removing the node which is present just after the node
containing the given data.

• 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

There are several operations in doubly linked list:

1. Creation & Traversing


2.Insertion & Traversing
3.Deletion & Traversing
4.Searching & Traversing
Operation: Creation of DLL

• Creation operation is used to create a linked list.

• Generally, we use dynamic memory allocation to create our


desired number of nodes for linked list in program run time.
Creation of Doubly linked list
Doubly Linked List Operations
There are several operations in doubly linked list:
Insertion in Beginning
1. Creation & Traversing
2. Insertion & Traversing Insertion in End
3.Deletion & Traversing
Insertion After a node
4.Searching & Traversing
Insertion
• Insertion operation is used to insert a new node in the linked list.

• Insertion is of three types. They are:

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

There are several operations in doubly linked list:


Insertion in Beginning
1. Creation & Traversing
2. Insertion & Traversing Insertion in End
3.Deletion & Traversing
Insertion After a node
4.Searching & Traversing
Deletion
• Deleting is a simple process.

 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=ptrnext
Ptr1prev=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)

Step 7 - Assign NULL to temp → previous → next and delete temp.


Deletion from End of the list
Deletion in doubly linked list at specific location
Deletion a specific node from 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 - Keep moving the temp until it reaches to the exact node to be deleted or to the last node.
Step 5 - If it is reached to the last node, then display 'Given node not found in the list! Deletion not possible!!!' and
terminate the function.
Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having only one node or not
Step 7 - If list has only one node and that is the node which is to be deleted then set head to NULL and
delete temp (free(temp)).
Step 8 - If list contains multiple nodes, then check whether temp is the first node in the list (temp == head).
Step 9 - If temp is the first node, then move the head to the next node (head = head → next),
set head of previous to NULL (head → previous = NULL) and delete temp.
Step 10 - If temp is not the first node, then check whether it is the last node in the list (temp → next == NULL).
Step 11 - If temp is the last node then set temp of previous of next to NULL (temp → previous → next = NULL) and
delete temp (free(temp)).
Step 12 - If temp is not the first node and not the last node, then set temp of previous of next to temp of next (temp →
previous → next = temp → next), temp of next of previous to temp of previous (temp → next → previous =
temp → previous) and delete temp (free(temp)).
Deletion a specific node from the list
Doubly Linked List Operations

There are several operations in Doubly linked list:

1. Creation & Traversing


2. Insertion & Traversing
3. Deletion & Traversing
4. Displaying
Displays the content of the list from beginning to end
Displays the content of the list from end to beginning
Application of DLL

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 !

You might also like