0% found this document useful (0 votes)
127 views10 pages

Doubly Linked List

A doubly linked list is a linked list where each node contains a link to both the next and previous nodes. This allows traversal in both directions. Each node stores data and two links - one to the previous node and one to the next. The first and last nodes have their previous/next links point to NULL. A doubly linked list class is defined with methods to add nodes to the front, after/before a given node, and end of the list. It also contains methods to delete nodes and traverse the list in the forward and backward directions.

Uploaded by

SANA MATEEN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views10 pages

Doubly Linked List

A doubly linked list is a linked list where each node contains a link to both the next and previous nodes. This allows traversal in both directions. Each node stores data and two links - one to the previous node and one to the next. The first and last nodes have their previous/next links point to NULL. A doubly linked list class is defined with methods to add nodes to the front, after/before a given node, and end of the list. It also contains methods to delete nodes and traverse the list in the forward and backward directions.

Uploaded by

SANA MATEEN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Doubly Linked List

Doubly linked list is a type of linked list in which each node apart from storing its data has two
links. The first link points to the previous node in the list and the second link points to the next
node in the list. The first node of the list has its previous link pointing to NULL similarly the
last node of the list has its next node pointing to NULL.

The two links help us to traverse the list in both backward and forward direction. But storing an
extra link requires some extra space.Now we define our class Doubly Linked List. It has the
following methods:

 add_front: Adds a new node in the beginning of list


 add_after: Adds a new node after another node
 add_before: Adds a new node before another node
 add_end: Adds a new node in the end of list
 delete: Removes the node
 forward_traverse: Traverse the list in forward direction
 backward_traverse: Traverse the list in backward direction

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. 
3) We can quickly insert a new node before a given node. 
In singly linked list, to delete a node, pointer to the previous node is needed. To get this
previous node, sometimes the list is traversed. In DLL, we can get the previous node using
previous pointer

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. For example, in insertion,
we need to modify previous pointers together with next pointers.

Insertion 
A node can be added in four ways 
1) At the front of the DLL 
2) After a given node. 
3) At the end of the DLL 
4) Before a given node.

struct node
{
int data; // Data
node *prev; // A reference to the previous node
node *next; // A reference to the next node
};
class Doubly_Linked_List
{
node *front; // points to first node of list
node *end; // points to last node of list
public:
Doubly_Linked_List()
{
front = NULL;
end = NULL;
}
void add_front(int );
void add_after(node* , int );
void add_before(node* , int );
void add_end(int );
void delete_node(node*);
void forward_traverse();
void backward_traverse();
};
1) Add a node at the front:
The new node is always added before the head of the given Linked List. And newly
added node becomes the new head of DLL.

I. The prev pointer of first node will always be NULL and next will point to front.


II. If the node is inserted is the first node of the list then we make front and end point to this
node.
III. Else we only make front point to this node

void Doubly_Linked_List :: add_front(int d)


{
// Creating new node
node *temp;
temp = new node();
temp->data = d;
temp->prev = NULL;
temp->next = front;

// List is empty
if(front == NULL)
end = temp;

else
front->prev = temp;

front = temp;
}

2) Add a node after a given node.:


We are given pointer to a node as prev_node, and the new node is inserted after the
given node
Let’s say we are inserting node Y after X.
Then Y’s prev pointer will point to X and Y’s next pointer will point the node X’s next
pointer is pointing.
And X’s next pointer will now point to Y.
We need to make sure that if X is the last node of list then after adding Y we make end
point to Y

void Doubly_Linked_List :: add_after(node *n, int d)


{
node *temp;
temp = new node();
temp->data = d;
temp->prev = n;
temp->next = n->next;
n->next = temp;
temp->next->prev=temp;

//if node is to be inserted after last node


if(n->next == NULL)
end = temp;
}

3) Add a node at the end:


The new node is always added after the last node of the given Linked List
void Doubly_Linked_List :: add_end(int d)
{
// create new node
node *temp;
temp = new node();
temp->data = d;
temp->prev = end;
temp->next = NULL;

// if list is empty
if(end == NULL)
front = temp;
else
end->next = temp;
end = temp;
}
4) Add a node before a given node:  
  Let the pointer to this given node be next_node and the data of the new node to be
added as new_data. 
 
Check if the next_node is NULL or not. If it’s NULL, return from the function because any
new node can not be added before a NULL
Allocate memory for the new node, let it be called new_node
Set new_node->data = new_data
Set the previous pointer of this new_node as the previous node of the next_node, new_node-
>prev = next_node->prev
Set the previous pointer of the next_node as the new_node, next_node->prev = new_node
Set the next pointer of this new_node as the next_node, new_node->next = next_node;
If the previous node of the new_node is not NULL, then set the next pointer of this previous
node as new_node, new_node->prev->next = new_node
Else, if the prev of new_node is NULL, it will be the new head node. So, make (*head_ref)
= new_node.

void Doubly_Linked_List :: add_before(node *n, int d)


{
node *temp;
temp = new node();
temp->data = d;
temp->next = n;
temp->prev = n->prev;
n->prev = temp;
//if node is to be inserted before first node
if(n->prev == NULL)
front = temp;
}

Remove a Node:
Removal of a node is quite easy in Doubly linked list but requires special handling if the node to
be deleted is first or last element of the list. Unlike singly linked list where we require the
previous node, here only the node to be deleted is needed. We simply make the next of the
previous node point to next of current node (node to be deleted) and prev of next node point to
prev of current node. Look code for more details.

void Doubly_Linked_List :: delete_node(node *n)


{
// if node to be deleted is first node of list
if(n->prev == NULL)
{
front = n->next; //the next node will be front of list
front->prev = NULL;
}
// if node to be deleted is last node of list
else if(n->next == NULL)
{
end = n->prev; // the previous node will be last of list
end->next = NULL;
}
else
{
//previous node's next will point to current node's next
n->prev->next = n->next;
//next node's prev will point to current node's prev
n->next->prev = n->prev;
}
//delete node
delete(n);
}

Forward Traversal:
Start with the front node and visit all the nodes untill the node becomes NULL.

void Doubly_Linked_List :: forward_traverse()


{
node *trav;
trav = front;
while(trav != NULL)
{
cout<<trav->data<<endl;
trav = trav->next;
}
}

Backward Traversal:
Start with the end node and visit all the nodes until the node becomes NULL.

void Doubly_Linked_List :: backward_traverse()


{
node *trav;
trav = end;
while(trav != NULL)
{
cout<<trav->data<<endl;
trav = trav->prev;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy