C Program for Deletion from end in doubly linked list

Deletion from end in doubly linked list

Today we will study in C Program for Deletion from end in doubly linked list . In doubly linked we have three field which contain previous pointer ,node having data and next pointer which have the address of next node.The advantage of doubly linked list is that the traversal of nodes is easy because having two pointer in the one node.

C Program for Deletion from end in doubly linked list

Working require for deletion in doubly linked list at the last node :-

  1.  First to delete the last node from the list we have to start moving on the list from the first node until the address part of the node is null.
  2.   After that we will keep going of the second last node in some extra node that is previous node.
  3.   As the address part of the node is null then set the address part of the previous node as null.
  4.   At last free the last node.

The Structure of the Node in the Doubly Linked List :-

struct node   
{  
    struct node *prev;   
    int data;  
    struct node *next;   
}   
Deletion in Doubly Linked List in C 2

Algorithm for doubly linked list to delete an element from the end :-

  • STEP 1  :  NOW IF HEAD = NULL (GO TO STEP 6)
  • STEP 2  : ASSIGN EXTRA = HEAD
  • STEP 3  :  MAKE A LOOP WHILE EXTRA = NEXT != NULL
  • STEP 4  :  ASSIGN EXTRA = EXTRA -> NEXT (END THE WHILE LOOP)
  • STEP 5  :  THAN ASSIGN EXTRA -> PREVIOUS -> NEXT = NULL
  • STEP 6  :  THAN FREE EXTRA
  • STEP 7  : EXIT

Code for Deletion From End in a Doubly Linked List

Run
#include<stdio.h>
#include<stdlib.h>

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

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

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

  freshNode->data = data;
  freshNode->next = *head;
  freshNode->prev = NULL;

  // If the linked list already had atleast 1 node
  if (*head != NULL)
    (*head)->prev = freshNode;

  // freshNode will become head
  *head = freshNode;
}

void deleteEnd (struct Node **head)
{
  struct Node *tempNode = *head;

  // if DLL is empty
  if (*head == NULL)
    {
      printf ("Linked List Empty, nothing to delete\n\n");
      return;
    }

  // if Linked List has only 1 node
  if (tempNode->next == NULL)
    {
      printf ("%d deleted\n\n", tempNode->data);
      *head = NULL;
      return;
    }

  // else traverse to the last node
  while (tempNode->next != NULL)
    tempNode = tempNode->next;

  struct Node *secondLast = tempNode->prev;

  // Curr assign 2nd last node's next to Null
  secondLast->next = NULL;

  printf ("%d deleted\n\n", tempNode->data);
  free (tempNode);
}

//function to print the doubly linked list
void display (struct Node *node)
{
  struct Node *end = NULL;

 
  while (node != NULL)
    {
      printf (" %d ", node->data);
      end = node;
      node = node->next;
    }

  printf ("\n\n");
}

int main ()
{
  struct Node *head = NULL;

  insert (&head, 7);
  insert (&head, 8);
  insert (&head, 9);
  insert (&head, 10);
  insert (&head, 11);
  insert (&head, 12);

  printf("Linked List Before Deletion");
  display (head);

  deleteEnd (&head);
  printf("Linked List After Deletion");
  display (head);

  return 0;
}

Output

Linked List Before Deletion 12  11  10  9  8  7 

7 deleted

Linked List After Deletion 12  11  10  9  8 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Doubly Linked List

Doubly Linked List

  • Introduction to Doubly Linked list in Data Structure
    Click Here
  • Doubly Linked List in –
    C | C++ | Java
  • Insertion in doubly linked list –
    C | C++ | Java
  • Insertion at beginning in doubly linked list –
    C | C++ | Java
  • Insertion at end in doubly linked list –
    C | C++ | Java
  • Insertion at nth node in doubly linked list –
    C | C++ | Java
  • Deletion in doubly linked list  –
    C | C++ | Java
  • Deletion from beginning in doubly linked list :
  • Deletion from nth in doubly linked list :
    C | C++ | Java
  • Deletion from end in doubly linked list :
    C | C++ | Java
  • Insertion and Deletion in a  doubly linked list :
    C | C++ | Java
  • Insertion in the middle in a  doubly linked list :
    C | C++ | Java