Doubly Linked List Initialization and Empty List
You'll learn how to create a brand-new, empty doubly linked list by properly setting up its head and tail pointers.
What Does "Empty" Mean?
An empty doubly linked list is one that contains no nodes yet. Unlike a singly linked list where you only needed a head pointer set to NULL, a doubly linked list maintains two pointers: head and tail. When the list is empty, both must point to nothing.
Think of it like an empty train track: you need markers for where the first car would be (head) and where the last car would be (tail). When there are no train cars yet, both markers point to empty space.
Initialization Steps
To initialize an empty doubly linked list:
- Set
headtoNULL— there's no first node - Set
tailtoNULL— there's no last node
struct Node {
int data;
Node* prev;
Node* next;
};
// Initialize an empty doubly linked list
Node* head = NULL;
Node* tail = NULL;
Both pointers start as NULL because the list literally has nothing in it. This is your baseline state before any insertions happen.
Why Both Pointers?
You might wonder why we need tail when head being NULL already tells us the list is empty. The answer: efficiency for later operations. When you want to insert at the end or access the last element, having a tail pointer saves you from traversing the entire list every single time.
Key Takeaway: An empty doubly linked list requires both head and tail pointers initialized to NULL, representing a list with zero nodes and establishing the foundation for future insertions.