#include<stdio.h>
#include<stdlib.h>
struct node
{
int x;
struct node *next,*prev;
};
typedef struct node node;
node *insert_beg(node *);
node *insert_end(node *);
node *insert_before(node *);
node *insert_after(node *);
void display(node *);
node *dlt_beg(node *);
node *dlt_end(node *);
node *dlt_target(node *);
void search(node *);
int count(node *);
int main()
{
int n,c;
node *start=NULL;
do{
printf("\n 1)Insert Node\n 2)Delete Node\n 3)Searching Node\n 4)count number of nodes\n 5)Exit");
printf("\n Enter your choice (1-5) :");
scanf("%d",&n);
switch(n)
{
case 1:printf("\n 1)Insert at begining\n 2)Insert at End\n 3)Insert before a node");
printf("\n 4)Insert after a node");
printf("\n Enter your choice (1-4) :");
scanf("%d",&c);
switch(c)
{
case 1:start=insert_beg(start); break;
case 2:start=insert_end(start); break;
case 3:start=insert_before(start); break;
case 4:start=insert_after(start); break;
default :printf("\n inavlid option");
}
break;
case 2:printf("\n 1)Deletion from start\n 2)Deletion from End\n 3)Deletion of a target node");
printf("\n Enter your choice (1-3) :");
scanf("%d",&c);
switch(c)
{
case 1:start=dlt_beg(start); break;
case 2:start=dlt_end(start); break;
case 3:start=dlt_target(start); break;
default :printf("\n Invalid option");
}
break;
case 3:search(start); break;
case 4:printf("\n Number of node=%d",count(start)); break;
case 5:printf("\n ...Thankyou!..."); break;
default :printf("\n Invalid choice");
}
display(start);
}while(n!=5);
return 0;
}
node *insert_beg(node *start)
{
node *ptr;
int n;
ptr=(node *)malloc(sizeof(node));
printf("\n Enter node value=");
scanf("%d",&n);
if(start==NULL)
{
ptr->x=n;
start=ptr;
ptr->next=NULL;
ptr->prev=NULL;
return start;
}
ptr->x=n;
ptr->next=start;
start->prev=ptr;
ptr->prev=NULL;
start=ptr;
return start;
}
node *insert_end(node *start)
{
node *ptr,*fpt=start;
int n;
ptr=(node *)malloc(sizeof(node));
printf("\n Enter node value=");
scanf("%d",&n);
if(start==NULL)
{
ptr->x=n;
ptr->prev=NULL;
ptr->next=NULL;
start=ptr;
return start;
}
while(fpt->next!=NULL)
fpt=fpt->next;
ptr->x=n;
fpt->next=ptr;
ptr->prev=fpt;
ptr->next=NULL;
return start;
}
node *insert_before(node *start)
{
node *fpt=start,*ptr;
int n,t,flag=0;
if(start==NULL)
{
printf("\n Insertion before a target node is not possible");
return NULL;
}
printf("\n Enter target node value=");
scanf("%d",&t);
while(fpt!=NULL)
{
if(fpt->x==t)
{
printf("\n target node found. Enter node value=");
scanf("%d",&n);
flag=1;
break;
}
else
fpt=fpt->next;
}
if(flag==0)
{
printf("\n target node not found");
return start;
}
ptr=(node *)malloc(sizeof(node));
if(fpt==start)
{
ptr->x=n;
ptr->next=fpt;
fpt->prev=ptr;
ptr->prev=NULL;
start=ptr;
return start;
}
ptr->x=n;
ptr->next=fpt;
fpt->prev->next=ptr;
fpt->prev=ptr;
return start;
}
node *insert_after(node *start)
{
node *ptr,*fpt=start;
int n,t,flag=0;
if(start==NULL)
{
printf("\n Insertion after a target is not possible");
return NULL;
}
printf("\n Enter target node value=");
scanf("%d",&t);
while(fpt!=NULL)
{
if(fpt->x==t)
{
printf("\n target node found. Enter node value=");
scanf("%d",&n);
flag=1;
break;
}
else
fpt=fpt->next;
}
if(flag==0)
{
printf("\n target node not found");
return start;
}
ptr=(node *)malloc(sizeof(node));
if(fpt->next==NULL)
{
ptr->x=n;
fpt->next=ptr;
ptr->prev=fpt;
ptr->next=NULL;
return start;
}
ptr->x=n;
ptr->next=fpt->next;
fpt->next->prev=ptr;
fpt->next=ptr;
ptr->prev=fpt;
return start;
}
void display(node *start)
{
node *ptr=start;
if(start==NULL)
{
printf("\n List is Empty.");
return;
}
printf("\n List is :");
while(ptr!=NULL)
{
printf("%d ",ptr->x);
ptr=ptr->next;
}
return ;
}
int count(node *start)
{
node *ptr=start;
int c=0;
while(ptr!=NULL)
{
c++;
ptr=ptr->next;
}
return c;
}
void search(node *start)
{
node *ptr=start;
int n,flag=0;
if(start==NULL)
return;
printf("\n Enter searching node value=");
scanf("%d",&n);
while(ptr!=NULL)
{
if(ptr->x==n)
{
flag=1;
break;
}
else
ptr=ptr->next;
}
if(flag==1)
printf("\n %d node is found",n);
else if(flag==0)
printf("\n node not found");
return ;
}
node *dlt_beg(node *start)
{
if(start==NULL)
return NULL;
node *ptr=start;
if(ptr->next!=NULL)
ptr->next->prev=NULL;
start=ptr->next;
free(ptr);
printf("\n first node is deleted");
return start;
}
node *dlt_end(node *start)
{
node *ptr=start;
if(start==NULL)
return NULL;
if(ptr->next==NULL)
{
free(ptr);
printf("\n last node is deleted");
return NULL;
}
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->prev->next=NULL;
free(ptr);
printf("\n last node is deleted");
return start;
}
node *dlt_target(node *start)
{
node *ptr=start;
int n,flag=0;
if(start==NULL)
return NULL;
printf("\n enter node that you want to delete=");
scanf("%d",&n);
while(ptr!=NULL)
{
if(ptr->x==n)
{
flag=1;
if(ptr->prev==NULL)
{
if(ptr->next!=NULL)
ptr->next->prev=NULL;
start=ptr->next;
}
else if(ptr->next==NULL)
ptr->prev->next=NULL;
else
{
ptr->prev->next=ptr->next;
ptr->next->prev=ptr->prev;
}
free(ptr);
break;
}
ptr=ptr->next;
}
if(flag==0)
printf("\n target node is not found");
else if(flag==1)
printf("\n %d node is deleted",n);
return start;
}