DEV Community

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

Posted on

2 3

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay