well, Here we give a linked list program in c with insertion, deletion, display and traversal.
The program is given below-
/*Insertion ,Deletion, Display and Traversal in Binary Search Tree*/
# include
# include
struct node
{
int info;
struct node *lchild;
struct node *rchild;
}*root;
main()
{
int choice,num;
root=NULL;
while (1)
{
printf("\n");
printf( "1.Insert\ n");
printf("2.Delete\n");
p rintf ("3.Inorder Traversal\n");
printf("4.Preo rder Traversal\n");
printf("5.Post order Traversal\n");
printf("6.Disp lay\n ");
printf("7.Quit\n");
prin tf("E nter your choice : ");
scanf("%d",&choice);
s witch (choice)
{
case 1:
printf("Enter the number to be inserted : ");
scanf("%d",&num);
insert (num) ;
break;
case 2:
printf("Enter the number to be deleted : ");
scanf("%d",&num);
del(nu m);
break;
case 3:
inorder(root);
break;
ca se 4:
preorder(root);
break;
c ase 5:
postorder(root);
break;
case 6:
display(root,1);
break;
case 7:
exit();
default:
printf( "Wron g choice\n");
}/*End of switch */
}/*End of while */
}/*End of main()*/
find(int item,struct node **par,struct node **loc)
{
struct node *ptr,*ptrsave;
if(root==NUL L) /*tree empty*/
{
*loc=NULL;
*par=N ULL;
return;
}
if(item==roo t->info) /*item is at root*/
{
*loc=root;
*par=NU LL;
return;
}
/*Initialize ptr and ptrsave*/
if(iteminfo)
ptr=r oot-> lchild;
else
ptr=root->rchil d;
p trsave=root;
while(ptr!=NUL L)
{
if(item==ptr->info)
{ *loc=ptr;
*par=ptrsave;
retu rn;
}
ptrsave=ptr;
if(itemi nfo)
ptr= ptr->lchild;
else
ptr=ptr->r child ;
}/*End of while */
*loc=NULL; /*item not found*/
*par=ptrsave;
}/*End of find()*/
insert(int item)
{ struct node *tmp,*parent,*location;
find( item, &parent,&location);
if(locati on!=N ULL)
{
printf("Item already present");
return;
}
tmp= (stru ct node *)malloc(sizeof(struct node));
tmp->info=item;
tmp- >lchi ld=NULL;
tmp->rchild=NULL;
if(p arent==NULL)
root=tmp;
else
if(i teminfo)
parent->lchild=tmp;
else
parent->rchild=tmp;
}/ *End of insert()*/
del(int item)
{
struct node *parent,*location;
if(root==N ULL)
{
printf("Tree empty");
return;
}
find(i tem,& parent,&location);
if(locatio n==NU LL)
{
printf("Item not present in tree");
return;
}
if(loca tion- >lchild==NULL && location->rchild==NULL)
case_ a(par ent,location);
if(location->l child !=NULL && location->rchild==NULL)
case_ b(par ent,location);
if(location->l child ==NULL && location->rchild!=NULL)
case_ b(par ent,location);
if(location->l child !=NULL && location->rchild!=NULL)
case_ c(par ent,location);
free(location) ;
}/ *End of del()*/
case_a(struct node *par,struct node *loc )
{
if(par==NULL) /*item to be deleted is root node*/
root=NULL;
else
if(l oc==p ar->lchild)
par->lchild=NULL;
els e
par->rchild=NULL;
}/*End of case_a()*/
case_b(struct node *par,struct node *loc)
{
struct node *child;
/*Initialize child*/
if(loc->lchild!=NULL) /*item to be deleted has lchild */
child=loc->lchild;
else /*item to be deleted has rchild */
child=loc->rchild;
if(p ar==N UL
Answered by
Romi
, an ibibo Master,
at
6:39 PM on June 20, 2008