0% found this document useful (0 votes)
15 views

Insert Function

The document describes a function for inserting an item into a linked list. It takes a head node as a parameter and allocates memory for a new node, sets the number value, and inserts it into the list before the specified key node.

Uploaded by

Sai Siva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Insert Function

The document describes a function for inserting an item into a linked list. It takes a head node as a parameter and allocates memory for a new node, sets the number value, and inserts it into the list before the specified key node.

Uploaded by

Sai Siva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

/* Write a function for inserting an item into a linked list */

node *insert(node *head)


{
node *find(node *p, int a);
node *new; /* pointer to new node */
node *n1; /* pointer to node preceding key node */
int key;
int x; /* new item (number) to be inserted */
printf("Value of new item?");
scanf("%d", &x);
printf("Value of key item ? (type -999 if last) ");
scanf("%d", &key);
if(head->number == key) /* new node is first */
{
new = (node *)malloc(size of(node));
new->number = x;
new->next = head;
head = new;
}
else /* find key node and insert new node */
{ /* before the key node */
n1 = find(head, key); /* find key node */
if(n1 == NULL)
printf("\n key is not found \n");
else /* insert new node */
{
new = (node *)malloc(sizeof(node));
new->number = x;
new->next = n1->next;
n1->next = new;
}
}
return(head);
}
node *find(node *lists, int key)
{
if(list->next->number == key) /* key found */
return(list);
else
if(list->next->next == NULL) /* end */
return(NULL);
else
find(list->next, key);
}

You might also like