Skip to main content

Posts

Showing posts from March, 2015

Insert a node at begining of linked list

Some how students find linked list very confusing. It is not. Let us say you have many nodes in the linked list. And each of this node is dynamically allocated. And hence you can not access them easily. So what is done is each node is connected to previous node with the help of a pointer. Any node will always have a pointer which has address of next node. And thus, if you just have address of first node, you can visit second, then third, then fourth and every node of list.  How do we add new nodes to the list? We can add them at the end. Appending a node to the end of a list is quite simple. Just go till last node, set the pointer of the last node to address of newly created node. That's all. But what if you want to add a node as the first node? When this newly added node is made is  first node, first node should become second and so on.  But how do you modify first node pointer or head? Since a function modifying head pointer gets only a copy of head, even if it     changes head,