A Linked List is a linear data structure which consists of a group of nodes. Unlike an array, it has elements that are stored in random memory locations.
Each node contains two fields:
- data stored at that particular address.
- Pointer which contains the address of the next node.
The last node of the Linked list contains a pointer to null to represent the termination of the linked list. Generally, we call the first node as Head node and last node as Tail node in Linked List.
Why Linked List Over Array?
Array contains following limitations:
- The size of an array is fixed. We must know the size of the array at the time of its creation, hence it is impossible to change its size at runtime.
- Inserting a new element in an array of elements is expensive because we need to shift elements to create room for new elements to insert.
- Deleting an element in an array is also expensive as it also takes shifting of elements in the array.
Advantages of Linked List:
- Insertion and deletion operations can be implemented very easily and these are not costly as they do not require shifting of elements.
- They are dynamic in nature. Hence, we can change their size whenever required.
- Stacks and queues can be implemented very easily using Linked Lists.
Disadvantages of Linked List:
- Random access of an element is not possible in Linked Lists, we need to traverse Linked List from starting to search an element into it.
- It is relatively slow to process in comparison to an Array.
- Since node of a Linked List contains both data and pointer to the next node, hence extra memory is required to store the pointer of each node.
Types of Linked List:
There are three types of Linked Lists:
- Singly Linked List
- Circular Linked List
- Doubly Linked List
Singly Linked List:
A Singly Linked List contains a node which has both the data part and pointer to the next node. The last node of the Singly Linked List has a pointer to null to represent the end of Linked List. Traversal to previous nodes is not possible in singly Linked List i.e We can not traverse in a backward direction.
Circular Linked List:
Circular Linked List is similar to singly Linked List but the last node of singly Linked List has a pointer to node which points to the first node (head node) of Linked List.
Doubly Linked List:
Doubly Linked List contains a node which has three entries: (1) data part, (2) pointer to the next node and (3) pointer to the previous node. We can traverse in both forward and backward directions in doubly Linked Lists.
Comments
Post a Comment