INSERT FUNCTION
/*prototypes of insert & find functions */
list * insert_list(list *);
list * find(list *, int);
/*definition of anyinsert function */
list * insert_list(list *start)
{
list *n, *f;
int key, element;
printf("enter value of new element");
scanf("%d", &element);
printf("enter value of key element");
scanf("%d",&key);
if(start->data ==key)
{
n=(list *)mallo(sizeof(list));
n->data=element; n->next = start; start=n;
}
else
{
f = find(start, key);
if(f == NULL)
printf("\n key is not found \n");
else
{
n=(list*)malloc(sizeof(list));
n->data=element; n->next=f->next; f->next=n;
}
}
return(start);
}
/*definition of a find function */
list * find(list *start, int key)
{
if(start->next->data == key)
return(start);
if(start->next->next == NULL)
return(NULL);
else
find(start->next, key);
}
void main()
{
list *head;
void create(list *);
int count(list *);
void traverse(list *);
head=(list *)malloc(sizeof(list));
create(head);
printf(" \ntraversing the list \n");
traverse(head);
printf("\n number of elements in the list %d \n", count(head));
head=insert_list(head);
printf(" \nafter the insertion of new elementstraverse list \n");
traverse(head);
}
Program: Insertion of an element in a linked list at a particular position