deletion in doubly linked list at the end
Data Structure Notes

Deletion in Doubly Linked List at the End

Deletion in Doubly Linked List at the End

Deletion in doubly linked list at the end means deleting the last node of a doubly linked list. In this tutorial, we will learn about how can we delete the last node of a doubly linked list.

The algorithm and C Program to perform deletion in a doubly linked list at the end is explained in this tutorial.

Step 1 – Check whether the Linked list is empty.  Check Head==NULL

Step 2 – If the Linked List is empty display the message deletion is not possible and exit.

Step 3 – If the Linked List is not empty then at first we have to reach the end of the linked list. For this purpose, we have to traverse the Linked list from beginning to end. To Traverse the linked List we Declare two node pointer variables temp1 and temp2 and initialize them with head.

Step 4- Move the temp1 and temp2 from beginning to end until temp1 refers to the last node of the linked list and temp2 refers to the second last node of the linked list.

Step 5 – When Temp1 refers to the last node and temp 2 refers to the second last node then set Null in the next ( address ) field of temp2 because after deleting the current last node ( temp1) temp2 will become the last node of doubly linked list.

Step 6 – Delete Temp1 which means free(temp1).

The process to perform deletion in a doubly linked list at the end is also represented in the following figure.

deletion in doubly linked list at the end

The pseudo-code or Function to delete the last node of the doubly Linked List is given here

void DeletefromEnd( )

{

    struct node * newNode ;

    if(head == NULL)

    {

        printf(“List is Empty!\n”);

    }

    else

    {

        struct node *temp1,*temp2;

        temp1=head;

        temp2=head;

       //Move temp1 and temp 2 until reach the end of the doubly linked list

       while( temp1->next !=NULL)

        {

            temp2=temp1;

            temp1=temp1->next ;

        }

        temp2->next=NULL;

        free(temp1);

    printf(“Last Node Deleted SUCCESSFULLY\n”);

    }

}

C Program for Deletion in Doubly Linked List at End

C Program for deletion in doubly linked list at the end is given below –

#include <stdio.h>

#include <stdlib.h>

/* Define structure of Node */

struct node {

int data;

struct node * prev;

struct node * next;

};

struct node *firstnode;

struct node *lastnode;

struct node *head;

void createList(int n);

void displayList();

void DeletefromEnd();

int main()

{

int numnode;

int value;

printf(“enter the number of nodes in doubly linked list”);

scanf(“%d”,&numnode);

createList(numnode);

displayList();

DeletefromEnd();

displayList();

return 0;

}

/* First we will create a doubly linked list of n nodes*/

void createList(int n)

{

int i, data;

struct node *newNode;

if(n >= 1)

{

/* Initially Creates and links list only with one node (first node) */

firstnode = (struct node *)malloc(sizeof(struct node));

printf(“Enter data of 1 node: “);

scanf(“%d”, &data);

firstnode->data = data;

firstnode->prev = NULL;

firstnode->next = NULL;

lastnode = firstnode;

head=firstnode;

/* Create and link remaining n-1 nodes */

for(i=2; i<=n; i++)

{

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

printf(“Enter data of %d node: “, i);

scanf(“%d”, &data);

newNode->data = data;

//Add a newly creted node at the end of the linked list

newNode->prev = lastnode; // Link new node with the previous node

newNode->next = NULL;

lastnode->next = newNode; // Link previous node with the new node

last node = newNode;          // Make new node as last/previous node

}

printf(“\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n”);

}

}

// Print the data value of nodes of Doubly Linked list tfrom Start to end.

void displayList()

{

struct node * temp;

int n = 1;

if(head == NULL)

{

printf(“List is empty.\n”);

}

else

{

temp = head;

printf(“Linked LIST Is:\n”);

while(temp != NULL)

{

printf(“%d\t”, temp->data);

n++;

/* Move the temp pointer to the next node */

temp = temp->next;

}

}

}

void DeletefromEnd( )

{

struct node * newNode ;

if(head == NULL)

{

printf(“Error, List is Empty!\n”);

}

else

{

struct node *temp1,*temp2;

temp1=head;

temp2=head;

while( temp1->next !=NULL)

{

temp2=temp1;

temp1=temp1->next ;

}

temp2->next=NULL;

free(temp1);

printf(“deletion in doubly linked list at the end is successful \n”);

}

}

Output

enter the number of nodes in the doubly linked list 4

Enter data of 1 node: 21

Enter data of 2 nodes: 22

Enter data of 3 nodes: 23

Enter data of 4 nodes: 24

DOUBLY LINKED LIST CREATED SUCCESSFULLY

Linked LIST Is:

21        22        23        24        Last Node Deleted SUCCESSFULLY AT THE BEGINNING OF THE LIST

Linked LIST Is:

21        22        23

Conclusion and Summary

In this tutorial, we have explained the algorithm and C Program to perform deletion in doubly linked list at the end .

Previous Tutorial – Deletion in Doubly Linked List from Beginning

Previous Tutorial – Deletion in Doubly Linked List from Beginning

Leave a Reply

Your email address will not be published. Required fields are marked *