C Programming language

adplus-dvertising
C - Doubly Linked List, Delete
Previous Home Next

Various deletion operations that can be perform on a doubly linked list

Just like the insertion operations the deletion operations in a doubly linked list can be done in the following ways :

  1. Deletion from the front.
  2. Deletion from the end.
  3. Deletion from the specified position in the list.

Since by using doubly linked list the deletion of the nodes or elements have become quite very easy in  a list. Just by doing few minor pointer changes we can perform various deletion operations. Following is the program which shows how we can delete a node from the list.

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
    {
int num;
struct node *next;
struct node *prev;
};                             /* declaring a global node of type struct */
typedef struct node NODE;     /* providing a type definition to the above created structure */
NODE *head=NULL;             /* declaring some of the global variable that would be used throughout the program */
NODE *temp, *first, *last;
int info;
void display();
void create_list();
void del_at_begin();
void del_at_end();
void del_at_specifiedpos();
void main()                                           /* starting the main method() */
{
int i;
clrscr();
printf("\nprogram for deletion in a doubly linked list :\n");
do
{
printf("\nEnter your choice :\n");          /*    creating menu for various insertion operations on the list */
printf("\n1.Create a linked list :");
printf("\n2.Delete from the front in the linked list :");
printf("\n3.Delete from the end position in the linked list :");
printf("\n4.Delete at any specified position in the linked list :");
printf("\n5.Exit\n");
fflush(stdin);
scanf("\n%d",&i);
switch(i)
{
case 1:
create_list();
display();
case 2:
del_at_begin();
display();
break;
case 3:
del_at_end();
display();
break;
case 4:
del_at_specifiedpos();
display();
break;
case 5:
exit(0);
}
}
while(i<=5);
getch();
}
void create_list()
{
printf("\nEnter your element in the linked list :");         /*creating a doubly linked list */
scanf("%d",&info);
temp=(NODE *)malloc(sizeof(NODE));  /* allocating memory for the node to be inserted */
temp->num=info;
temp->next=NULL;
temp->prev=NULL;
if(head==NULL)                       /* checking whether list is empty */
{
head=temp;
first=temp;
last=temp;                                                            
}
else                             /*inserting at the end of the list*/
{
first=head;
while(first->next!=last)  
{
first=first->next;
}
first->next=temp;
temp->prev=first;
temp->next=NULL;
last=temp;
}
}
void display()
{
if(head==NULL)
printf("linked list is empty");
else
{
    first=head;
    printf("\nStatus of the linked list is as follows :\n");
     while(first->next!=last)           /* traversing the linked list */
     {
      printf("\n%d",first->num);
      first=first->next;
     }
    }
}
void del_at_begin()
{
 if(head==NULL)
{
     printf("linked list is empty");
}
 else
{
    temp=head;
head=head->next;
head->prev=NULL;
free(temp);
}

}
void del_at_end()
{
 if(head==NULL)

 {
  printf("\nlinked list is empty");
 }
else
{
     temp=head;
 while(temp->next!=last)
{
 temp=temp->next;
}
    last=temp->prev;
last->next=NULL
free(temp);
}
}
void del_at_specifiedpos()
{
 int node_num;
 if(head==NULL)
{
    printf("\nlinked list is empty");
}
else
{
    temp=head;
 printf("\nEnter the position of the node you want to delete");
 scanf("\n%d",&node_num);
    for(int i=1;i<node_num;i++)
{
     second=temp;
 temp=temp->next;
}
first=temp->next;
second->next=temp->next;
first->prev=temp->prev;
free(temp);
}
}
Previous Home Next