DEV Community

Cover image for Managing pointers in a doubly linked list in a nutshell
crayoncode
crayoncode

Posted on

Managing pointers in a doubly linked list in a nutshell

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.

Structure of a double linked list.

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.

Adding an item to a doubly linked list

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.

Removing an item from a doubly linked list

Top comments (0)