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

Data Structure Unit 3

Uploaded by

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

Data Structure Unit 3

Uploaded by

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

DATA

STRU CTURES
Unlocking the Power of Data: Your Journey into Data Structures

By Pranav Zambre
MEET YOUR INSTRUCTOR

Hi! I’m Pranav Zambre

Completed My B.tech From Parul


University.
Gate Exam: AIR 4125.
Pursing My M.tech From IIT Patna
Area Of Expertise : Data Structure,
Algorithm, And Blockchain
OVERVIEW
UNIT - 3

Introduction to Singly Linked List Deletion Operations

Representation in Memory Searching a Node in a Linked List

Operations on a Single Linked List Counting the Number of Nodes in a


Linked List
Insertion Operation
INTRODUCTION TO LINKED LIST

➤ A linked list is a linear data structure where elements are not


stored in contiguous memory locations. Instead, each element
(node) points to the next element, forming a chain.

Node: Each element in a linked list is called a node. It contains


two parts:
Data: The actual information stored in the node.
Pointer: A reference to the next node in the sequence.

➤ Real-life analogy: Think of a linked list like a train. Each carriage


(node) is connected to the next one by a coupling. You can start
from any carriage and follow the chain to reach the desired
destination.
EXAMPLE OF LINKED LIST

Each train route has a starting station, followed by


intermediate stations, and finally, the destination
station.

Each station (node) contains information about the


next station in the sequence, forming a chain of
stations along the route.

Just like in a linked list, you can traverse the train


schedule by following the pointers from one station
to the next.
REPRESENTATION OF LINKED LIST

Node Structure: A node in a linked list typically consists of two components:


Data: It holds the actual value or data associated with the node.
Next Pointer: It stores the memory address (reference) of the next node in the
sequence.
TYPES OF LINKED LIST

➤ In Linked list, elements are not stored at a contiguous


location, rather they are linked using pointers. linked List
Single-linked list
forms a series of connected nodes, where each node
stores the data and the address of the next node.
Double linked list

➤ There are mainly three types of linked lists:


Circular linked list
Single-linked list

Double linked list

Circular linked list


SINGLE LINKED LIST
➤ Singly Linked Lists are a type of data structure where each element (node) points to the
next element in the sequence.

➤ Unlike arrays, the elements are not stored in contiguous memory locations. Instead,
they are connected via pointers.

➤ Traversals can be done in one direction only as there is only a single link between two
nodes of the same list.
SINGLE LINKED LIST
Retrieving Nodes
➤ In a single linked list, you can easily find and get a specific piece of data. It's like looking
for a book in a library; you can start from the beginning, go through the pages one by one,
or jump directly to the section you need.

Adding Nodes
➤ When you want to put something new in the list, you have options. It's like adding new
friends to your contact list on your phone. You can put them at the start, at the end, or
squeeze them in wherever you want.

Removing Nodes
➤ Just like deleting unwanted apps from your phone, removing items from a linked list is
straightforward. You can get rid of them from the front, from the back, or pluck them out
from anywhere in between.
APPLICATION OF LINKED LIST
Employee Management System

➤ In an employee management system, single linked lists can be used to represent the
list of employees in a company. Each employee's information, such as their name,
employee ID, position, and salary, can be stored in a node of the linked list.
REPRESENTATION OF A LINKED
LIST IN MEMORY
There are two ways to represent a linked list in memory:

Static representation using array


Dynamic representation using free pool of storage

➤ Static representation : In static representation of a


single linked list, two arrays are maintained: one array for
data and the other for links
OPERATIONS ON A SINGLY LINKED LIST

The following operations are performed on a Single


Linked List

Insertion
Deletion
Search
Display
INSERTION IN SINGLY LINKED LIST
Insertion into a singly linked list can be performed at
different positions. This operation involves adding a new
node containing certain data into the list. The new node
typically has an empty data field and an empty next field
initially.

Based on the position of the new node being inserted, the


insertion is categorized into the following categories.

Inserting a node at the beginning


Inserting a node at the end
Inserting a node at intermediate position
AVAIL POINTER
Computer Maintains a List Of all Free memory cells. The list of available space is
called the free pool.

Likewise, For the free pool, we have a pointer AVAIL which stores the address of
the first free space.
INSERTING A NODE AT THE BEGINNING

In a linked list, each element (node) contains a data part and a reference (or link) to the
next node in the sequence. When inserting a node at the beginning of a linked list, the
new node becomes the first node, and its link points to the former first node.

Real-Life Example : Imagine you are organizing a playlist of songs where each song is
connected to the next one in a sequence (like a linked list). When you want to add a new
song to the beginning of the playlist.
ALGORITHM
INSERT_FIRST(START,ITEM)

Step 1: [Check For Overflow]

IF PTR = Null then


Print Overflow
Exit

Else
PTR = (Node*) malloc (size of (Node))
// create new node from memory and assign its address to PTR
ALGORITHM
Step 2: Set PTR -> INFO = ITEM

Step 3: Set PTR -> Next = START

Step 4: Set START = PTR


REPRESENTATION OF INSERTING A NODE
AT THE BEGINNING
Creating a New Node:

Allocate memory for the new node.


Initialize the node with the given data value.

Link the New Node:

Set the next pointer of the new node to point to the current head node of the list.

Update the Head Pointer:


Make the head pointer point to the new node.
The new node becomes the first node in the list.
REPRESENTATION OF INSERTING A NODE
AT THE BEGINNING
INSERTING A NODE AT THE END OF
LINKED LIST
When inserting a node at the end of a linked list, the new node is added after the
last node, and the link of the former last node points to the new node.

Imagine you are organizing a playlist of songs where each song is connected to
the next one in a sequence (like a linked list). When you want to add a new song
to the end of the playlist, you simply add it after the last song. The new song's
link points to NULL, indicating it is now the last song in the playlist.
ALGORITHM
INSERT_FIRST(START,ITEM)

Step 1: [Check For Overflow]

IF PTR = Null then


Print Overflow
Exit

Else
PTR = (Node*) malloc (size of (Node))
// create new node from memory and assign its address to PTR
ALGORITHM
Step 2: Set PTR -> Info = Item
Step 3: Set PTR -> Next = NULL
Step 4: IF Start = NULL and then set Start = PTR;
Else
Step 5: Set Loc = Start
Step 6: Repeat Step 7 Until Loc -> Next != Null
Step 7: Set Loc = Loc -> Next
Step 8: Set Loc -> Next = Ptr
ALGORITHM
REPRESENTATION OF INSERTING A NODE
AT THE END
Create a New Node:
Allocate memory for the new node.
Initialize the new node with the given data.

Traverse to the End:


Start from the head node.
Move through each node until reaching the last node (where the next pointer is
NULL).

Link the New Node:


Set the next pointer of the last node to point to the new node.
Set the next pointer of the new node to NULL.
REPRESENTATION OF INSERTING A NODE
AT THE END
INSERT A NODE AT SPECIFIC
POSITION
This operation involves adding a new node at a particular position in a linked
list. It requires updating the pointers of adjacent nodes to ensure the new node
is correctly placed without breaking the sequence of the list.

Example: You have a playlist with the following songs in order: Song1, Song2, and
Song3. Now, you want to add a new song, "NewSong," at position 2. This means
"NewSong" should come after Song1 but before Song2.
ALGORITHM
Insert Location (START,ITEM,LOC)

Step 1: [Check For Overflow]

IF PTR = Null then


Print Overflow
Exit

Else
PTR = (Node*) malloc (size of (Node))
// create new node from memory and assign its address to PTR
ALGORITHM
Step 2 : Set PTR -> INFO = Item
Step 3 : If Start = NULL Then
Set Start = PTR
Set PTR -> Next = Null
Step 4 : Initialize The Counter I and Pointer
Set I = 0
Set temp = Start
Step 5 : Repeat Steps 6 and 7 Until I < Loc
Step 6 : Set temp = temp -> Next
Step 7 : Set I = I+1
Step 8 : Set PTR -> Next = Temp -> Next
Step 9 : Set temp -> Next = PTR
ALGORITHM
REPRESENTATION OF INSERT A NODE AT
SPECIFIC POSITION
Identify the Position:
Determine the position where the new node is to be inserted, denoted as pos.

Create the New Node:


Allocate memory for the new node.
Initialize the new node with the given data.

Traverse to the Desired Position:


Start from the head node.
Move through each node until reaching the node at position pos-1. Let's call this
node prev_node.
REPRESENTATION OF INSERT A NODE AT
SPECIFIC POSITION
Link the New Node:

Set the next pointer of the new node to point to the node currently at
position pos.
Set the next pointer of prev_node to point to the new node.
REPRESENTATION OF INSERT A NODE AT
SPECIFIC POSITION
INSERT A NODE IN SORTED LINKED LIST
This operation involves placing a new node into a linked list such that the order
of nodes, based on a specified key (like a numerical value or a string), is
maintained after insertion. The process ensures that the linked list remains
sorted without needing to sort the entire list again.

Example: Imagine you have a list of students' names in alphabetical order in a


school attendance record. You need to insert a new student's name into this list
while maintaining alphabetical order.
ALGORITHM
// Step 1: Create a temporary pointer 'temp' and initialize it to 'head'
temp = head

// Step 2: Retrieve the data value 'key' from the node to be inserted ('newP')
key = newP->data

// Step 3: Traverse the linked list to find the insertion point


While temp->link != NULL AND temp->link->data < key
temp = temp->link

// Step 4: Insert the new node ('newP') into the linked list
newP->link = temp->link
temp->link = newP
ALGORITHM
REPRESENTATION OF INSERT A NODE AT
SPECIFIC POSITION
Start at the Beginning
Begin with a pointer (temp) set to the head of the linked list.

Retrieve the Data to Insert


Retrieve the data (key) from the new node (newP) that needs to be inserted into
the sorted list.
Traverse the List
Traverse through the linked list using temp until you find the correct position to
insert newP.
Compare key with the data of each node (temp->data or temp->link->data
depending on the condition) to determine the insertion point.
REPRESENTATION OF INSERT A NODE AT
SPECIFIC POSITION
Insert the Node

Once the correct position (temp) is found:


Link newP to the rest of the list by adjusting pointers:
Set newP->link to point to temp->link.
Update temp->link to point to newP.
REPRESENTATION OF INSERT A NODE AT
SPECIFIC POSITION
DELETE A NODE FROM THE
STARTING OF LINKED LIST
Deleting a node from the starting of a linked list means removing the
node located at the head of the linked list structure. This operation
involves adjusting the pointers or references to ensure that the list
remains connected after the deletion.
ALGORITHM
Deleted First (START)

Step 1: Check For Under Flow


If Start = Null, Then
Print Linked List Empty
Exit

Step 2: Set PTR=START


Step 3: Set START = START->NEXT
Step 4: Print Element deleted is ptr->Info
Step 5: free(ptr)
ALGORITHM
REPRESENTATION OF DELETE A NODE
FROM THE STARTING OF LINKED LIST
Step 1: Check for Underflow
Check if the linked list is empty.
If START is NULL, print "Linked List Empty" and exit.

Step 2: Set PTR = START


Initialize a pointer PTR to point to the current head of the linked list.

Step 3: Update START


Move START to point to the next node in the linked list (START = START->NEXT).
This removes the current head node from the list.
REPRESENTATION OF DELETE A NODE
FROM THE STARTING OF LINKED LIST
Step 4: Print Deleted Element
Print the element that was deleted using PTR->Info.
This displays the information stored in the node that was removed from
the start of the linked list.

Step 5: Free Memory


Free the memory allocated to the node pointed to by PTR using free(PTR).
This step ensures that the memory used by the deleted node is released,
preventing memory leaks.
REPRESENTATION OF DELETE A NODE
FROM THE STARTING OF LINKED LIST
DELETE A NODE FROM THE END
OF THE LIST
Deleting a node from the end of a linked list means removing the node
that is at the tail position, or the last node in the list. This operation
typically requires traversing the list to find the second-to-last node
(penultimate node) so that its next pointer can be adjusted to NULL,
effectively removing the last node.
ALGORITHM
Step 1: Check For Underflow
If Start=Null then
Print Link List Is Empty
Exit

Step 2: If start->Next = Null then


Set Ptr = Start
Set Start = Null
Print element deleted is Ptr -> Info
Free (Ptr)
ALGORITHM
end if

Step 3: Set PTR = Start


Step 4: Repeat Step 5 and 6 untill
Ptr->Next != Null

Step 5: Set Loc = PTR


Step 6: Set PTR = PTR -> Next
Step 7: Set Loc->Next = Null
Step 8: Free(PTR)
ALGORITHM
REPRESENTATION OF DELETE A NODE
FROM THE END OF THE LIST
Step 1: Check for Underflow
Check if the linked list is empty (Start = NULL).
If true, print "Linked List is Empty" and exit.

Step 2: Check if Only One Node Exists


Check if there is only one node in the linked list (Start->Next = NULL).
If true:
Set Ptr = Start.
Set Start = NULL.
Print "Element deleted is Ptr->Info".
Free memory for Ptr.
REPRESENTATION OF DELETE A NODE
FROM THE END OF THE LIST
Step 4: Traverse to Second-to-Last Node
Repeat steps 5 to 8 until PTR->Next != NULL.

Step 5: Set Loc to Current Node


Set Loc = PTR.

Step 6: Move PTR to Next Node


Set PTR = PTR->Next.
Step 7: Disconnect the Last Node
Set Loc->Next = NULL.
Step 8: Free Memory for Last Node
REPRESENTATION OF DELETE A NODE
FROM THE END OF THE LIST
SEARCH A NODE IN THE LINKED LIST
Searching a node in a linked list refers to the process of locating a specific
element (or node) within a sequence of connected nodes. Each node
contains data and a reference (or pointer) to the next node in the
sequence. The search operation typically involves traversing through the
linked list starting from the beginning (or any designated starting point)
until the desired element is found or until the end of the list is reached.
ALGORITHM
Step 1: Check if the linked list is empty:
If START = Null,
Print "Linked List Empty"
Exit

Step 2: Initialize PTR with START:


Set PTR = START

Step 3: Input the value to be searched (Key):


Read Key
ALGORITHM
Step 4: Traverse the linked list:
While PTR is not Null and PTR->INFO ≠ Key,
Set PTR = PTR->NEXT

Step 5: Check if the element was found or not:


If PTR = Null,
Print "Element not found"
Else,
Print "Element found"
ALGORITHM
REPRESENTATION OF DELETE A NODE
FROM THE END OF THE LIST
Step 1: Check if the linked list is empty. If it is, there are no nodes to search, so we
print a message and exit.

Step 2: Initialize PTR to point to the first node (START) of the linked list.

Step 3: Input the value (Key) that you want to search for in the linked list.

Step 4: Traverse through the linked list using a loop. Inside the loop:
Check if PTR is not Null (to prevent accessing null pointer) and if PTR's INFO (the
data stored in the current node) is not equal to the Key.
REPRESENTATION OF DELETE A NODE
FROM THE END OF THE LIST
Move PTR to point to the next node in the linked list.

Step 5: After exiting the loop, check the status of PTR:


If PTR is Null, it means the Key was not found in the linked list, so
print "Element not found".
Otherwise, print "Element found", indicating that the Key was found
somewhere in the linked list.
REPRESENTATION OF SEARCH A NODE IN
THE LINKED LIST
COUNT THE NUMBER OF NODES IN
THE LINKED LIST
Counting the number of nodes in a linked list refers to the process of
determining how many individual elements (nodes) are present in the
linked list structure. Each node contains data and a reference (or pointer)
to the next node in the sequence. The count specifically includes all nodes
that contain data and excludes the terminating null pointer that signifies
the end of the list.
ALGORITHM
Step 1: Initialize a counter:
Set Count = 0

Step 2: Check if the linked list is empty:


If START = Null,
Print "Linked List Empty"
Exit
ALGORITHM
Step 3: Traverse the linked list to count nodes:
Set PTR = START
While PTR is not Null,
Increment Count by 1
Set PTR = PTR->NEXT

Step 4: Print the total number of nodes:


Print "Total number of nodes in the linked list: Count"
ALGORITHM
REPRESENTATION OF COUNT THE
NUMBER OF NODES IN THE LINKED LIST
Step 1: Check if the linked list is empty:
Verify if the START pointer is Null.
If START is Null, print "Linked List Empty".
Exit the process since there are no nodes to count.

Step 2: Initialize a counter:


Set up a variable Count and initialize it to 0.
This counter will keep track of the number of nodes in the linked list.
REPRESENTATION OF COUNT THE
NUMBER OF NODES IN THE LINKED LIST
Step 3: Traverse the linked list to count nodes:
Start with a pointer (PTR) set to START, which points to the first node of
the list.
Use a loop to iterate through each node:
Increment the Count variable by 1 for each node visited.
Move the PTR to the next node (PTR = PTR->NEXT) until PTR becomes
Null, indicating the end of the list.
REPRESENTATION OF COUNT THE
NUMBER OF NODES IN THE LINKED LIST
Step 4: Print the total number of nodes:
After exiting the loop, Count will hold the total number of nodes in
the linked list.
Print the value of Count to display the total count of nodes found in
the linked list.
REPRESENTATION OF SEARCH A NODE IN
THE LINKED LIST
DOUBLY LINKED LISTS
A Doubly Linked List is a data structure where
each node contains data and two pointers: one
pointing to the next node and another pointing
to the previous node. This allows traversal in
both forward and backward directions.

Data: The value or information stored in the


node.

Previous Pointer: A reference to the previous


node in the sequence (or null if it is the first
node).

Next Pointer: A reference to the next node in


the sequence (or null if it is the last node).
REAL-LIFE EXAMPLE OF A DOUBLY
LINKED LIST
Forward Link (Next Song): In a music playlist,
pressing "Next" moves to the next song, similar to
the forward pointer in a doubly linked list.

Backward Link (Previous Song): Pressing


"Previous" takes you back to the previous song,
like the backward pointer in a doubly linked list.

Adding/Removing Songs: When you add or


remove a song, the app updates the links
between songs, just like inserting or deleting
nodes in a doubly linked list.
DOUBLY LINKED LISTS DIAGRAM
ADVANTAGES OF DISADVANTAGES OF
DOUBLY LINKED LIST DOUBLY LINKED LIST

In singly linked list we can traverse in only Insertion and deletion take more time
one direction (forward). While, doubly compared to singly linked list.
linked list allows to traverse in both the
directions i.e. both forward and backward. Increased Memory Usage: Requires more
memory due to two pointers per node.
Insertion and deletion operations are more
efficient.

Program execution time is reduced.

Deletion is faster compared to singly linked


list due to two way traversal.
SINGLY LINK LIST DOUBLY LINKED LIST

Can only be traversed in one Can be traversed in both directions


direction (forward). (forward and backward).

Requires less memory as each Requires more memory as each node


node has one pointer. has two pointers (next and
previous).
Easier to implement with simpler
operations. More complex to implement due to
managing two pointers.
Insertion and deletion are less
efficient. Insertion and deletion are more
efficient.
INSERTION OF NEW NODE AT THE BEGINNING OF
THE DOUBLY LINKED LIST

Insertion of a new node at the beginning of a doubly linked list means adding a new
item to the very start of the list. A doubly linked list is like a chain where each link
(node) has a value and connections to both the next and previous links.
STEPS INVOLVED IN THIS INSERTION
Create the New Node: Allocate space for the new node, set its data, and initialize
its pointers. The new node’s Next pointer is set to the current head of the list
(making it point to the former first node), and its Prev pointer is set to NULL
(since it will be the new head).
Update Pointers: Adjust the pointers of the new node and the current head
node. If the list is empty, the new node becomes both the head and the only
node in the list. If the list is not empty, update the Prev pointer of the current
head node to point to the new node and then set the list's head pointer to the
new node.
Change Head Pointer: Set the list's head pointer to the new node, making it the
new starting point of the list.
ALGORITHM
// Step 1: Check for Memory Allocation // Step 3: Update the current head
PTR = malloc(size of(Node)) node if it exists
if PTR is null: if START is not null:
print("Overflow") START -> Prev = PTR
exit
// Step 4: Update the head pointer
// Step 2: Set node information START = PTR
PTR -> Info = ITEM
PTR -> Next = START
PTR -> Prev = null
INSERTION OF NEW NODE AT THE
BEGINNING OF THE DOUBLY LINKED LIST
DIAGRAM
INSERTION OF NEW NODE AT THE
END OF THE DOUBLY LINKED LIST
Inserting a new node at the end of a doubly linked list means adding a new item to
the last position in the list. A doubly linked list is like a chain where each node (link)
has a value and connections to both the next and previous nodes.
STEPS INVOLVED IN THIS INSERTION
Create the New Node: Allocate space for the new node, set its data, and initialize
its pointers. The new node’s Next pointer is set to NULL because it will be the
last node in the list, and its Prev pointer is initially set to NULL (which will be
updated later if the list is not empty).
Update Pointers: Adjust the pointers of the new node and the current last node.
If the list is empty, the new node becomes both the head and the tail. If the list
is not empty, traverse to the end of the list, update the Next pointer of the
current last node to point to the new node, and set the Prev pointer of the new
node to the current last node.
Change Tail Pointer (if applicable): If a tail pointer is maintained in the list
implementation (pointing to the last node), update it to reference the new node.
ALGORITHM
// Step 1: Create the New Node START = PTR
PTR = malloc(size of(Node)) TAIL = PTR
if PTR is null: else:
print("Overflow") // List is not empty
exit LAST = START
PTR -> Info = ITEM while LAST -> Next is not null:
PTR -> Next = null LAST = LAST -> Next
PTR -> Prev = null
LAST -> Next = PTR
// Step 2: Update Pointers PTR -> Prev = LAST
if START is null: TAIL = PTR
// List is empty
INSERTION OF NEW NODE AT THE END OF
THE DOUBLY LINKED LIST DIAGRAM
INSERTION OF NEW NODE AT THE SPECIFIC
POSITION OF THE DOUBLY LINKED LIST
Insertion of a new node at a specific position in a doubly linked list involves adding
a new node at a particular location within the list. In a doubly linked list, each node
contains a value and two pointers: one pointing to the next node and another
pointing to the previous node.
STEPS INVOLVED IN THIS INSERTION
Create the New Node: Allocate space for the new node, set its data, and initialize
its pointers. The new node’s Next pointer will be set to point to the node
currently at the specified position, and its Prev pointer will be set to point to the
node immediately before the specified position (these will be updated later).
Update Pointers: Traverse the list to find the node currently at the specified
position and the node just before it. Adjust the pointers of the surrounding
nodes:
Set the Next pointer of the node before the specified position to point to the new
node.
Set the Prev pointer of the node at the specified position to point to the new node.
STEPS INVOLVED IN THIS INSERTION
Set the Prev pointer of the new node to the node before the position.
Set the Next pointer of the new node to the node at the specified position.
Maintain List Integrity: If the insertion occurs at the beginning or end of the list,
update the HEAD or TAIL pointers accordingly to reflect the new node's position
in the list.
ALGORITHM
// Step 1: Create the New Node while CURRENT is not null and INDEX
PTR = malloc(size of(Node)) < POSITION - 1:
if PTR is null: CURRENT = CURRENT -> Next
print("Overflow") INDEX = INDEX + 1
exit if POSITION == 0:
// Insert at the beginning
PTR -> Info = ITEM PTR -> Next = START
PTR -> Next = null if START is not null:
PTR -> Prev = null START -> Prev = PTR
START = PTR
// Step 2: Traverse to the Position
CURRENT = START
INDEX = 0
ALGORITHM
else if CURRENT is not null:
// Insert at a position other than the beginning
PTR -> Next = CURRENT
PTR -> Prev = CURRENT -> Prev

if CURRENT -> Prev is not null:


CURRENT -> Prev -> Next = PTR
CURRENT -> Prev = PTR
else:
print("Error: Position out of bounds")
exit
INSERTION OF NEW NODE AT THE
SPECIFIC POSITION OF THE DOUBLY
LINKED LIST DIAGRAM
DELETION OF A NODE FROM BEGINNING OF
THE DOUBLY LINKED LIST
Deletion of a node from the beginning of a doubly linked list involves removing the
first node (head) of the list. In a doubly linked list, each node has two pointers: one
pointing to the next node and another pointing to the previous node. Removing the
head node requires updating the head pointer and adjusting the pointers of the
new head node (if any).
STEPS INVOLVED IN THIS INSERTION
Identify the Node to Remove: The node at the beginning of the list (the
head node) is identified as the target for deletion.
Adjust the HEAD Pointer: Set the list's HEAD pointer to the node that was
previously the second node in the list.
Update the New Head Node: If the new head node is not NULL, update its
Prev pointer to NULL (since it is now the first node in the list).
Handle Empty List: If the list becomes empty after deletion (i.e., it had only
one node), ensure to set the HEAD pointer to NULL.
Free the Deleted Node: Deallocate the memory used by the removed node
to prevent memory leaks.
ALGORITHM
// Step 1: Check if the list is empty // Step 4: Handle Empty List
if START is null: if START is null:
print("Error: List is empty") TAIL = null
return
// Step 5: Free the Deleted Node
// Step 2: Identify the Node to free (NODE_TO_DELETE)
Remove
NODE_TO_DELETE = START
// Step 3: Update Pointers
START = NODE_TO_DELETE -> Next

if START is not null:


START -> Prev = null
DELETION OF A NODE FROM BEGINNING
OF THE DOUBLY LINKED LIST
DELETION OF A NODE FROM END OF THE
DOUBLY LINKED LIST
Deletion of a node from the end of a doubly linked list involves removing the last
node (tail) of the list. In a doubly linked list, each node contains pointers to both the
next and previous nodes, allowing bidirectional traversal. Removing the tail node
requires updating the tail pointer and adjusting the pointers of the new tail node (if
any).
STEPS INVOLVED IN THIS INSERTION
Identify the Node to Remove: Locate the node at the end of the list, known
as the tail node.
Adjust the TAIL Pointer: Set the list's TAIL pointer to the node that was
previously the second-to-last node.
Update the New Tail Node: If the new tail node is not NULL, set its Next
pointer to NULL (since it is now the last node).
Handle Empty List: If the list becomes empty after deletion (i.e., the list had
only one node), set both the TAIL and HEAD pointers to NULL.
Free the Deleted Node: Deallocate the memory used by the removed tail
node to prevent memory leaks.
ALGORITHM
Step 1: Check if the list is empty else:
if TAIL is null: // List had only one node
print("Error: List is empty") START = null
return TAIL = null

// Step 2: Identify the Node to Remove // Step 4: Free the Deleted Node
NODE_TO_DELETE = TAIL free(NODE_TO_DELETE)

// Step 3: Update Pointers


if TAIL -> Prev is not null:
TAIL = TAIL -> Prev
TAIL -> Next = null
DELETION OF A NODE FROM END OF THE
DOUBLY LINKED LIST
SEARCHING A NODE IN DOUBLY
LINKED LIST
Searching a node in a doubly linked list involves locating a node with a specific
value within the list. A doubly linked list is a data structure where each node has
pointers to both its previous and next nodes, allowing traversal in both directions.
The search operation typically starts from the head of the list and proceeds
sequentially through the nodes until the target value is found or the end of the list
is reached.
STEPS INVOLVED IN THIS INSERTION
Start at the Head: Begin the search from the head node of the list.
Traverse the List: Move through each node in the list, checking the value of
the current node.
Compare Values: Compare the value of each node with the target value.
Handle Node Found: If a node with the matching value is found, return that
node or its position.
Handle Node Not Found: If the end of the list is reached without finding the
target value, indicate that the node is not present in the list.
ALGORITHM
// Step 1: Start at the head of the list
CURRENT = START
// Step 2: Traverse the list
while CURRENT is not null:
// Step 3: Compare the current node's value with the target value
if CURRENT -> Info == VALUE:
return CURRENT
// Step 4: Move to the next node
CURRENT = CURRENT -> Next
// Step 5: Node not found
print("Node with value", VALUE, "not found in the list")
return null
SEARCHING A NODE IN DOUBLY LINKED
LIST DIAGRAM

You might also like