Saturday, May 9, 2009

PROGRAM FOR IMPLEMENTING DOUBLE LINKED LIST

#include
#include
#include
#define NULL 0
typedef struct dlist
{
int info;
struct dlist *left;
struct dlist *right;
}node;
node *root,*first,*prev,*list,*start,*temp,*end,*list1,*last;
int ele;
void create()
{
printf("Type -1 to stop");
printf("\n Enter element");
scanf("%d",&ele);
while(ele!=-1)
{
first=(node *)malloc(sizeof(node));
first->info=ele;
first->left=NULL;
first->right=NULL;
if(root==NULL)
root=first;
else
{
prev->right=first;
first->left=prev;
}
prev=first;
end=first;
scanf("%d",&ele);
}
}
void print()
{
list=root;
list1=end;
if(root==NULL)
printf("\n List is empty");
else
{
printf("\n contents of list in forward direction\n");
printf("root->");
while(list!=NULL)
{
printf("%d->",list->info);
list=list->right;
}
printf("NULL\n");
}
if(end==NULL)
printf("\n List is empty");
else
{
printf("\n contents of list in backward direction\n");
printf("end->");
while(list1!=NULL)
{
printf("%d->",list1->info);
list1=list1->left;
}
printf("NULL\n");
}
}
void delet()
{
int item;
temp=start=root;
if(root==NULL)
printf("Empty list");
else
{
printf("Enter the element to be deleted\n");
scanf("%d",&item);
if(root->info==item)
{
root=root->right;
root->left=NULL;
}
else
{
while((item!=temp->info)&&(temp!=NULL))
{
start=temp;
temp=temp->right;
}
start->right=temp->right;
temp->right->left=start;
}
}
}
void main()
{
int ch;
root=NULL;
clrscr();
do
{
printf("\n 1->create");
printf("\n 2->deletion");
printf("\n 3->exit");
printf("Enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
print();
break;
case 2: delet();
print();
break;
case 3: exit(0);
break;
}
}while(ch!=3);
getch();
}

No comments:

Post a Comment