Monday, January 18, 2021

Basics of Linked List

Linked List

Its a data structure - A way to organize and store data in memory. 

Remember how arrays organize and store data ? Its done sequentially , lets say we have 10 items in the array , the data is stored sequentially starting from 0th index till 9th . In similar , a random block in the memory[RAM].


 

 

 

 



Remember - We declare an array in advance and the memory gets allocated in STACK. That will be used to manage/save the data that will be used by the program. This is statically allotted space and you get the asked space even if you use or not. 

Imagine - You reserve  5 seats on a bus that cannot be cancelled . Even if there are 5 members or 2 on the day of travel , you pay for what you asked for. Expensive right ? But that's how it works ! 

Now Lets move to the Linked List .

Imagine a short travel , without a reservation system - We get into a bus , we pay for a single seat . Now the next day i take my friend along with me , i take 2 seat , i pay for 2 .

In the same manner 

While creating a Linked list , the basic blocks are Pointers . Pointer represent a variable that points to a memory location in the RAM .






Imagine the pointer as the door that allows us to get into the bus where we get seats for what we pay for.
Thus we create HEAD [just a variable name] pointer that represent the beginning of the linked list.


We save the first data at a reserved memory location and then point that via HEAD , along with that we have another variable called next.  Next indicate the next place in memory where data is stored . and thus the list grows on.

Anytime you need to add new data to linked list, you ask a special place in memory called HEAP , via the help of malloc() function. It allocates a new block when ever required. 













I shall be adding Notes on Linked list operation via new blogs as next node :) Imaging this as the starting node - Our HEAD node . Drop a comment for more queries.

No comments:

Post a Comment