Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Function for inserting an item into a linked list

Posted By: Isabella Brown     Category: C Programming     Views: 6911

Write a function to insert a given item before a specified node known as key node.

Code for Function for inserting an item into a linked list in C Programming

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); elseif(list->next->next == NULL) /* end */
return(NULL); else find(list->next, key); }
  
Share: 


Didn't find what you were looking for? Find more on Function for inserting an item into a linked list Or get search suggestion and latest updates.

Isabella Brown
Isabella Brown author of Function for inserting an item into a linked list is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!