Free Online Tutorials ==>> Machine Learning || Python Programming || DBMS || OOPs using C++ || DSA || Java Programming || Linux/Unix || C Programming
Go to your University || Python Lab || DSA Lab || AI & Machine Learning Lab || Linux Lab || OOPs Lab || DBMS Lab || JAVA Lab ||| Free Online Tutorials ||| 
companies for Campus Placement || Logical Reasoning || Quantitative Aptitude || General English || Technical-MCQ and Interview Questions || HR Interview Questions
0 like 0 dislike
50 views

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.8k points)
 
Best answer
So far, we have looked at inserting a node at the beginning of a singly linked list. However, most of the times you will want to be able to insert nodes elsewhere as well. The code written below shows how it is possible to write an insert() function to insert nodes anywhere in the linked lists.

#include <stdio.h>

#include <stdlib.h>

struct Node

{  

int data;  

struct Node* next;

};

struct Node* insert(struct Node* head, int value, size_t position);

void print_list (struct Node* head);

int main(int argc, char *argv[])

{  

struct Node *head = NULL; /* Initialize the list to be empty */

  /* Insert nodes at positions with values: */  

head = insert(head, 1, 0);  

head = insert(head, 100, 1);  

head = insert(head, 21, 2);  

head = insert(head, 2, 3);  

head = insert(head, 5, 4);  

head = insert(head, 42, 2);

print_list(head);  return 0;

}

struct Node* insert(struct Node* head, int value, size_t position) {  size_t i = 0;  struct Node *currentNode;

  /* Create our node */  

currentNode = malloc(sizeof *currentNode);  /* Check for success of malloc() here! */

  /* Assign data */  

currentNode->data = value;

  /* Holds a pointer to the 'next' field that we have to link to the new node.     By initializing it to &head we handle the case of insertion at the beginning. */  

struct Node **nextForPosition = &head;  /* Iterate to get the 'next' field we are looking for.     Note: Insert at the end if position is larger than current number of elements. */  

for (i = 0; i < position && *nextForPosition != NULL; i++)

{      /* nextForPosition is pointing to the 'next' field of the node. So *nextForPosition is a pointer to the next node. Update it with a pointer to the 'next' field of the next node. */      nextForPosition = &(*nextForPosition)->next;  

}

  /* Here, we are taking the link to the next node (the one our newly inserted node should  point to) by dereferencing nextForPosition, which points to the 'next' field of the node  that is in the position we want to insert our node at.  We assign this link to our next value. */  currentNode->next = *nextForPosition;

  /* Now, we want to correct the link of the node before the position of our  new node: it will be changed to be a pointer to our new node. */  

*nextForPosition = currentNode;

return head;

}

void print_list (struct Node* head)

{  /* Go through the list of nodes and print out the data in each node */  

struct Node* i = head;  while (i != NULL)

{    

printf("%d\n", i->data);    

i = i->next;  

}

}
  Realize your learning potential with courses starting at ₹ 420
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Online Free Training:  MACHINE LEARNINGPython Programming | Database Management System(DBMS) | Object Oriented Programming(OOPs) using C++ | Data Structures and Algorithms(DSA) | Java Programming | Linux/Unix | C Programming
Exams: List of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG) | JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Goeduhub
About Us | Contact Us   Social::   |  | 
...