Managing pointers in a doubly linked list in a nutshell
crayoncode
Posted on November 22, 2020
How to code a doubly linked list
Implementing a doubly linked list is all about consistently managing the internal structure of each item's next and previous pointers. At the end it's a set of basic operations that are actually quite easy. Watch this episode of crayoncode and let's write some code together! ⌨️📐⚙️
In Short
A doubly linked list is a least where each item knows its previous and next item.The first item of the list is called head and the last item of the list is called tail.
When adding new data to the end of the list, the current tail needs to point to the new item and the new item needs to point to the current tail. After that is being set up, the new item can become the new tail.
When removing data from an arbitrary position of the list, the points before and after the item being removed need to be rewired. Which means that the previous item's next pointer will be setup to skip the item to be removed and point to the next-next item. Analogously the next item's previous pointer will be setup to also skip the item to be removed and point to the previous-previous item.
Posted on November 22, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.