Have u ever played those childish games in which few kids used to hold hands and run around in a line, maybe in shape of a train. Ever thought why that train was able to move.Wasnt ie because of the order of holding hands. What would have happened of the fourth kid said “Wait, I will not hold of the third and fifth guy but third and sixth”. Would that train survive? And what used to happen when a kid in between had to leave. Your answer would be “Duh, thats simple.Join the link. The guy before and the gal after him should join hands”. And what if a new guy came:”Come on, Dont mock my intelligence. Either fit him in between or at one of the ends”. So, whats the most important point abt this human chain.I am pretty sure all of you would say :”If at all points, it is ensured that if you start at one end of the chain and reach the other hand by just following the hands, everything is fine”
Before you start screaming on me, understand this “You have just told the condition of existence and operation of a linked list” This is all there to linked lists(more specifically Singly Linked Lists). Just ensure that at any point of time , the link is traversable from one end to another and u can use the list. Now, you would have understood why it is called linked list too. Coz each kid (called a node) is linked to the next kid. And what do you need to modify the lists, change the links.
In Coding paradigm, each kid is called a node. Going thru the list is called Traversal. Adding a kid to the list is called, well, Adding. Removing the kid is called Deletion. There are few new terms though: Head Pointer, Tail Pointer.
If u imagine the chain as a dragon, the the begining of the chain (Dragon’s head), thats the Head Pointer. The end the Tail Pointer. The Head Pointer is a very imp pointer , because it lets you access the linked list. The Tail Pointer is the end pointer, so that you know that there are no more nodes to traverse.
In some cases, head node and tail nodes contain valid data, In some cases, they dont . Generally, tail node is set as NULL.
(Q: whats the benefit/disadvantage of storing data in the head and tail nodes? Hint: For any node,how will you know that it is tail Node. Think maan )
To put the operations in more geeky language, here is the pseudo-code for basic operations
In the foll pseudo codes, Head Pointer means the first node containing valid data.
Traversal
Start from Head Pointer
Get_next:
Get the Pointer of next node
Is the value of next node pointer equal to the Tail Pointer
If yes, Then Stop Traversing(Goto Exit)
Else, Continue Traversing (Goto Get_next)
Exit: Done
Search for a entry
Start from Head Pointer
Get_next:
Get the data of the node
Is the data of the node is the search value
If yes: return the pointer of current node
Else continue,
Get the Pointer of next node
Is the value of next node pointer equal to the Tail Pointer
If yes, Then Stop Traversing(Goto Exit)
Else, Continue Traversing (Goto Get_next)
Exit: Done
Lets take the complex case now, addition after a particular entry .(Lets assume it is in middle)(A node containing data NEW to be added after a node containing OLD)
Addition :
Search and get the pointer of the node containing OLD (Lets call it OLD_NODE)
In a temp node pointer, save the next node pointer of the node containing OLD (OLD_NEXT)
set the next pointer of OLD_NODE to the new node’s pointer
Make the next pointer of the new node to OLD_NEXT
Now, think what we have done.We have ensured that the link’s order is maintained.
Q: What change will be needed to above for handling adding at the beginning and adding at the end?
Hint: Those two are special pointers. The head and Tail pointer I mean. So, their integrity should be preserved.
Big HW: Write deletion pseudo code. Would have written but this keyboard is killing me.
Before signing off, where are linked lists used: Ans: practically for everywhere, be it Stacks, Queues, Trees, all use some form of the Linked List .