#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *flink;
struct node *blink;
};
struct node* creation();
void display(struct node*);
struct node* insfirst(struct node*);
struct node* inslast(struct node*);
struct node* insele(struct node*);
struct node* inspos(struct node*);
struct node* delfirst(struct node*);
struct node* dellast(struct node*);
struct node* delele(struct node*);
struct node* delpos(struct node*);
void main()
{
int choice,ch;
struct node *head=NULL;
clrscr();
do
{
printf("\n\n\n\t\t\tPGM FOR DOUBLY LINKED LIST\nenter your choice\n");
printf("\n\n1.creation\n2.viewing\n3.insertion\n4.deletion\n5.exit from the program\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n\t\t\tcreation of doubly linked list\n");
head=creation();
display(head);
break;
case 2:display(head);
break;
case 3:printf("\n\n\t\tinsertion in doubly linked list\n");
printf("\n\n\tenter your choice\n1.insertion at first node\n2.insertion at last\n3.insertion by element\n4.insertion by position\n\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n\tinsertion at head node\n");
head=insfirst(head);
break;
case 2:printf("\n\tinsertion at last\n");
head=inslast(head);
break;
case 3:printf("\n\tinsertion by element\n");
head=insele(head);
break;
case 4:printf("\n\tinsertion by position\n");
head=inspos(head);
break;
default:printf("\n\tinvalid entry\n");
};
break;
case 4:if(head!=NULL)
{
printf("\n\n\tdeletion of a node\nenter your choice\n");
printf("\n1.del at first\n2.del at last\n3.del by element\n4.del by position");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n\tdeletion at head node\n");
head=delfirst(head);
break;
case 2:printf("\n\tdeletion at last\n");
head=dellast(head);
break;
case 3:printf("\n\tinsertion by element\n");
head=delele(head);
break;
case 4:printf("\n\tinsertion by position\n");
head=delpos(head);
break;
default:printf("\n\tinvalid entry\n");
};
}
else
printf("\ndeletion not possible\nlist is empty\n");
break;
case 5:printf("\n\n\tThankyou");
getch();
break;
default:printf("\n\ninvalid entry");
};
}while(choice!=5);
}
struct node* creation()
{
int n,i;
struct node *first,*temp,*newnode;
first->blink=NULL;
first->flink=NULL;
printf("\nenter the number of nodes to be created\n");
scanf("%d",&n);
first=(struct node*)malloc(sizeof(struct node));
printf("\nenter the data for node 1:");
scanf("%d",&(first->info));
temp=first;
if(n>1)
{
for(i=1;i<n;i++)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
printf("\nenter the data for node %d:",i+1);
scanf("%d",&(newnode->info));
newnode->blink=temp;
newnode->flink=NULL;
temp->flink=newnode;
temp=newnode;
if(i==1)
first->flink=temp;
}
}
return(first);
}
void display(struct node *head)
{
struct node *travel;
if(head==NULL)
printf("\n\nlist is empty\n");
printf("\n\n\n\n\n");
travel=head;
printf("<---%u:%d:%u--->",travel->blink,travel->info,travel->flink);
travel=travel->flink;
while(travel!=NULL)
{
printf("<---%u:%d:%u--->",travel->blink,travel->info,travel->flink);
travel=travel->flink;
}
}
struct node* insfirst(struct node *head)
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node*));
printf("\nenter the data to be inserted at head node\n");
scanf("%d",&(newnode->info));
newnode->blink=NULL;
newnode->flink=head;
head->blink=newnode;
head=newnode;
display(head);
return(head);
}
struct node* inslast(struct node *head)
{
struct node *newnode,*travel;
newnode=(struct node*)malloc(sizeof(struct node));
travel=head;
printf("\nenter the data to be inserted at last\n");
scanf("%d",&(newnode->info));
newnode->flink=NULL;
while(travel->flink!=NULL)
travel=travel->flink;
newnode->blink=travel;
travel->flink=newnode;
display(head);
return(head);
}
struct node* insele(struct node *head)
{
struct node *new,*travel,*next;
int ele;
travel=head;
new=(struct node*)malloc(sizeof(struct node));
printf("\ndisplaying list\n");
display(head);
printf("\nenter the data value after which element is to be inserted");
scanf("%d",&ele);
while(travel->flink!=NULL)
{
if(travel->info==ele)
{
printf("\nenter the data to be inserted\n");
scanf("%d",&(new->info));
next=travel->flink;
new->blink=travel;
new->flink=travel->flink;
travel->flink=new;
next->blink=new;
display(head);
return(head);
}
else
travel=travel->flink;
}
printf("\nelement not found\n");
return(head);
}
struct node* inspos(struct node *head)
{
struct node *new,*travel,*next;
int n,i;
i=1;
new=(struct node*)malloc(sizeof(struct node));
while(travel->flink!=NULL)
{
travel=travel->flink;
i++;
}
printf("\nenter the positon after which insertion is to be made\n(b/w1and%d)",i);
scanf("%d",&n);
if(n<i)
{
printf("\nenter the data to be inserted\n");
scanf("%d",&(new->info));
i=1;
travel=head;
while(i<n)
{
travel=travel->flink;
i++;
}
new->blink=travel;
new->flink=travel->flink;
next=travel->flink;
next->blink=new;
travel->flink=new;
display(head);
return(head);
}
else
printf("\ninvalid entry\n");
return(head);
}
struct node* delfirst(struct node *head)
{
struct node *last,*prev;
if(head->flink==NULL)
{printf("\nthe data being deleted is %d\n",head->info);
head=NULL;
return(head);
}
printf("\nthe data being deleted is %d\n",head->info);
head=head->flink;
head->blink=NULL;
display(head);
return(head);
}
struct node* dellast(struct node *head)
{
struct node *last,*prev;
if(head->flink==NULL)
{
printf("\nthe element being deleted is %d\n",head->info);
head=NULL;
display(head);
return(head);
}
last=head;
while(last->flink!=NULL)
last=last->flink;
prev=last->blink;
prev->flink=NULL;
printf("\n the element being deleted is %d\n",last->info);
display(head);
return(head);
}
struct node* delele(struct node *head)
{
struct node *travel,*prev;
int deldata;
travel=head;
printf("\nenter the element to be deleted\n");
scanf("%d",&deldata);
while(travel->flink!=NULL)
{
if(travel->info==deldata)
{
prev=travel->blink;
prev->flink=travel->flink;
travel=travel->flink;
travel->blink=prev;
printf("\n\nthe deleted data is %d",deldata);
display(head);
return(head);
}
else
travel=travel->flink;
}
display(head);
return(head);
}
struct node* delpos(struct node *head)
{
struct node *travel,*prev;
int count,pos;
count=1;
prev=head;
while(travel->flink!=NULL)
{
count++;
travel=travel->flink;
}
travel=head;
printf("\nenter the positon of the node to be deletedb/w1 1 and %d\n",count);
scanf("%d",&pos);
if(pos<count)
{
count=1;
while(count<=(pos-1))
{ prev=travel;
travel=travel->flink;
count++;
}
prev->flink=travel->flink;
travel->blink=prev;
printf("\nelement deleted sucessfully\n");
display(head);
}
else
printf("\ninvalid entry");
return(head);
}
No comments:
Post a Comment