25
Data Structures Doubly Link List

Data Structure Lecture 6

Embed Size (px)

DESCRIPTION

Doubly link list.

Citation preview

Page 1: Data Structure Lecture 6

Data Structures

Doubly Link List

Page 2: Data Structure Lecture 6

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

Page 3: Data Structure Lecture 6

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?

Page 4: Data Structure Lecture 6

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

Page 5: Data Structure Lecture 6

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

Page 6: Data Structure Lecture 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.

Page 7: Data Structure Lecture 6

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.

Page 8: Data Structure Lecture 6

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

Page 9: Data Structure Lecture 6

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

Page 10: Data Structure Lecture 6

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

Page 11: Data Structure Lecture 6

Doubly Linked List Operations AddToDLLTail(x)

3. The node's info member is initialized to a particular value.

\ 7 \head tail

8

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

\ 7head tail

8 \

Page 12: Data Structure Lecture 6

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.

\ 7head tail

8 \

\ 7head tail

8 \

Page 13: Data Structure Lecture 6

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.

Page 14: Data Structure Lecture 6

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

Page 15: Data Structure Lecture 6

Doubly Linked List Operations DeleteFromDLLTail(x)

2.2. Set both Set both headhead and and tail tail pointers to pointers to nullnull and return back the memory and return back the memory occupied by the node to the system.occupied by the node to the system.

Page 16: Data Structure Lecture 6

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.

\ 7head tail

8 9 4 \

Page 17: Data Structure Lecture 6

Doubly Linked List Operations DeleteFromDLLTail(x)

2. Set the variable tail to its predecessor.

\ 7head tail

8 9 4 \

\ 7head

tail

8 9 4 \

Page 18: Data Structure Lecture 6

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

headtail

8 9 4 \

\ 7head

tail

8 9

Page 19: Data Structure Lecture 6

Doubly Linked List Operations DeleteFromDLLTail(x)

4. The next of the tail node is a dangling reference; therefore next is set to null.

\ 7head

tail

8 9

\ 7head

tail

8 9 \

Page 20: Data Structure Lecture 6

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;

}};

Page 21: Data Structure Lecture 6

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();……………………………..

};

Page 22: Data Structure Lecture 6

Implementation of Doubly Linked List

template<class T>void DoublyLinkedList<T>:: addToDLLTail(const T& el){ if(head == 0) //List is empty

head = tail = new DLLNode<T>(el);else // Insert node at the end of the list{ tail = new DLLNode<T>(e1, 0, tail);

tail -> prev -> next = tail

}

}

\ 7 \head tail

\ 7head tail

8 \ 9 \tail

Page 23: Data Structure Lecture 6

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 listdelete head; head = tail = 0; }

else // more then one nodes in the list{

tail = tail -> prev;

delete tail -> next; tail -> next = 0;

}

}

\ 7 \head tail

\ 7head

8 \ 9 \tailtail

\

Page 24: Data Structure Lecture 6

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 list3. 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.

Page 25: Data Structure Lecture 6

Lab Exercise5. 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.