In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked records called Nodes. Each Node contains two fields, called Links, that are references to the Previous and to the Next Node in the sequence of Nodes as well as field named Data.
For every Linked List we have something called Head which marks the starting of a list. So we have two main structures namely
Why we need a new structure for HEAD variable ?
Just for convenience I decided to have HEAD its own structure. You can even use the Node structure.
Node
Every Node in a Doubly Linked List has three main members namely
As their names say
- PREV - holds the memory location of the Previous Node in the List. If there are none we point towards NULL. For the First Node in the List PREV points to NULL.
- NEXT - holds the memory location of the Next Node in the List. If there are none we point towards NULL. For the Last Node in the List NEXT points to NULL.
- DATA - In simple words it holds Data. In our case it holds the memory location to the actual data to be held by the Node.
typedef struct node
{
struct node *prev;
void *data;
struct node *next;
}NODE;
Head
Head acts as the "head" of the List. Head structure has two members namely
- LENGTH - holds the count of number of Nodes in the List.
- FIRST - hold the memory location of the first Node in the List. If the List is EMPTY it points to NULL.
typedef struct head
{
unsigned int length;
struct node *first;
}HEAD;
NOTE: Our Head structure doesn't contain any pointer to the Tail of the List. Eventhough its a best way to include a pointer to Tail Node we decided to cover that implementation in Circular Doubly Linked List.