Friday, April 17, 2009

DOUBLE LINKED LIST

#include
#include
#include
#include
struct node
{
int data;
struct node *ptr;
};
typedef struct node NODE;
NODE *first=NULL,*last=NULL;
NODE *nn,*temp,*temp1,*temp2;
main()
{
int ch,r=1;
clrscr();
while(r)
{
printf("enter the choice\n");
printf("1.entering from the front\n");
printf("2.entering from the end\n");
printf("3.deleting from the front\n");
printf("4.deleting from the end\n");
printf("5.display\n");
printf("6.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:enterf();
break;
case 2: entere();
break;
case 3:delf();
break;
case 4:dele();
break;
case 5:display();
break;
case 6:return;
}
printf("do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&r);
}
}
enterf()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->ptr=nn;
if(first==NULL)
{
first=last=nn;
}
else
{
last->ptr=nn;
nn->ptr=first;
first=nn;
}
}

display()
{
if(first==NULL)
printf("empty list\n");
else if(first->ptr==first)
printf("%d->",first->data);
else
{
temp=first;
while(temp!=last)
{
printf("%d->",temp->data);
temp=temp->ptr;
}
printf("%d->",last->data);
}
}

entere()
{
int item;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the item to be inserted\n");
scanf("%d",&item);
nn->data=item;
nn->ptr=nn;
if(first ==NULL)
{
first=last=nn;
}
else
{
last->ptr=nn;
nn->ptr=first;
last=nn;
}
}
delf()
{
if(first==NULL)
printf("empty list\n");
else if(first->ptr==first)
{
printf("deleted element =%d",first->data);
free(first);
first=last=NULL;
}
else
{
temp=first;
first=first->ptr;
printf("deleted element=%d",temp->data);
free(temp);
last->ptr=first;
}
}
dele()
{
if(first==NULL)
printf("empty list\n");
else if(last->ptr==last)
{
printf("deleted element=%d",first->data);
free(first);
first=last=NULL;
}
else
{
temp1=first;
while(temp1->ptr!=last)
{
temp1=temp1->ptr;
}
printf("deleted element=%d",last->data);
free(last);
last=temp1;
last->ptr=first;
}
}

No comments: