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

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free