2. Problem ….
The member function deleteFromTail() of
singly linked list indicates a problem
inherent to singly linked lists.
The nodes in singly linked lists contain only
pointers to the successors;
Therefore there is no immediate access to
the predecessors.
For this reason, deleteFromTail() was
implemented with a loop allowed us to find
the predecessor of tail.
But the problem is
3. Problem ….
We have to scan the entire list to stop
right in front of the tail to delete it.
For long lists and for frequent
executions of deleteFromTail(), this
may be an impediment to swift list
processing.
Is there any way to avoid this
effort?
4. Solution
We redefine the linked list so that
Each node in a list has two pointers,
one to the successor and one to the
predecessor.
A list of this type is called a doubly
linked list.
a b c d
head tail
5. Doubly Linked List
Operations
The common operations on doubly linked lists are:
1. AddToDLLHead(x) Inserts element x in front of the
doubly linked list .
2. AddToDLLTail(x) Inserts element x at the end of
the doubly linked list.
3. DeleteFromDLLHead() Deletes the first element of
the doubly linked list.
4. DeleteFromDLLTail() Deletes the last element of
the doubly linked list.
5. DLLDelete(x) Deletes the node of value x from the
doubly linked list.
6. DLLFind(x) Find the entry x in the doubly linked
list.
7. DLLprint() prints the contents of the doubly linked
list.
8. And so on i.e.as many operations as you
required
6. Doubly Linked List
Operations AddToDLLTail(x)
Two cases must be considered to add the
node at the end of doubly linked list.
1. Either we are creating the first node of the
doubly linked list or
1. We are inserting the node at the end of an
existing doubly linked list.
7. Doubly Linked List
Operations AddToDLLTail(x)
First Case: Creating first node of a doubly
linked list.
We follow the following steps to create the first
node of the doubly linked list.
1. Check the value of the head of the doubly linked
list.
If this value is null it means we are creating the first node
of the doubly linked list.
2. An empty node of doubly linked list is created.
It is empty in the sense that the program performing
insertion does not assign any values to the data members
of the node.
8. Doubly Linked List
Operations AddToDLLTail(x)
3. The node's info member is initialized to a particular
value.
7
4. Since it is the only node of the doubly linked list
therefore its next and previous members will be
initialized to null.
7
5. Since it is the only node of the doubly linked list
therefore both head and tail points to this node of the
doubly linked list. 7
head tail
9. Doubly Linked List
Operations AddToDLLTail(x)
Second Case: Inserting node at the
end of an existing doubly linked
list.
We follow the following steps to insert
the node at the end of an existing
doubly linked list.
1. Check the value of the head of the doubly
list.
If this value is not null it means doubly linked
list already exists and we insert the node at the
end of an existing doubly linked list.
7
head tail
10. Doubly Linked List
Operations AddToDLLTail(x)
2. An empty node of doubly linked list is
created.
It is empty in the sense that the program
performing insertion does not assign any
values to the data members of the node.
7
head tail
11. Doubly Linked List
Operations AddToDLLTail(x)
3. The node's info member is initialized to a particular value.
7 8
head tail
4. Because the node is being included at the end of the doubly
linked list, the next member is set to null.
5. Set the previous member of the new node to the value of tail,
so that this member points to the last node in the list.
6. Set the next member of the tail to point to the new node
7 8
head
tail
12. Doubly Linked List
Operations AddToDLLTail(x)
7. Now, the new node should become the last node of the
linked list, therefore tail is set to point to the new node.
7 8
head tail
7 8
head tail
13. Doubly Linked List Operations
DeleteFromDLLTail(x)
Two cases must be considered to
delete the node from the end of a
linked list.
1. Either a doubly linked list has only one
node or
1. A doubly linked list has more than one
nodes.
14. Doubly Linked List Operations
DeleteFromDLLTail(x)
First Case: Linked List has only
one node:
We follow the following steps to
handle this case:
1. Check the values of the pointer
variables head and tail.
If both points to the same node, means
doubly linked list has only one node.
7
head tail
15. Doubly Linked List Operations
DeleteFromDLLTail(x)
2. Set both head and tail pointers to
null and return back the memory
occupied by the node to the system.
16. Doubly Linked List Operations
DeleteFromDLLTail(x)
Second Case: Doubly Linked List
has more than one nodes:
We follow the following steps to
handle this case:
1. Check the values of the pointer
variables head and tail.
If both points to the different nodes,
means linked list has more than one
nodes.
7 8 9 4
head tail
17. Doubly Linked List Operations
DeleteFromDLLTail(x)
2. Set the variable tail to its
predecessor.
7 8 9 4
head tail
7 8 9 4
head
tail
18. Doubly Linked List Operations
DeleteFromDLLTail(x)
3. Remove the last node of the doubly
linked list, i.e. node next to the node
pointed by tail pointer.
7 8 9 4
head
tail
7 8 9
head
tail
19. Doubly Linked List Operations
DeleteFromDLLTail(x)
4. The next of the tail node is a
dangling reference; therefore next is
set to null.
7 8 9
head
tail
7 8 9
head
tail
20. Implementation of
Doubly Linked List
template<class T>
class DLLNode {
public:
T info;
DLLNode *next, *prev;
DLLNode() {
next = prev = 0;
}
DLLNode(T el, Node *n = 0, Node *p = 0) {
info = el;
next = n;
prev = p;
}
};
21. Implementation of
Doubly Linked List
template<class T>
class DoublyLInkedList {
private:
DLLNode <T> *head, *tail;
Public:
DoublyLinkedList(){
head = tail =0;
}
void addToDLLTail(const T&)
void deleteFromDLLTail();
……………………………..
};
22. Implementation of
Doubly Linked List
template<class T>
void DoublyLinkedList<T>:: addToDLLTail(const T& el)
{ if(head == 0) //List is empty
7
head = tail = new DLLNode<T>(el); head tail
else // Insert node at the end of the list
{ tail = new DLLNode<T>(e1, 0, tail);
tail -> prev -> next = tail
}
}
7 8 9
head tail tail
23. Implementation of
Doubly Linked List
template<class T>
void DoublyLinkedList<T>:: deleteFromDLLTail()
{ if(head != 0) //List not empty
if(head == tail) { //If only one node in the list
delete head; head = tail = 0; } 7
else // more then one nodes in the list head tail
{
tail = tail -> prev;
delete tail -> next;
tail -> next = 0;
}
} 7 8 9
head tail tail
24. Lab Exercise
Implement the template class for doubly
linked list with following member functions.
1. A function which inserts node in front of
doubly linked list.
2. A function which inserts node at the end of
doubly linked list
3. A function which removes a node from the
front of doubly linked list.
4. A function which removes a node from the
end of doubly linked list.
25. Lab Exercise
5. A function which removes a node of
particular value from doubly linked
list.
6. A function which finds a node of
particular value from doubly linked
list.
7. A function which prints the contents of
doubly linked list.