Doubly Linked List

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Doubly Linked List

Types of Lists: Double Linked List


Double linked list
• Pointers exist between adjacent nodes in both directions.
• The list can be traversed either forward or backward.
• Usually two pointers are maintained to keep track of the list, head
and tail.

head tail

A B C

2
Defining a Node of a Double Linked List
Each node of doubly linked list (DLL) consists of three fields:
• Item (or) Data
• Pointer of the next node in DLL
• Pointer of the previous node in DLL
node
Data
prev next

How to define a node of a doubly linked list (DLL)?


struct node
{
int data;
struct node *next; // Pointer to next node in DLL
struct node *prev; // Pointer to previous node in DLL
};

3
Double Linked List
• Doubly linked list is a collection of nodes linked together in a sequential way.
• Doubly linked list is almost similar to singly linked list except it contains two
address or reference fields, where one of the address field contains reference of
the next node and other contains reference of the previous node.
• First and last node of a linked list contains a terminator generally a NULL value,
that determines the start and end of the list.
• Doubly linked list is sometimes also referred as bi-directional linked list since it
allows traversal of nodes in both direction.
• Since doubly linked list allows the traversal of nodes in both direction, we can
keep track of both first and last nodes.

4
Double versus Single Linked List

Advantages over singly linked list


1) A DLL can be traversed in both forward and backward direction.
2) The delete operation in DLL is more efficient if pointer to the node to be deleted
is given.

Disadvantages over singly linked list


1) Every node of DLL Require extra space for an previous pointer.
2) All operations require an extra pointer previous to be maintained.

5
Double Linked List: Insertion at any Position
Steps to insert a new node at nth position in a Doubly linked list.
Step 1: Traverse to N-1 node in the list, where N is the position to insert. Say temp now
points to N-1th node.
Ptr->data=10
Ptr->next=null
ptr->prev=tail
If tail==null
Ptr->prev=null
Head=tail=ptr
Else
Ptr->prev=tail
tail->next=ptr
tail=ptr
Step 2: Create a newNode that is to be inserted and assign some data to its data field.

CS 10001 : Programming and Data


Lecture #05: © DSamanta 6
Structures
Doubly Linked List: Insertion at any Position
Step 3: Connect the next address field of newNode with the node pointed by next address
field of temp node.

Step 4: Connect the previous address field of newNode with the temp node.

7
Doubly Linked List: Insertion at any Position
Step 5: Check if temp.next is not NULL then, connect the previous address field of node
pointed by temp.next to newNode.

Step 6: Connect the next address field of temp node to newNode.

8
Doubly Linked List: Insertion at any Position

Step 7: Final doubly linked list looks like

9
Doubly Linked List: Insertion at any Position
#include <stdio.h>
#include <stdlib.h>

struct node { /* Basic structure of Node */


int data;
struct node * prev;
struct node * next;
}*head, *last;

int main()
{
int n, data;
head = NULL;
last = NULL;

printf("Enter the total number of nodes in list: ");


scanf("%d", &n);
createList(n); // function to create double linked list
displayList(); // function to display the list

printf("Enter the position and data to insert new node: ");


scanf("%d %d", &n, &data);
insert_position(data, n); // function to insert node at any position
displayList();
return 0;
}

10
Doubly Linked List: Insertion at any Position
void createList(int n)
{
int i, data;
struct node *newNode;
if(n >= 1){ /* Creates and links the head node */
head = (struct node *)malloc(sizeof(struct node));
printf("Enter data of 1 node: ");
scanf("%d", &data);
head->data = data;
head->prev = NULL;
head->next = NULL;

last = head;

for(i=2; i<=n; i++){ /* Creates and links rest of the n-1 nodes */
newNode = (struct node *)malloc(sizeof(struct node));
printf("Enter data of %d node: ", i);
scanf("%d", &data);

newNode->data = data;
newNode->prev = last; //Links new node with the previous node
newNode->next = NULL;

last->next = newNode; //Links previous node with the new node


last = newNode; //Makes new node as last/previous node
}
printf("\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}

11
Doubly Linked List: Insertion at any Position
void insert_position(int data, int position)
{
struct node * newNode, *temp;
if(head == NULL){
printf("Error, List is empty!\n");
}
else{
temp = head;
if(temp!=NULL){
newNode = (struct node *)malloc(sizeof(struct node));

newNode->data = data;
newNode->next = temp->next; //Connects new node with n+1th node
newNode->prev = temp; //Connects new node with n-1th node

if(temp->next != NULL)
{
temp->next->prev = newNode; /* Connects n+1th node with new node */
}
temp->next = newNode; /* Connects n-1th node with new node */
printf("NODE INSERTED SUCCESSFULLY AT %d POSITION\n", position);
}
else{
printf("Error, Invalid position\n");
}
}
}

12
Doubly Linked List: Insertion at any Position
void displayList()
{
struct node * temp;
int n = 1;

if(head == NULL)
{
printf("List is empty.\n");
}
else
{
temp = head;
printf("DATA IN THE LIST:\n");

while(temp != NULL)
{
printf("DATA of %d node = %d\n", n, temp->data);
n++;

/* Moves the current pointer to next node */


temp = temp->next;
}
}
}

13

You might also like