Successfully reported this slideshow.
Upcoming SlideShare
×

# Data Structure Lecture 6

640 views

Published on

Published in: Education
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Data Structure Lecture 6

1. 1. Data Structures Doubly Link List
2. 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. 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. 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
6. 6. Doubly Linked ListOperations 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. 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. 8. Doubly Linked List Operations AddToDLLTail(x)3. The nodes info member is initialized to a particular value. 74. 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. 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. 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. 11. Doubly Linked List Operations AddToDLLTail(x)3. The nodes info member is initialized to a particular value. 7 8 head tail4. 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. 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. 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. 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. 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. 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. 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. 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. 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 9head tail 7 8 9 head tail
20. 20. Implementation of Doubly Linked Listtemplate<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; }};