#include <stdio.h>
struct node
{
int data;
struct node *next;
};
int getdata(int n)
{
struct node *current;
struct node *temp;
struct node *head;
current=(struct node*)malloc(sizeof(struct node));
head=current;
while(1)
{
if(n!=0)
{
current->data=n;
temp=current;
current=(struct node*)malloc(sizeof(struct node));
temp->next=current;
printf("Enter Number(0 to terminate)\n");
scanf("%d",&n);
}
else
{
temp->next=NULL;
break;
}
}
return head;
}
int print(struct node *head)
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
printf("%d %d %d\n",temp,temp->data,temp->next);
temp=temp->next;
}
return;
}
int insert(struct node *head)
{
struct node *ihead,*lhead,*uhead;
uhead=head;
int a,b;
printf("After Which Data=");
scanf("%d",&a);
printf("Enter Element=");
scanf("%d",&b);
ihead=(struct node*)malloc(sizeof(struct node));
ihead->data=b;
while(uhead->data!=a)
{
uhead=uhead->next;
}
lhead=uhead->next;
uhead->next=ihead;
ihead->next=lhead;
}
int search(struct node *head)
{
struct node *shead=head;
int s;
printf("Enter Search Element=");
scanf("%d",&s);
while(1)
{
if(shead->data==s)
{
printf("Found\n");
break;
}
else if(shead->next)
{
shead=shead->next;
}
else
{
printf("Not Found\n");
break;
}
}
}
int main()
{
int n;
printf("Enter Data=");
scanf("%d",&n);
struct node *head;
head=getdata(n);
print(head);
insert(head);
print(head);
search(head);
print(head);
}
0 comments:
Post a Comment