Can we insert node in between a linked list? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can we insert node in between a linked list?

9th Nov 2017, 6:09 PM
rishab Jain
rishab Jain - avatar
2 Answers
+ 2
<Skip to the bottom if you don't wanna read about linking and nodes> A general linked list structure in computer science is a data structure that contains a set of nodes in a sequence. The start of the chain is a unqiue node that contains generally the HEAD (1st node), TAIL (last node), and SIZE(amount of nodes in chain, excludes this node). Each other node contains 2 elements: DATA, and NEXT. DATA represents the value that the node is storing. NEXT is a reference or pointer to the next node in the link. if no other link exists after, a null value is given to indicate the end if the link. A good way to imagine it is a chain. Each link represents a node connected to one another to form the entire chain. <Solution> Why am I explaining all this? Well, in order to insert a node into the chain at a specified index, you must travel along the chain to reach it. Then separate the link, reference the previous node to your new node, then reference your node to the next node. Once you reach it, you must be careful. you cannot simply replace the post NEXT to your new node. You'll lose all the information after that node. So be sure to save the reference/pointer to that specific node to your new node's NEXT or a temp variable. If you're still confused, see here: http://www.zentut.com/c-tutorial/c-linked-list/
9th Nov 2017, 8:35 PM
Sapphire
0
thanks for your vital info but can u provide a link for code of the same
10th Nov 2017, 1:46 PM
rishab Jain
rishab Jain - avatar