Done By: Prof.Dhumane A.V. VIIT, Pune
*/
#include
#include
#include
#include
struct node
{
int data;
struct node*next;
};
struct node*add_node(struct node*head)
{
struct node*trav,*temp;
if(head==NULL)
{
head=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the data ");
scanf("%d",&head->data);
head->next=NULL;
}
else
{
trav=head;
while(trav->next!=NULL)
{
trav=trav->next;
}
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the data ");
scanf("%d",&temp->data);
temp->next=NULL;
trav->next=temp;
}
return head;
}
void display(struct node*head)
{
struct node*trav;
printf("\n");
for(trav=head;trav!=NULL;trav=trav->next)
{
printf("[%d]->",trav->data);
}
}
void search(struct node*head)
{
struct node*trav;
int KEY, found=0;
printf("\nEnter the KEY to be searched ");
scanf("%d",&KEY);
while(trav!=NULL)
{
if(trav->data==KEY)
{
printf("\n KEY found !!!");
found=1;
break;
}
trav=trav->next;
}
if(found==0)
{
printf("\nKEY not found !!!");
}
}
struct node*delete_node(struct node*head)
{
int KEY,found=0;
struct node*trav;
printf("\nEnter the KEY to be deleted ");
scanf("%d",&KEY);
if(head->data==KEY)
{
head=head->next;
printf("\nKEY deleted ..");
}
else
{
trav=head;
while(trav->next!=NULL)
{
if(trav->next->data==KEY)
{
trav->next=trav->next->next;
printf("\nKEY deleted ..");
found=1;
break;
}
trav=trav->next;
}
if(found==0)
{
printf("\nKEY not present to be deleted ..");
}
}
return head;
}
void main()
{
struct node*head=NULL,*trav,*temp;
int choice;
clrscr();
while(1)
{
printf("\n1.Add");
printf("\n2.Delete");
printf("\n3.Search");
printf("\n4.Display");
printf("\n5.Exit");
printf("\nEnter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1: head=add_node(head);
break;
case 2: head=delete_node(head);
break;
case 3: search(head);
break;
case 4: display(head);
break;
case 5:
exit(0);
}
}
}
No comments:
Post a Comment