Linked List
A linked list is a linear data structure and stores it at the non-contiguous memory location. Non-contiguous memory location means that it doesn't guarantee that each data element will reside next to each other in the memory. There are chances that one memory location address is 1000 and another one is 1200; then the immediate question comes into our mind. How do they actually know which data will be next? Are they using some kind of magic behind the scene?
Let's understand the linked list with the carousel/slider example which we used many times on many websites. You have analyzed the carousel and are working on the number of images that put it into some sequence and rotate one by one. They have one common characteristic, that is, if we want to look for the last image in the carousel then we have to go one by one till the last. There is one start point, which we usually call head and one termination point which we usually call a tail. Each Image can be considered as a node in a linked list term. Each node contains data and the address of the next node.
NOTE:- X represents null means that there is no more node that exists after it.
A singly linked list traverses only in a forward direction. Each node consists of the value and the address of the next node. We can traverse only from the start to the end node but not vice versa. If we lose the start node then the complete list will be lost.
A Circular doubly linked list is more powerful than any other linked list but it stores previous and next node reference on it. We can transverse in any of the directions. We can traverse reverse also.
If and only if you have such a scenario then only you should use a Doubly Linked List otherwise we should avoid using it.
Let's understand the linked list with the carousel/slider example which we used many times on many websites. You have analyzed the carousel and are working on the number of images that put it into some sequence and rotate one by one. They have one common characteristic, that is, if we want to look for the last image in the carousel then we have to go one by one till the last. There is one start point, which we usually call head and one termination point which we usually call a tail. Each Image can be considered as a node in a linked list term. Each node contains data and the address of the next node.
NOTE:- X represents null means that there is no more node that exists after it.
Singly Linked List
Circular Singly Linked List
A Circular singly linked list is almost the same as a singly linked list but the last node is pointing to the start node of a linked list. We have to be very careful, while writing a traversing logic into the circular linked list. If we make a mistake and miss the base condition that the node's next address must not equal the start node's address then traversing will run infinite times.
Doubly Linked List
A Doubly linked list is more powerful than the singly linked list in terms of traversing a linked list backward and forward. Each node contains two references of the next and previous node with the value of itself. Still, we can't directly traverse to the last node.
Circular Doubly Linked List
A Circular doubly linked list is more powerful than any other linked list but it stores previous and next node reference on it. We can transverse in any of the directions. We can traverse reverse also.
If and only if you have such a scenario then only you should use a Doubly Linked List otherwise we should avoid using it.
Comments
Post a Comment