doubly linked list, doubly linked list delete, doubly linked list delete at any position
Write a program in C language to implement doubly linked list deletion at any position.

Below is a picture of a doubly linked list for reference:


Implementation of the above problem:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int data,c,i,n,pos;
struct node
{
int element;
struct node*next;
struct node*prev;
}
*D=NULL;
void create(int);
void mdel(void);
void display(void);
int count(void);
void main()
{
int ch;
while(1)
{
printf("\n\n1.Create\n");
printf("\n2.Delete at any position\n");
printf("\n3.Display\n");
printf("\n4.Count\n");
printf("\n5.Exit\n");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the no.of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the element");
scanf("%d",&data);
create(data);
}
break;
case 2:
mdel();
display();
break;
case 3:
display();
break;
case 4:
count();
break;
case 5:
exit(0);
break;
default: printf("\nWrong choice....");
}
}
}
void create(int data)
{
struct node *temp,*P;
temp=malloc(sizeof(struct node));
temp->element=data;
temp->next=NULL;
if(D==NULL)
{
temp->prev=NULL;
D=temp;
return;
}
P=D;
while(P->next!=NULL)
P=P->next;
temp->prev=P;
P->next=temp;
}
void display(void)
{
struct node *P,*temp;
if(D==NULL)
{
printf("List empty");
return;
}
P=D;
printf("\nThe Elements are :");
while(P!=NULL)
{
printf("\t%d->",P->element);
P=P->next;
}
}
void mdel(void)
{
struct node *temp,*P;
if(D==NULL)
{
printf("\nList is empty");
return;
}
printf("\nEnter the position to delete");
scanf("%d",&pos);
if(pos>count())
{
printf("Deletion is not possible");
return;
}
P=D;
for(i=1;i<pos-1;i++)
P=P->next;
temp=P->next;
P->next=temp->next;
temp->next->prev=P;
printf("\nThe element is deleted is %d",temp->element);
free(temp);
}
int count(void)
{
int c=0;
struct node *temp,*P;
if(D==NULL)
{
printf("\nThe list empty");
return 0;
}
P=D;
while(P!=NULL)
{
P=P->next;
c++;
}
printf("\nThe elements are is %d",c);
return c;
}


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments:

Thanks
Mukesh Rajput