> For the complete documentation index, see [llms.txt](https://soumyajit4419.gitbook.io/ds-algo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://soumyajit4419.gitbook.io/ds-algo/linked-list/basic-implementation-of-doubly-linked-list.md).

# Basic Implementation of Doubly Linked List

Various implementation using doubly linked list like insertion, deletion etc.

**Node Structure**&#x20;

```
struct node
{
    int data;
    node *prev;
    node *next;
};
```

## Insertion

**Insertion at Head and Insertion at Tail**

```cpp
void insertAtEnd(int val)
{
    node *temp = new node;
    temp->data = val;
    temp->next = NULL;
    temp->prev = NULL;

    if (head == NULL)
    {
        head = temp;
    }
    else
    {
        node *p = head;
        while (p->next != NULL)
        {
            p = p->next;
        }
        p->next = temp;
        temp->prev = p;
    }
}

void insertAtBegin(int val)
{
    node *temp = new node;
    temp->data = val;
    temp->next = NULL;
    temp->prev = NULL;

    if (head == NULL)
    {
        head = temp;
    }
    else
    {
        node *p = head;
        temp->next = p;
        p->prev = temp;
        head = temp;
    }
}
```

**Insertion at any random position**&#x20;

```cpp
void insertAtPos(int pos, int val)
{
    int i = 0;
    int len = getLen();
    if (pos < 0 || pos > len - 1)
    {
        return;
    }
    node *p = head;
    node *q = NULL;
    while (i < pos && p->next != NULL)
    {
        q = p;
        p = p->next;
        i++;
    }
    node *temp = new node;
    temp->data = val;
    temp->next = NULL;
    temp->prev = NULL;
    q->next = temp;
    temp->prev = q;
    temp->next = p;
    p->prev = temp;
}
```

## Deletion

```
```

```
```
