Categories: Data Structure

Understand Doubly Linked List Data Structure

In a doubly linked list, we define a linked list in which each node keeps an explicit reference to the node before it and a reference to the node after it.

These lists allow a greater variety of O(1)-time update operations, including insertions and deletions.

We continue to use the term “next” for the reference to the node that follows another.

We have a new term “prev” for the reference to the node that precedes it.

Sentinal Node

  • We add special nodes at both ends of the list.
  • a header node at the beginning of the list a trailer node at the end of the list.
  • These “dummy” nodes are known as sentinels (or guards)

Inserting and Deleting

Every insertion into our doubly linked list representation will take place between a pair of existing nodes.

When a new element is inserted at the front of the sequence, we will simply add the new node between the header and the node that is currently after the header.

  • (a) before the operation
  • (b) after creating the new node
  • (c) after linking the neighbors to the new node

Insertion of a Node to Front

  • (a) before the operation
  • (b) after creating the new node
  • (c) after linking the neighbors to the new node

Deletion of a Node

The two neighbors of the node to be deleted are linked directly to each other. As a result, that node will no longer be considered part of the list and it can be reclaimed by the system.

Because of sentinels, the same implementation can be used when deleting the first or the last element of a sequence.

  • (a) before the removal
  • (b) after linking out the old node
  • (c) after the removal

Recommended:

Understand The Singly Linked List and its Operation

Implementation of Singly Linked List in Python

Follow Me ❤😊

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Recent Posts

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

2 days ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 months ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

2 months ago