0% found this document useful (0 votes)
15 views6 pages

Algorithm for Deletion in a Doubly Linked List

The document outlines an algorithm for deleting nodes in a doubly linked list, detailing steps for deleting the first node, last node, and a node at a specific position. It includes C code for implementing these deletion functions, along with a function for inserting nodes and printing the list. The main function demonstrates the usage of these operations on a sample doubly linked list.

Uploaded by

Ritika Lohiya
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)
15 views6 pages

Algorithm for Deletion in a Doubly Linked List

The document outlines an algorithm for deleting nodes in a doubly linked list, detailing steps for deleting the first node, last node, and a node at a specific position. It includes C code for implementing these deletion functions, along with a function for inserting nodes and printing the list. The main function demonstrates the usage of these operations on a sample doubly linked list.

Uploaded by

Ritika Lohiya
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/ 6

Algorithm for Deletion in a Doubly Linked List

Steps to Delete a Node

1. Check if the list is empty:

o If head == NULL, print an error message.

2. Delete the first node:

o Update head to the next node.

o Set head->prev = NULL.

o Free the memory of the removed node.

3. Delete the last node:

o Traverse to the last node.

o Update the second last node’s next to NULL.

o Free the memory of the last node.

4. Delete a node at a specific position:

o Traverse to the node at the given position.

o Adjust the prev and next pointers of the adjacent nodes.

o Free the memory of the removed node.

C Code for Deletion in a Doubly Linked List

#include <stdio.h>

#include <stdlib.h>

// Define a node structure

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

// Function to create a new node


struct Node* createNode(int data) {

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

newNode->data = data;

newNode->prev = NULL;

newNode->next = NULL;

return newNode;

// Function to insert at the end (for testing)

void insertAtEnd(struct Node** head, int data) {

struct Node* newNode = createNode(data);

if (*head == NULL) {

*head = newNode;

return;

struct Node* temp = *head;

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

// Function to delete the first node

void deleteFirstNode(struct Node** head) {

if (*head == NULL) {

printf("List is empty.\n");

return;

struct Node* temp = *head;


*head = temp->next;

if (*head != NULL)

(*head)->prev = NULL;

free(temp);

// Function to delete the last node

void deleteLastNode(struct Node** head) {

if (*head == NULL) {

printf("List is empty.\n");

return;

struct Node* temp = *head;

if (temp->next == NULL) {

*head = NULL;

free(temp);

return;

while (temp->next != NULL)

temp = temp->next;

temp->prev->next = NULL;

free(temp);

// Function to delete a node at a specific position (1-based index)

void deleteAtPosition(struct Node** head, int position) {

if (*head == NULL || position <= 0) {

printf("Invalid position or list is empty.\n");

return;
}

struct Node* temp = *head;

// If deleting the first node

if (position == 1) {

deleteFirstNode(head);

return;

for (int i = 1; temp != NULL && i < position; i++)

temp = temp->next;

// If position is greater than the number of nodes

if (temp == NULL) {

printf("Position out of range.\n");

return;

if (temp->next != NULL)

temp->next->prev = temp->prev;

if (temp->prev != NULL)

temp->prev->next = temp->next;

free(temp);

// Function to print the list


void printList(struct Node* head) {

struct Node* temp = head;

printf("Doubly Linked List: ");

while (temp != NULL) {

printf("%d <-> ", temp->data);

temp = temp->next;

printf("NULL\n");

// Driver code

int main() {

struct Node* head = NULL;

insertAtEnd(&head, 10);

insertAtEnd(&head, 20);

insertAtEnd(&head, 30);

insertAtEnd(&head, 40);

printList(head);

deleteFirstNode(&head);

printList(head);

deleteLastNode(&head);

printList(head);

deleteAtPosition(&head, 2);

printList(head);
return 0;

Explanation of the Code

1. Node Structure:

o data: Stores the integer value.

o prev: Pointer to the previous node.

o next: Pointer to the next node.

2. Deletion Functions:

o deleteFirstNode(): Removes the first node.

o deleteLastNode(): Removes the last node.

o deleteAtPosition(): Deletes a node at a specific position.

3. Insertion Function (for testing):

o insertAtEnd(): Adds nodes to the end.

4. Printing Function:

o printList(): Displays the doubly linked list.

5. Main Function:

o Inserts elements into the list.

o Deletes nodes from different positions.

o Displays the list after each deletion.

This code efficiently handles deletion operations in a Doubly Linked List.

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