Structure of a Doubly-Linked List using sentinel nodes

The structure of each node and the creation of the new node in a doubly-linked list with sentinel nodes are the same as the simple doubly-linked list, as shown below.

C++
// Each node contains the data,
// previous node pointer and
// the next node pointer
struct node {
    int data;
    struct node* next;
    struct node* pre;
};

// To create a node, first define the node pointer
// and than assign the memory required by the node
// and return the node
struct node* createnode()
{
    struct node* t;
    t = (struct node*)malloc(sizeof(struct node));
    return (t);
}
Java
// Each node contains the data,
// previous node pointer and
// the next node pointer
static class Node {
    int data;
     node next;
     node pre;
}

// To create a node, first define the node pointer
// and than assign the memory required by the node
// and return the pointer
Node createnode()
{
    Node node = new Node();
 retutn node; 
}
Python
# Each node contains the data,
# previous node pointer and
# the next node pointer
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
        self.pre = None

# To create a node, first define the node pointer
# and than assign the memory required by the node
# and return the pointer
def createNode():
    return Node(None)
C#
// Each node contains the data,
// previous node pointer and
// the next node pointer
public static class Node {
    public int data;
    public Node next;
    public Node pre;
}

// To create a node, first define the node pointer
// and than assign the memory required by the node
// and return the pointer
public static Node CreateNode()
{
    Node node = new Node();
    return node;
}

// This code is contributed by akashish__
Javascript
// Each node contains the data,
// previous node pointer and
// the next node pointer
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
        this.pre = null;
    }
}

// To create a node, first define the node pointer
// and than assign the memory required by the node
// and return the node
function createNode() {
    return new Node();
}

Doubly Linked List using Sentinel Nodes

In the case of the simple doubly linked list, if we have to perform the insertion or deletion operation at the starting of the doubly linked list, end of the doubly linked list, or in between the starting and end nodes for each, we require to check different condition that makes the algorithm complex so to solve this problem we can use doubly linked list along with the sentinel nodes.

Similar Reads

What is a sentinel node?

Sentinel nodes are specially designed nodes that do not hold or refer to any data of the doubly linked list (that data structure)....

Advantage of Sentinel node:

The most significant advantage of using the sentinel nodes in the doubly linked list:...

Structure of a Doubly-Linked List using sentinel nodes:

The structure of each node and the creation of the new node in a doubly-linked list with sentinel nodes are the same as the simple doubly-linked list, as shown below....

Operations on a doubly-linked list using Sentinel Nodes:

The common operations in a doubly-linked list are:...