A program on double linked list... To perform various operations on DLL.
#include"stdio.h" /*Note tags are to be used for pre-prcessor directives*/
#include"conio.h"
#include"stdlib.h"
struct node
{
int info;
struct node *left;
struct node *right;
};
typedef struct node *nodeptr;
nodeptr getnode()
{
nodeptr p;
p=(nodeptr)malloc(sizeof(struct node));
p->left=NULL;
p->right=NULL;
return p;
}
void freenode(nodeptr p)
{
free(p);
}
nodeptr ins_f(nodeptr list,int ele)
{
nodeptr p;
p=getnode();
p->info=ele;
if(list==NULL)
{
list=p;
}
else
{
p->right=list;
list->left=p;
list=p;
}
clrscr();
printf("%d element inserted",ele);
return list;
}
nodeptr ins_end(nodeptr list,int ele)
{
nodeptr p,q;
p=getnode();
p->info=ele;
if(list==NULL)
{
list=p;
}
else
{
for(q=list;q->right!=NULL;q=q->right);
p->left=q;
q->right=p;
}
clrscr();
printf("%d element inserted",ele);
return list;
}
nodeptr ins_aftx(nodeptr list,int ele,int x)
{
nodeptr p,q;
int flag=0;
p=getnode();
p->info=ele;
if(list==NULL)
{
printf("\nNo element X found in the list...!");
return 0;
}
else
{
for(q=list;q!=NULL;q=q->right)
if(q->info==x)
{
flag=1;
break;
}
if(flag)
{
(q->right)->left=p;
p->right=q->right;
q->right=p;
p->left=q;
clrscr();
printf("%d inserted into the list after %d",ele,x);
}
else printf("%d not found in the list..!",x);
}
return list;
}
void display(nodeptr list)
{
nodeptr p;
if(list==NULL)
{
printf("List empty..!");
return;
}
else
{
printf("\nLIST ELEMENTS\n-------------");
for(p=list;p!=NULL;p=p->right)
{
printf("\n%d",p->info);
}
}
}
nodeptr del_f(nodeptr list)
{
nodeptr p;
int x;
if(list==NULL)
{
printf("\nInvalid operation..!");
return 0;
}
else
{
p=list;
list=list->right;
list->left=NULL;
x=p->info;
freenode(p);
printf("%d deleted form list..!",x);
}
return list;
}
nodeptr del_end(nodeptr list)
{
nodeptr p,q;
int x;
if(list==NULL)
{
printf("\nInvalid opertion...!");
return 0;
}
else
{
q=NULL;
for(p=list;p->right!=NULL;p=p->right)
q=p;
x=p->info;
q->right=NULL;
freenode(p);
}
printf("%d deleted from the list..!",x);
return list;
}
nodeptr del_b4x(nodeptr list,int x)
{
nodeptr p,q,r;
int y,flag=0;
if(list==NULL||list->info==x)
{
printf("Invalid opertaion..!");
return 0;
}
else
{
q=NULL;
for(p=list;p!=NULL;p=p->right)
{
if((list->right)->info==x)
list=list->right;
if(p->info==x)
{
flag=1;
break;
}
q=p;
}
if(flag)
{
printf("\nElement %d found in the list!!",x);
r=q->left;
r->right=p;
p->left=r;
y=q->info;
freenode(q);
printf("\n%d deleted form the list..!",y);
}
else
printf("\nElement not found in the list..!");
return list;
}
}
main()
{
int ele,c1,c2,x;
nodeptr list;
clrscr();
list=NULL;
while(1)
{
printf("\n1 : Insert node\n2 : Delete node\n3 : Display list\n4 : Exit");
printf("\nEnter choice : ");
scanf("%d",&c1);
switch(c1)
{
case 1:printf("\n1 : Insert from front\n2 : Insert from rear\n3 : Insert after element X\n");
printf("Enter your option : ");
scanf("%d",&c2);
printf("Enter the element to be inserted : ");scanf("%d",&ele);
switch(c2)
{
case 1:list=ins_f(list,ele);
break;
case 2:list=ins_end(list,ele);
break;
case 3:printf("\nEnter the element X : ");scanf("%d",&x);
list=ins_aftx(list,ele,x);
break;
}
break;
case 2:printf("1 : Delete from front\n2 : Delete from rear\n3 : Delete before element X\n");
printf("Enter your option : ");scanf("%d",&c2);
switch(c2)
{
case 1:list=del_f(list);
break;
case 2:list=del_end(list);
break;
case 3:printf("\nEnter the element x : ");
scanf("%d",&x);
list=del_b4x(list,x);
break;
}
break;
case 3:display(list);
break;
default:exit(0);
break;
}
}
getch();
return 0;
}
1 comments:
wish v had internet services in our ds lab!!!!!!! cud hav copied prgrms 4m ths..... cool!!!!!!!
Post a Comment