0% found this document useful (0 votes)
25 views17 pages

DSA m3

A doubly linked list is a complex data structure where each node contains data and pointers to both the previous and next nodes. Key operations include insertion and deletion at the front, tail, and after a specific node, as well as searching and deleting a node. The document provides detailed examples of how to implement these operations in C programming language.

Uploaded by

abhilashkotian08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views17 pages

DSA m3

A doubly linked list is a complex data structure where each node contains data and pointers to both the previous and next nodes. Key operations include insertion and deletion at the front, tail, and after a specific node, as well as searching and deleting a node. The document provides detailed examples of how to implement these operations in C programming language.

Uploaded by

abhilashkotian08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Doubly Linked List

Doubly linked list is a complex type of linked list in which each node has links to both the previous and
next node. Each node consists of three elements: one holds the data, and another two are the next and
the previous node’s pointers. These two pointers help us to go forward or backward from a particular
node.

Here’s the basic structure of the doubly linked list.

Every linked list has a head and tail node. The Head node has no “prev” (previous pointer) node, and
the tail node has no “next” node.

Defining the structure for a doubly-linked list node


struct Node
{
int data;
struct Node* next;
struct Node* prev;
};

Here’re some important terms for a doubly linked list:


• Prev: Each node is linked to its previous node. It is used as a pointer or link.
• Next: Each node is linked to its next node. It is used as a pointer or link.
• Data: This is used to store data in a node. “Data” can hold other Data Structures inside it. For
example, string, dictionary, set, hashmap, etc can be stored in the “Data”.

Here is the basic structure of a single node in the doubly linked list:

Here’s the list of operations we can implement using the doubly linked list:
• Insertion in front
• Insertion in the tail or last node
• Insertion after a node
• Deletion from front
• Deletion from tail
• Search and delete a node
• Traverse head to tail

Insertion in front of Doubly Linked List


Insertion in front means we need to create a node in the linked list and place it at the
beginning of the linked list.

For example, there’s a given node “15”. We need to add this to the head node.

Here’re two important conditions while doing this operation:

1. The new node will be the head node if there’s no node in the Doubly Linked List.
2. If there’s already a head node, the previous head will be replaced by the new node.

struct Node* insertAtFront(struct Node* head, int data)


{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = head;
newNode->prev = NULL;

if (head != NULL)
{
head->prev = newNode;
}

head = newNode;
return head;
}
Insertion at the end of Doubly Linked List
“Insertion at the end” means we will create a node in the linked list and place it at the end.

To perform this, we can use the following method: Start traversing from the head of the Doubly Linked
List until the “next” becomes null. Then link the new node with the “next”.

struct Node* insertAtTail(struct Node* head, int data)


{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;

if (head == NULL)
{
newNode->prev = NULL;
head = newNode;
}
else
{
struct Node* current = head;
while (current->next != NULL)
{
current = current->next;
}

newNode->prev = current;
current->next = newNode;
}

return head;
}
Insertion after a node
We want to insert a given node that will be linked after the node, which has the value of “12”.
Here’re the steps for this:
Step 1: Traverse from the head to the last node. Check which node has the value of “12”.
Step 2: Create a new node and assign it as the next pointer of node “12”. The “next” node of the new
node will be 15.

struct Node* insertAfter(struct Node* head, int searchItem, int data)


{
struct Node* current = head;

while (current != NULL && current->data != searchItem)


current = current->next;

if (current == NULL)
return head;

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));


newNode->data = data;

newNode->next = current->next;
newNode->prev = current;

if (current->next != NULL)
current->next->prev = newNode;

current->next = newNode;
return head;
}
Delete the head of the doubly linked list
The head node in the doubly linked list that doesn’t have any previous node. So, the next pointer will
be the new head node if we want to delete the head node. We also need to free the memory occupied
by a node while deleting a node.

Here’re the steps for deleting the head node:


Step 1: Assign a variable to the current head node.
Step 2: Visit the “next” node of the current head node and make the “prev” (previous pointer) node
as ‘’NULL”. This means we are disconnecting the second node from the first node.
Step 3: Free the occupied memory by the previous head node.

struct Node* deleteHead(struct Node* head)


{
if (head == NULL)
return NULL;

struct Node* temp = head;


head = head->next;

if (head != NULL)
head->prev = NULL;

free(temp);
return head;
}

Delete the tail of the doubly linked list


This operation is same as the deletion of the head where, instead of the head, we need to delete the
tail. To identify a node as a tail node, we should check if the next pointer or next node is null or not.
After deleting the tail, we need to free the memory.

Here’re the steps to do this:


Step 1: Traverse until the tail node of the doubly linked list.
Step 2: Assign a variable or pointer to the tail node.
Step 3: Make the “next” node as NULL and free the memory of the tail node.

struct Node* deleteTail(struct Node* head)


{
if (head == NULL || head->next == NULL)
return NULL;

struct Node* current = head;

while (current->next->next != NULL)


current = current->next;

struct Node* tail = current->next;


current->next = NULL;

free(tail);
return head;
}

Search and delete a node from Doubly Linked List


This operation allows us to search for a specific node data and delete that node. We need to perform
a linear search as the linked list is a linear data structure. After deleting, we also need to free the
memory.
Here’re the steps for searching and deleting a node in the doubly linked list:
Step 1: Traverse the linked list from the head until the node’s value equals the search item.
Step 2: Assign a variable “deleteNode” to the matched node.
Step 3: Assign the previous node of the “deleteNode” to the next node.
Step 4: Free the memory of the “deleteNode”
struct Node* searchAndDelete(struct Node* head, int searchItem)
{
struct Node* current = head;
while (current->next != NULL && current->next->data != searchItem)
current = current->next;
if (current->next == NULL)
return head;
struct Node* temp = current->next;
current->next = current->next->next;
if (current->next != NULL)
current->next->prev = current;
free(temp);
return head;
}

Traverse a doubly linked list from forward


To traverse from the head node and iterate over the next node until we find “NULL”. While traversing
each node, we can print the value of the node. Here are the steps for traversing from the front :
Step 1: Assign a pointer or variable to the current head node.
Step 2: Iterate to the next node of the head until getting “NULL”
Step 3: Print the node’s data in each iteration.
Step 4: Return the head node.

void traverseFromFront(struct Node* head)


{
struct Node* current = head;
while (current != NULL)
{
printf("%d\n", current->data);
current = current->next;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy