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

Chepter 3 Dsa - Linked List

Uploaded by

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

Chepter 3 Dsa - Linked List

Uploaded by

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

Data Structure & Algorithms

CHEPTER-3
Linked List

Internal
Sessions Topics
To understands the concept of
• Singly linked lists: Representation in memory, Algorithms of several
operations: Traversing, Searching, Insertion into, Deletion from linked
list
• Linked representation of Stack and Queue, Header nodes
• Doubly linked list: operations on it and algorithmic analysis
• Circular Linked Lists: all operations their algorithms and the
complexity analysis

Internal
What is Linked Lists
• A linked list is a collection of “nodes” connected together via links. These
nodes consist of the data to be stored and a pointer to the address of the
next node within the linked list.
• A linked list is also a collection of elements, but the elements are not
stored in a consecutive location.
• Linked List is a sequence of links which contains items. Each link contains a
connection to another link. Linked list is the second most-used data
structure after array.

Internal
Why use linked list over array?
Array contains following limitations:
• The size of array must be known in advance before using it in the program.
• Increasing size of the array is a time taking process. It is almost impossible
to expand the size of the array at run time.
• All the elements in the array need to be contiguously stored in the memory.
Linked list is the data structure which can overcome the limitations of an
array:
• It allocates the memory dynamically.
• Sizing is no longer a problem since we do not need to define its size at the
time of declaration.

Internal
Linked List Representation
• Linked list can be visualized as a chain of
nodes, where every node points to the next
node.
Following are the important points to be
considered:-
• Linked List contains a link element called first
(head).
• Each link carries a data field(s) and a link field
called next.
• Each link is linked with its next link using its
next link.
• Last link carries a link as null to mark the end
of the list.
• Here, the start pointer stores the address of
the first node, and at the end, there is a null
pointer that states the end of the Linked List.

Internal
Type of Linked List
1. Singly Linked List − The nodes only point to the address of the next
node in the list.
2. Doubly Linked List − The nodes point to the addresses of both
previous and next nodes.
3. Circular Linked List − The last node in the list will point to the first
node in the list. It can either be singly linked or doubly linked.

Internal
SINGLY LINKED LIST AND THEIR OPERATIONS

Internal
Singly Linked List
• A singly linked list is a special type of linked list in which each node has only one link that points to
the next node in the linked list.
• It is called as one way chain.
• The number of elements may vary according to need of the program.
• Singly linked lists contain two “buckets” in one node; one bucket holds the data and the other
bucket holds the address of the next node of the list.
• Traversals can be done in one direction only as there is only a single link between two nodes of
the same list.

Internal
One Way Chain
• Data part of the node stores actual information that is to be represented by
the node while the link part of the node stores the address of its immediate
successor.
• One way chain or singly linked list can be traversed only in one direction.
• In other words, we can say that each node contains only next pointer,
therefore we can not traverse the list in the reverse direction.

Internal
Representation of Singly Linked List
Suppose we want to store a list of numbers : 23, 54, 78, 90

Internal
How to access the first node of Singly Linked
List
• If we have the address of the first node then we can reach out to the
other node.
• For that we required a pointer which is called Head pointer, who store
the address of the first node.

Internal
How can we declare Linked List?

In the mentioned declaration, we have defined


a structure named as a node consisting of two
variables: an integer variable (data), and the
other one is the pointer (next), which contains
the address of the next node.

Internal
Node Creation
struct node
{
int data;
struct node *next;
};

struct node *head, *ptr;


head = (struct node *) malloc (sizeof(struct node *));
ptr = (struct node *) malloc (sizeof(struct node *));

Internal
Internal
Internal
Program

Internal
Program

Internal
But there is problem?

Internal
What is the problem?

• Head is now pointing to the second node .


• And Now there is no way to access the first
node of the list.

Internal
Solution

Internal
Solution (Conti…)

Internal
Operations on Singly Linked List
1. Traversal - access each element of the linked list
2. Count the linked list node
3. Insertion - adds a new element to the linked list
(i) Insert at beginning
(ii) Insert at end
(iii) Insert at specific location
4. Deletion - removes the existing elements
(i) Delete the first node
(ii) Delete the last node
(iii) Delete after a given node
5. Search - find a node in the linked list

Internal
Operations on Linked List (Conti.)
1.Traversing to a Linked List
Traversing a SLL means visiting each node of a single linked list until the end node is
reached.
Algorithm for Traversing to a Linked List

Internal
Operations on Linked List (Conti.)
2. Count the Linked List node
To do this, we will traverse each and every node of the list and while traversing every individual node, we will
increment the counter by 1.
Once we reach NULL, that is, when all the nodes of the linked list have been traversed,

Algorithm for Counting no of Nodes

Internal
Program to Count the Node

We can call the function count_of_nodes (head) with the argument head, that is actually pointing
to first node

Internal
Operations on Linked List (Conti.)
We will see how a new node can be added to an existing linked list in the following cases.

3.1 Insert at beginning


Suppose we want to insert new node at the very beginning of the linked list then this procedure is suitable to
follow.
Algorithm for Insert at beginning • In Step 1, we first check whether memory is available for the new
node.
• If the free memory has exhausted, then an OVERFLOW message is
printed.
• Otherwise, if a free memory cell is available, then we allocate
space for the new node.
• Set its DATA part with the given VAL and the next part is initialized
with the address of the first node of the list, which is stored in
START.
• Now, since the new node is added as the first node of the list, it
will now be known as the START node, that is, the START pointer
variable will now hold the address of the NEW_NODE.

Internal
Example of Insert at beginning

Internal
Program to Insert at Beginning

Internal
#include <stdio.h> struct node *display(struct node *start)
#include <stdlib.h> {
#include <malloc.h> struct node *ptr;
ptr = start;
struct node while(ptr != NULL)
{ {
int data; printf("%d----->", ptr->data);
struct node *next; //printf(“ %d”, ptr->data);
}; ptr = ptr -> next;
struct node *start = NULL; }
struct node* insert_beg(struct node*); printf("NULL"); struct node *insert_beg(struct node *start)
struct node *display(struct node*); return start; {
} struct node *new_node;
int main() int num;
{ printf("Enter the data");
for(int i=0;i<4;i++) scanf("%d",&num);
{ start = insert_beg(start); } //scanf(“%d”,&num);
start = display(start); new_node = (struct node *)malloc(sizeof(struct node));
return 0; new_node -> data = num;
} Enter the data10
Enter the data20 new_node -> next = start;
Enter the data30 start = new_node;
Enter the data40 return start;
40----->30----->20----->10----->NUL }
Internal
Operations on Linked List (Conti.)
3.2 Insert at ending
Suppose we want to insert new node at the end of the linked list then this procedure is suitable to follow.

Algorithm for Insert at ending • Suppose we want to add a new node with data 9 as the last node
of the list.
• Then the following changes will be done in the linked list that is
• In Step 6, we take a pointer variable PTR and initialize it with
START.
• That is, PTR now points to the first node of the linked list.
• In the while loop, we traverse through the linked list to reach the
last node.
• Once we reach the last node, in Step 9, we change the NEXT
pointer of the last node to store the address of the new node.
• Remember that the NEXT field of the new node contains NULL,
which signifies the end of the linked list.

Internal
Example of Insert at ending

Internal
Program to Insert at ending

Internal
Operations on Linked List (Conti.)
3.3 Insert at specific(After)
Suppose we want to insert new node after some data value of the linked list then this procedure is suitable to
follow.
Algorithm for Insert at Specific(After) • In Step 5, we take a pointer variable PTR and initialize it with
START.
• That is, PTR now points to the first node of the linked list.
• Then we take another pointer variable PREPTR which will be used
to store the address of the node preceding PTR.
• Initially, PREPTR is initialized to PTR. So now, PTR, PREPTR, and
START are all pointing to the first node of the linked list.
• In the while loop, we traverse through the linked list to reach the
node that has its value equal to NUM.
• We need to reach this node because the new node will be inserted
after this node.
• Once we reach this node, in Steps 10 and 11, we change the NEXT
pointers in such a way that new node is inserted after the desired
node.
Internal
Example of Insert at Specific(After)

Internal
Program to Insert at Specific(After)

Internal
Operations on Linked List (Conti.)
3.4 Insert at specific(Before)
Suppose we want to insert new node before to some data value of the linked list then this procedure is suitable
to follow.
Algorithm for Insert at Specific(Before) • In Step 5, we take a pointer variable PTR and initialize it with
START.
• That is, PTR now points to the first node of the linked list.
• Then, we take another pointer variable PREPTR and initialize it
with PTR.
• So now, PTR, PREPTR, and START are all pointing to the first node
of the linked list.
• In the while loop, we traverse through the linked list to reach the
node that has its value equal to NUM.
• We need to reach this node because the new node will be inserted
before this node.
• Once we reach this node, in Steps 10 and 11, we change the NEXT
pointers in such a way that the new node is
• inserted before the desired node.
Internal
Example of Insert at Specific(Before)

Internal
Program to Insert at Specific(Before)

Try yourself

Internal
Operations on Linked List (Conti.)
Letʼs discuss how a node can be deleted from a linked listed in the following cases.

4.1 Delete the First Node


When we want to delete a node from the beginning of the list.

Algorithm for Delete the First Node • In Step 1, we check if the linked list exists or not. If START = NULL,
then it signifies that there are no nodes in the list and the control
is transferred to the last statement of the algorithm.
• However, if there are nodes in the linked list, then we use a
pointer variable PTR that is set to point to the first node of the list.
• For this, we initialize PTR with START that stores the address of the
first node of the list.
• In Step 3, START is made to point to the next node in sequence and
finally the memory occupied by the node pointed by PTR (initially
the first node of the list) is freed and returned to the free pool.

Internal
Example of Delete the First Node

Internal
Program to Delete the First Node

Internal
Operations on Linked List (Conti.)
Letʼs discuss how a node can be deleted from a linked listed in the following cases.

4.2 Delete the Last Node


When we want to delete a node at the end of the list.

Algorithm for Delete the Last Node • In Step 2, we take a pointer variable PTR and initialize it with
START.
• That is, PTR now points to the first node of the linked list. In the
while loop, we take another pointer variable PREPTR such that it
always points to one node before the PTR.
• Once we reach the last node and the second last node, we set the
NEXT pointer of the second last node to NULL, so that it now
becomes the (new) last node of the linked list.
• The memory of the previous last node is freed and returned back
to the free pool.

Internal
Example of Delete the Last Node

Internal
Program to Delete the Last Node

Internal
Operations on Linked List (Conti.)
Letʼs discuss how a node can be deleted from a linked listed in the following cases.

4.3 Delete Node After a Given Node


Suppose we want to delete the node that succeeds the node which contains data value 4.

Algorithm for Delete Node After a Given


• In Step 2, we take a pointer variable PTR and initialize it with
Node START.
• That is, PTR now points to the first node of the linked list. In the
while loop, we take another pointer variable PREPTR such that it
always points to one node before the PTR.
• Once we reach the node containing VAL and the node succeeding
it, we set the next pointer of the node containing VAL to the
address contained in next field of the node succeeding it.
• The memory of the node succeeding the given node is freed and
returned back to the free pool.

Internal
Example of Delete Node After a Given Node

Internal
Program to Delete Node After a Given Node

Internal
Operations on Linked List (Conti.)
5. Search the Linked List node
• Searching a linked list means to find a particular element in the linked list.
• As already discussed, a linked list consists of nodes which are divided into two parts, the information part and the
next part.
• So searching means finding whether a given value is present in the information part of the node or not.
• If it is present, the algorithm returns the address of the node that contains the value.

Algorithm for Searching of Nodes

Internal
Time Complexity of Linked List Operation

Internal
Applications of Singly Linked List
• Dynamic memory allocation
• Implemented in stack and queue
• In undo functionality of software's
• Hash tables, Graphs

Internal
Difference between Singly Linked list and
Doubly Linked list.

Internal
Code for Singly Linked List Operations
https://www.thecrazyprogrammer.com/2013/09/menu-driven-c-progr
am-to-perform-insert.html

Internal
DOUBLY LINKED LIST AND THEIR OPERATIONS

Internal
What is Doubly Linked List?
• A doubly linked list or a two-way linked list is a more complex type of linked list which contains a
pointer to the next as well as the previous node in the sequence.
• Therefore, it consists of three parts—data, a pointer to the next node, and a pointer to the
previous node.

Internal
Somehow look like DLL

Internal
Doubly Linked List at a glance
• We see that a doubly linked list calls for more space per node and
more expensive basic operations.
• However, a doubly linked list provides the ease to manipulate the
elements of the list as it maintains pointers to nodes in both the
directions (forward and backward).
• The main advantage of using a doubly linked list is that it makes
searching twice as efficient.

Internal
How can we declare Doubly Linked List?

In the mentioned declaration, we have defined


a structure named as a node consisting of three
variables: first one is pointer (prev), then an
integer variable (data), and the last one is the
pointer (next) which contains the address of
the next node.

Internal
How to create a node for DLL

Either head or start, both are same.

Internal
Operations on Doubly Linked List
1. Create DLL- Create the multiple nodes as per user requirements
2. Insertion - adds a new element to the linked list
(i) Insert at beginning
(ii) Insert at end
(iii) Insert at specific location
3. Deletion - Removes the existing elements
(i) Delete the first node
(ii) Delete the last node
(iii) Delete after a given node

Internal
Operations on Doubly Linked List (Conti.)
1.Creating a Doubly Linked List
Initially we need to create a linked list

Internal
Operations on Doubly Linked List (Conti.)
We will see how a new node can be added to an existing linked list in the following cases.

2.1 Insert at beginning


Suppose we want to add a new node with data 9 as the first node of the list.

Algorithm for Insert at beginning • In Step 1, we first check whether memory is available for the new
node.
• If the free memory has exhausted, then an OVERFLOW message is
printed.
• Otherwise, if free memory cell is available, then we allocate space
for the new node.
• Set its DATA part with the given VAL and the NEXT part is initialized
with the address of the first node of the list, which is stored in
START.
• Now, since the new node is added as the first node of the list, it
will now be known as the START node, that is, the START pointer
variable will now hold the address of NEW_NODE.

Internal
Example of Insert at beginning

Internal
Program to Insert at Beginning

Internal
Operations on Doubly Linked List (Conti.)
2.2 Insert at ending
Suppose we want to insert new node at the end of the linked list then this procedure is suitable to follow.

Algorithm for Insert at Ending • In Step 6, we take a pointer variable PTR and initialize it with
START.
• In the while loop, we traverse through the linked list to reach the
last node.
• Once we reach the last node, in Step 9, we change the NEXT
pointer of the last node to store the address of the new node.
• Remember that the NEXT field of the new node contains NULL
which signifies the end of the linked list.
• The PREV field of the NEW_NODE will be set so that it points to
the node pointed by PTR (now the second last node of the list).

Internal
Example of Insert at Ending

Internal
Program to Insert at Ending

Internal
Operations on Doubly Linked List (Conti.)
2.3 Insert at specific(After)
Suppose we want to insert new node after some data value of the linked list then this procedure is suitable to
follow.
Algorithm for Insert at specific(After) • In Step 5, we take a pointer PTR and initialize it with START.
• That is, PTR now points to the first node of the linked list.
• In the while loop, we traverse through the linked list to reach the
node that has its value equal to NUM.
• We need to reach this node because the new node will be inserted
after this node.
• Once we reach this node, we change the NEXT and PREV fields in
such a way that the new node is inserted after the desired node.

Internal
Example of Insert at Specific(After)

Internal
Program to Insert at Specific (After)

Internal
Operations on Doubly Linked List (Conti.)
2.4 Insert at specific(Before)
Suppose we want to insert new node Before some data value of the linked list then this procedure is suitable to
follow.

Algorithm for Insert at specific(Before) • In Step 5, we take a pointer variable PTR and initialize it with
START.
• That is, PTR now points to the first node of the linked list.
• In the while loop, we traverse through the linked list to reach the
node that has its value equal to NUM.
• We need to reach this node because the new node will be inserted
before this node.
• Once we reach this node, we change the NEXT and PREV fields in
such a way that the new node is inserted before the desired node.

Internal
Example of Insert at Specific(Before)

Internal
Program to Insert at Specific (Before)

Try yourself

Internal
Operations on Linked List (Conti.)
Letʼs discuss how a node can be deleted from a linked listed in the following cases.

3.1 Delete the First Node


When we want to delete a node from the beginning of the list.

Algorithm for Delete the First Node • In Step 1 of the algorithm, we check if the linked list exists or not.
• If START = NULL, then it signifies that there are no nodes in the list
and the control is transferred to the last statement of the
algorithm.
• However, if there are nodes in the linked list, then we use a
temporary pointer variable PTR that is set to point to the first node
of the list.
• For this, we initialize PTR with START that stores the address of the
first node of the list.
• In Step 3, START is made to point to the next node in sequence and
finally the memory occupied by PTR (initially the first node of the
list) is freed and returned to the free pool.

Internal
Example of Delete the First Node

Internal
Program to Delete the First Node

Internal
Operations on Linked List (Conti.)
3.2 Delete the Last Node
When we want to delete a node at the end of the list.

Algorithm for Delete the Last Node • In Step 2, we take a pointer variable PTR and initialize it with
START.
• That is, PTR now points to the first node of the linked list.
• The while loop traverses through the list to reach the last node.
Once we reach the last node, we can also access the second last
node by taking its address from the PREV field of the last node.
• To delete the last node, we simply have to set the next field of
second last node to NULL, so that it now becomes the (new) last
node of the linked list.
• The memory of the previous last node is freed and returned to the
free pool.

Internal
Example of Delete the Last Node

Internal
Program to Delete the Last Node

Internal
Operations on Linked List (Conti.)
3.3 Delete Node After a Given Node
Suppose we want to delete the node that succeeds the node which contains data value 4

Algorithm for Delete Node After a Given Node


• In Step 2, we take a pointer variable PTR and initialize it with
START.
• That is, PTR now points to the first node of the doubly linked list.
• The while loop traverses through the linked list to reach the given
node.
• Once we reach the node containing VAL, the node succeeding it
can be easily accessed by using the address stored in its NEXT field.
• The NEXT field of the given node is set to contain the contents in
the NEXT field of the succeeding node.
• Finally, the memory of the node succeeding the given node is
freed and returned to the free pool.

Internal
Operations on Linked List (Conti.)
3.3 Delete Node After a Given Node
Suppose we want to delete the node that succeeds the node which contains data value 4

Algorithm for Delete Node After a Given Node


• In Step 2, we take a pointer variable PTR and initialize it with
START.
• That is, PTR now points to the first node of the doubly linked list.
• The while loop traverses through the linked list to reach the given
node.
• Once we reach the node containing VAL, the node succeeding it
can be easily accessed by using the address stored in its NEXT field.
• The NEXT field of the given node is set to contain the contents in
the NEXT field of the succeeding node.
• Finally, the memory of the node succeeding the given node is
freed and returned to the free pool.

Internal
Example of Delete Node After a Given Node

Internal
Program to Delete Node After a Given Node

Internal
Time Complexity of Doubly Linked List
Operation

Internal
Applications of Doubly Linked List
• Doubly linked list can be used in navigation systems where both
forward and backward traversal is required.
• It can be used to implement different tree data structures.
• It can be used to implement undo/redo operations.

Internal
Code for Doubly Linked List Operations
https://www.javatpoint.com/doubly-linked-list

Internal
CIRCULAR LINKED LIST AND THEIR OPERATIONS

Internal
What is Circular Linked List?
• In a circular linked list, the last node contains a pointer to the first node of the list.
• We can have a circular singly linked list as well as a circular doubly linked list.
• While traversing a circular linked list, we can begin at any node and traverse the list in any
direction, forward or backward, until we reach the same node where we started.
• Thus, a circular linked list has no beginning and no ending.

Internal
Somehow look like CLL

Internal
How can we declare Circular Linked List?

Internal
Operations on Circular Linked List
1. Create LL- Create the multiple nodes as per user requirements
2. Insertion - adds a new element to the linked list
(i) Insert at beginning
(ii) Insert at end
3. Deletion - Removes the existing elements
(i) Delete the first node
(ii) Delete the last node

Internal
Operations on Circular Linked List (Conti.)
1.Creating a Circular Linked List
Initially we need to create a linked list

Internal
Operations on Circular Linked List (Conti.)
2.1 Insert at beginning
Suppose we want to add a new node with data 9 as the first node of the list.

Algorithm for Insert at beginning


• In Step 1, we first check whether memory is available for the new
node.
• If the free memory has exhausted, then an OVERFLOW message is
printed.
• Otherwise, if free memory cell is available, then we allocate space
for the new node.
• While inserting a node in a circular linked list, we have to use a
while loop to traverse to the last node of the list.
• Because the last node contains a pointer to START, its NEXT field is
updated so that after insertion it points to the new node which
will be now known as START.

Internal
Example of Insert at beginning

Internal
Program to Insert at Beginning

Internal
Operations on Circular Linked List (Conti.)
2.2 Insert at ending
Suppose we want to insert new node at the end of the linked list then this procedure is suitable to follow.

Algorithm for Insert at ending


• In Step 6, we take a pointer variable PTR and initialize it with
START.
• That is, PTR now points to the first node of the linked list. In the
while loop, we traverse through the linked list to reach the last
node.
• Once we reach the last node, in Step 9, we change the NEXT
pointer of the last node to store the address of the new node.
• Remember that the NEXT field of the new node contains the
address of the first node which is denoted by START.

Internal
Example of Insert at ending

Internal
Program to Insert at ending

Internal
Operations on Circular Linked List (Conti.)
3.1 Delete the First Node
When we want to delete a node from the beginning of the list.

Algorithm for Delete the First Node


• In Step 1 of the algorithm, we check if the linked list exists or not.
If START = NULL, then it signifies that there are no nodes in the list
and the control is transferred to the last statement of the
algorithm.
• However, if there are nodes in the linked list, then we use a
pointer variable PTR which will be used to traverse the list to
ultimately reach the last node.
• In Step 5, we change the next pointer of the last node to point to
the second node of the circular linked list.
• In Step 6, the memory occupied by the first node is freed.
• Finally, in Step 7, the second node now becomes the first node of
the list and its address is stored in the pointer variable START.

Internal
Example of Delete the First Node

Internal
Program to Delete the First Node

Internal
Operations on Circular Linked List (Conti.)
3.2 Delete the Last Node
When we want to delete a node at the end of the list.

Algorithm for Delete the Last Node


• In Step 2, we take a pointer variable PTR and initialize it with
START.
• That is, PTR now points to the first node of the linked list.
• In the while loop, we take another pointer variable PREPTR such
that PREPTR always points to one node before PTR.
• Once we reach the last node and the second last node, we set the
next pointer of the second last node to START, so that it now
becomes the (new) last node of the linked list.
• The memory of the previous last node is freed and returned to the
free pool.

Internal
Example of Delete the Last Node

Internal
Program to Delete the Last Node

Internal
Time Complexity of Circular Linked List
Operation

Internal
Applications of Circular Linked List
• Circular Linked Lists can be used to manage the computing resources of the computer.
• Data structures such as stacks and queues are implemented with the help of the circular linked
lists.
• Circular Linked List is also used in the implementation of advanced data structures such as a
Fibonacci Heap.
• It is also used in computer networking for token scheduling.
• Round Robin scheduling technique in games.
• Audio/Video Streaming
• Circular Escalators

Internal
Code for Circular Linked List Operations
https://www.javatpoint.com/circular-singly-linked-list

Internal

You might also like