Friday, August 28, 2009

Doubly Linked List



#include<stdio.h>



#include<conio.h>



typedef
struct dll



{



int
data;



struct
dll *prev,*next;



}lnode;



typedef
struct



{



int
count,currpos;



lnode *current;



}list;



list l;




void

main()



{




int

ch;




void

insert();




void

delete();




void

display();




void

setposition(int);



l.count=0;



l.currpos=0;



l.current=NULL;




do



{




clrscr();



printf("\nMAIN MENU");




printf("\n=========");




printf("\n\t 1.INSERT");




printf("\n\t 2.DELETE");




printf("\n\t 3.DISPLAY");




printf("\n\t 4.EXIT");




printf("\n\nENTER THE OPTION :->");




scanf("%d",&ch);




switch(ch)




{




case 1:




insert();




getch();




break;




case 2:




delete();




getch();




break;




case 3:




display();




getch();




break;




case 4:




exit(0);




getch();




break;




}



}while(ch!=4);



}




void

insert()



{



int
p,x;



lnode *newnode,*following;



newnode=(lnode*)malloc(sizeof(lnode));



printf("\nENTER
THE POSTION:"
);



scanf("%d",&p);



if(p<1||p>(l.count+1))



printf("\nINVALID POSITION");




else



{



printf("\n ENTER THE VALUE:");



scanf("%d",&x);



newnode->data=x;




if
(p==1)



{



newnode->prev=NULL;




if
(l.count==0)



newnode->next=NULL;



else



{



setposition(1);



newnode->next=l.current;



l.current->prev=newnode;



}



}




else



{



setposition(p-1);



following=l.current->next;



newnode->next=following;



newnode->prev=l.current;



l.current->next=newnode;



if(following!=NULL)




following->prev=newnode;




}




l.current=newnode;




l.currpos=p;




l.count++;




printf("%d IS INSERTED",x);




}




}
























void

delete()



{



int
p,x;



lnode *following;



if(l.count==0)



printf("\n\nLIST
IS EMPTY"
);



else



{



printf("\n
ENTER THE POSITION"
);



scanf("%d",&p);



if(p<1||p>l.count)



printf("\nINVALID POSITION");




else
if(p==1)



{



setposition(1);



x=l.current->data;



following=l.current->next;



if(following!=NULL)




following->prev=NULL;




l.currpos=1;




l.current=l.current->next;




}




else




{




setposition(p);




x=l.current->data;




l.current->prev->next=l.current->next;




if(l.current->next!=NULL)




{




l.current->next->prev=l.current->prev;




l.currpos=p;




l.current=l.current->next;




}




else




{




l.currpos=p-1;




l.current=l.current->prev;




}




}




l.count--;




printf("%d IS DELSETED",x);




}




}




void setposition(int
p)




{




int i;




lnode *current;




if(p<1||(p>l.count))




printf("\nCANNOT FIND");




else if(l.currpos<p)




{




while(l.currpos!=p)




{




l.current=l.current->next;




l.currpos=l.currpos+1;




}




}




else if(l.currpos>p)




{




while(l.currpos!=p)




{




l.currpos=l.currpos-1;




l.current=l.current->prev;




}




}




}




void display()




{




if(l.count==0)




{




printf("\nLIST IS EMPTY");




getch();




exit(0);




}




else




{




lnode *temp;




setposition(1);




temp=l.current;




while(temp!=NULL)




{




printf("\n\t%d \t%d \t%d \t%d",temp->prev,temp->data,temp,temp->next);




temp=temp->next;




}




}




}





No comments:

Post a Comment