Thursday, August 27, 2009

Doubly Linked List



# include
<stdio.h>



# include <conio.h>



# include <alloc.h>



# include <stdlib.h>



struct link



{



struct
link *next,*pre;



char
name[25];



}*node,*h,*t,*t1;



main()



{



int
ch=1,tmp,op,ip,pno,co,i,fg=0;



char
pname[25],v[25];



node=NULL;



clrscr();



printf("\n\t\t\tDOUBLY
LINKED LIST\n\t\t\t~~~~~~ ~~~~~~ ~~~~"
);



node=(struct
link*)malloc(sizeof(
struct link));



printf("\n\tDoubly
linked list creation "
);



printf("\n\tEnter
a string : "
);



scanf("%s",node->name);



node->pre=NULL;



node->next=NULL;



h=node;



do



{



printf("\n\n1.Insert\n2.Delete\n3.Display\n4.Exit.");



printf("\n\tEnter
your choice : "
);



scanf("%d",&op);



switch(op)



{




case
1:




printf("\n\t\tINSERT OPERATION\n");




printf("\n1. First\n2. Middle\n3.Last");




printf("\n\tEnter your choice : ");




scanf("%d",&ip);




switch(ip)




{




case 1:




t=(struct
link
*)malloc(sizeof(
struct
link));




printf("\n\tEnter the string : ");




scanf("%s",t->name);




t->pre=NULL;




t->next=h;




h=t;




node=t;




break;




case 2:




printf("\n\tEnter the position : ");




scanf("%d",&pno);




node=h;




co=0;




while(node!=NULL)




{




co=co+1;




node=node->next;





}




if(pno>co || pno<=0
|| pno==1)




{




printf("\n\tEnter the correct
position"
);




getch();




}




else




{




node=h;




i=2;




while(i<pno)




{




node=node->next;




i=i+1;




}




t=(struct
link*)malloc(sizeof(
struct link));




printf("\n\tEnter the string
: "
);




scanf("%s",t->name);




t->next=node->next;




t->pre=node;




node->next=t;




node=node->next;




node->pre=t;




}




break;




case
3:




node=h;




while(node->next!=NULL)




node=node->next;




t=(struct
link
*)malloc(sizeof(
struct
link));




printf("\n\tEnter the string : ");




scanf("%s",t->name);




t->next=node->next;




t->pre=node;




node->next=t;




node=node->next;




node->pre=t;




break;




default :




printf("\n\n\t\tInvalid Choice.");




getch();




}




break;




case
2:




printf("\n\tDeletion");




printf("\n\tEnter the value to be
deleted : "
);




scanf("%s",v);




node=h;




while(node!=NULL)




{




if(strcmp(node->name,v)==0)



{




fg=1;




if(node!=h && node->next!=NULL)




{




t->next=node->next;




node=node->next;




node->pre=t;




}




else
if
(node==h && node->next!=NULL)




{




node=node->next;




node->pre=NULL;




h=node;




}




else
if
(node==h && node->next==NULL)




{




node=NULL;




h=node;




}




else
if
(node!=h && node->next==NULL)




{




t->next=NULL;




t=node;




}




printf("\n\t\tGiven value is deleted
"
);




getch();




break;



}




t=node;




node=node->next;




}




if(fg==0)




{



printf("\n\n\t\tThat value not in the list ");



getch();




}




break;




case
3:




printf("\n\tItems in the List\n\n\t\t");




node=h;




if(node==NULL )




printf("\n\tThe list is empty");




else




{




while(node->next!=NULL)




{




printf(" %s --> ",node->name);




node=node->next;




}




if(node->next==NULL)



printf("%s",node->name);




}




getch();




break;




case
4:




clrscr();




exit(0);




default
:




printf("\n\n\n\t\t\tInvalid Choice.\n");




getch();



}



}while(ch!=4);



}





No comments:

Post a Comment