You are on page 1of 14

DOUBLY LINKED

LIST
Chandani Bhasin
NODES IN DLL
• Nodes contain 3 parts:-
– Value
– Pointer to next variable
– Pointer to previous variable
Advantages of DLL
• Can be traversed in two directions
– Forward and backward
• Add nodes at 4 places
– At the beginning
– At the end
– Before a given node
– After a given node
Disadvantages of DLL
• Occupies more space
• Since there are 2 pointers.

• Take care of two pointers so be very particular


Singly LL or Doubly LL??
• Use SLL when you have limitation of space and
when you are not to perform more addition or
deletion operations.
Implementing DLL- use pointers
• We use pointers since we are dynamically
changing the size of the list.
STEPS to add 1st node-
1.Create a new node
2.Set the value of node
3.Set value for next and previous pointer.
4.Since it’s the only node so it’s the head and
the tail.
Step 1 - Create a new node
• Node* node = new Node()
Step 2 – set the value for each node
• node-> value=4;
Step 3 -Set value for next and previous
pointer.
• node->next=nullptr;
• node->previous=nullptr;
Step 4 - Since it’s the only node so it’s the
head and the tail.
• head=node;
• tail=node;
Adding 2 node nd

• Create a new node


• Assign value to each node
• Head and previous pointers.
– node->next=nullptr;
– node->previous=tail;
• Now next pointer of 1st node will not point to
null, it should point to 2nd node.
– tail->next=node;

• Now 2nd node will be the tail


– tail=node;
Invoking functions
• printForward- it includes head
void printForward(Node* head)
{
Node* traverser= head;
while(traverser!=nullptr)
{
cout<<traverser->value<<endl;
traverser=traverser->next;
}
}
• PrintBackward – it includes ?

You might also like