# 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. ## 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;   }   ` ## 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

## C Program for Deletion from end in doubly linked list :-

```#include <stdio.h>
#include <stdlib.h>
struct node
{				// node structure
int data;
struct node *nextpt;

void print ()
{
struct node *extra;
{
printf ("List is empty");
}
else
{
while (extra != NULL)	// Print the linked list
{
printf ("%d  ", extra->data);
extra = extra->nextpt;
}
}
}

void list (int n)
{
struct node *new_node, *extra;
int data, l;
headpt = (struct node *) malloc (sizeof (struct node));
if (headpt == NULL)		// list is empty
{
printf ("list is empty");
}
else
{
printf ("Enter the data of node : ");
scanf ("%d", &data);
for (l = 2; l <= n; l++)
{
new_node = (struct node *) malloc (sizeof (struct node));
if (new_node == NULL)
{
printf ("list is empty");
break;
}
else
{
printf ("Enter the data of node : ", l);
scanf ("%d", &data);
new_node->data = data;
new_node->nextpt = NULL;
extra->nextpt = new_node;
extra = extra->nextpt;
}
}
}
}

void delete_At_end()
{
if(headpt -> nextpt == NULL)  //list is empty
{
headpt = NULL; // now make the reference to Null
}
while(extra -> nextpt != NULL)
{
prev_node = extra;
extra = extra -> nextpt;
}
free(extra); //t will delete the last node
prev_node -> nextpt = NULL;
}

int main ()
{
int n, data, pos;
printf ("Enter the size of nodes: ");
scanf ("%d", &n);
if (n == 0)
{
printf ("List is empty\n");
exit (0);
}
else
{
list (n);
}
printf ("The Doubly Linked List is :\n");
print ();
delete_At_end ();
printf ("\nAfter Deleting the last node the Linked List is :\n");
print ();
return 0;
}```
```Output :-
Enter the size of nodes: 7
Enter the data of node : 32
Enter the data of node : 85
Enter the data of node : 12
Enter the data of node : 11
Enter the data of node : 47
Enter the data of node : 5
Enter the data of node : 100
The Doubly Linked List is :
32  85  12  11  47  5  100
After Deleting the last node the Linked List is :
32  85  12  11  47  5
```