Ds-Intro Linked List
Ds-Intro Linked List
Linked list data structure are a collection of data elements in linear form.
A linked list is a linear data structure used for storing collections of data in the form of
nodes. Each node in a linked list store two elements – data and address of next node.
The linked list starts with a HEAD which denotes the starting point or the memory location of first
node.
Linked list ends with the last node pointing to NULL value.
Unlike array the elements are not stored in contiguous memory locations, but are stored in
random location.
Random allocation of memory location helps to add any number of elements to the lined list.
A linked list is a chain of nodes, and each node has the following parts:
memory allocation
Elements of the arrays are Elements present in Linked Lists are dependent on
independent of each other. each other.
Arrays are comparatively easier to Linked Lists are hard to implement as they are prone to
implement memory leaks, segmentation f aults, etc.
Complexity:Access an element –
Complexity:Access an element – O(n)Insert an
O(1)Insert an element at the beginning
element at the beginning – O(1)Insert an element at
– O(n)Insert an element at the end –
the end – O(n)
O(n)
In general, linked list means a Singly Linked Lists. Every node contains some data and a
pointer to the address of the next node of the same data type in sequence.
NOTE: Singly Linked Lists are unidirectional. i.e. allows the t raversal of data in a single
direction.
As the name suggests, the Doubly Linked List are two-way linked lists containing pointers to
the previous and the next node in the sequence. So, as you can refer to below, you can t
raverse forward and backward in this type of Linked List.
Circular Linked Lists t raverse in the form of a circle. So, you can begin at any node and t
raverse the list in either the forward or backward direction until you reach the same node
again. The last node of the Circular Linked List contains the pointer to the f irst node of the
list . Hence, there is no start or endpoint of this type of list .
Note: A Circular Linked List can be Singly if the next pointer of the last
Traversing a linked list simply means accessing the nodes of the linked list in order to perform
some processing on them.
For t raversing the linked list , we will make use of another pointer variable ‘PTR‘ which points
to the node that is currently being accessed. Before we proceed let’s understand the algorithm
behind it .
Algorithm:
Step 1: [INITIALIZE] SET PTR = HEAD
Step 2: Repeat Steps 3 and 4 while PTR != NULL
Step 3: Apply Process to PTR DATA
Step 4: SET PTR = PTR NEXT
[END OF LOOP]
Step 5: EXIT
In this algorithm,
Step 1: Initialize PTR with the starting address of HEAD. So now, PTR points to the f irst node of the
linked list.
Step 2: Execute a loop which is repeated till PTR processes the last node or it encounters NULL.
Step 4, Move to the next node by making the PTR variable point to the address of NEXT node
Let’s understand it better with an example of Counting the elements in a linked list.
To count the elements in a list , we will t raverse each node of the list and while t
raversing every individual node, increment the counter value by 1. Once we reach NULL, that
is, when all the nodes of the linked list have been t raversed, print the f inal value of the
counter.
Algorithm:
NOTE: Time Complexity: O(n), for scanning/ t raversing the list of size n
Inserting a new node bef ore the HEAD (at the beginning)
Inserting a new node af ter the last node (at the end) Inserting a
In this case a new node is inserted before the current HEAD node. We need to modify only
one pointer – New node next pointer. This can be done in following two steps:
Update the next pointer of the new node to point to the current HEAD.
Update the HEAD pointer to point to the new node.
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL - > NEXT
Step 4: SET NEW_NODE - > DATA = VAL Step
5: SET NEW_NODE - > NEXT = HEAD Step 6:
SET START = NEW_NODE
Step 7: EXIT
Step 1 involves checking if memory is available for the new node. If the f ree memory is
depleted, an OVERFLOW message is printed. Otherwise, if a f ree memory cell is available,
we allocate space for the new node. Set its DATA part with the given VAL and the next part
is init ialized with the address of the f irst node of the list , which is stored in HEAD. Because
the new node was inserted as the f irst node in the list , it is now known as the HEAD node,
and the HEAD pointer variable now contains the address of the NEW NODE.
NOTE: Time Complexity: O(1), for inserting an element at the beginning of linked list
In this case, we need to modify two pointers – last node next pointer and new node next
pointer
Let’s implement adding a new node at the end of Linked List in C Programming
Copy code
NOTE: Time Complexity of inserting an element at the end of the linked list is O(n).
Inserting a Node in the middle of the Linked List
Let’s say we are given a position to insert the new node, so in this case we would have to
modify two next pointers.
If we want to add an element at position 3, then we need to traverse 2 nodes to insert a new node.
New node will point to the next node of the position where we want to add this node
intermediate node
Move the head node pointer to the next node and delete the temporary node.
Traverse the list and while traversing maintain the previous node address. By the time we reach the end
of list, we will have two pointers – one pointing to tail node and other pointing to the node bef ore.
Linked Lists provide an optimized way to insert, delete, and update the information along
with storing the data, at the cost of extra space required for storing the address of the next
node. The t ime and space complexities of Linked Lists are as follows:
Average Scenario Worst Scenario
Now that you know what Linked Lists are and their types, let us understand their various
operations. With this, we end this article on Introduction to Linked Lists and how to
implement it . We hope you found it informative. Now you can practice our popular linked list
questions asked in interview. In our next article we will learn about the t ree data structure.
Have you recently completed any online course/certification? Tell us what liked or disliked in
the course. It might help others to choose a right course for them.
Click here to submit its review with Shiksha Online.
FAQs