Upcoming SlideShare
×

# Data Structure Lecture 7

340 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

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

• Be the first to like this

Views
Total views
340
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
3
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Data Structure Lecture 7

1. 1. Data Structures
2. 2. Problem to be Solved Multiple processes want a service of a single resource Each process will use the resource for a fix amount of time and then the next resource will start using it. The process just used the resource will go to the end We have to assure that
3. 3. Problem to be Solved Multiple processes want a service of a single resource. Each process will use the resource for a fix amount of time and then the next process will start using it. The process just used the resource will go to the end. We have to assure that  No process accesses the resource before all the other processes did.  Therefore
4. 4. Problem to be Solved We want to arrange all these processes in such a way that they form a ring. Now the question is  in which data structures such a processes will be arranged so that they form a ring.  The data structures used for this type of problems is  Circular Lists
5. 5. Circular Lists In a circular lists nodes form a ring. The list is finite and each node has successor. In circular lists the tail of the list is always pointing to the head. The external pointer, points to the current "tail" of the list, then the "head" is found trivially via tail->next.
7. 7. Circular Lists (Linked Lists) Operations AddToCLLHead(x) tailtail 2 1 1 tail 2 1 3 tail 2 1 4 3
8. 8. Circular Lists (Linked Lists)Operations AddToCLLHead(x) Two cases must be considered to add the node in front of the circular linked list. 1. Either we are creating the first node of the circular linked list or 1. We are inserting the node in front of the existing circular linked list.
9. 9. Circular Lists (Linked Lists)Operations AddToCLLHead(x) First Case: Creating first node of a circular linked list. We follow the following steps to create the first node of the circular linked list.1. Check the value of the tail of the list. If this value is null it means we are creating the first node of the circular linked list.2. An empty node 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.
10. 10. Circular Lists (Linked Lists)Operations AddToCLLHead(x)3. The nodes info member is initialized to a particular value. 7 tail4. Since it is the only node of the circular linked list therefore its next member will be initialized to point to the node itself.5. Since it is the only node of the circular linked list therefore tail points to this node of the circular linked list.
11. 11. Circular Lists (Linked Lists)Operations AddToCLLHead(x) Second Case: Inserting node in front of the existing circular linked list: We follow the following steps to insert the node in front of the existing circular linked list.1. Check the value of the tail of the circular list. If this value is not null it means we are inserting node in front of the existing circular linked list. 7 tail
12. 12. Circular Lists (Linked Lists)Operations AddToCLLHead(x)2. An empty node 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 7 tail3. The nodes info member is initialized to a particular value.
13. 13. Circular Lists (Linked Lists)Operations AddToCLLHead(x)4. Because the node is being included at the front of the list, the next member becomes a pointer to the first node on the list, i.e. the node to which next of tail pointing. 8 7 tail5. The new node precedes all the nodes on the list, but this fact has to be reflected, otherwise the new node is not accessible. Therefore next of tail is updated to become the pointer to the new node
14. 14. Circular Lists (Linked Lists) Operations AddToCLLTail(x) tailtail 1 2 1 tail 2 3 1 tail 3 4 1 2
15. 15. Circular Lists (Linked Lists)Operations AddToCLLTail(x) Two cases must be considered to add the node at the end of the circular linked list. 1. Either we are creating the first node of the circular linked list or 1. We are inserting the node at the end of the existing circular linked list. The first case is exactly similar as we discussed in AddToCLLHead(x) operation.
16. 16. Circular Lists (Linked Lists)Operations AddToCLLTail(x) Second Case: Inserting node at the end of the existing circular linked list: We follow the following steps to insert the node in front of the existing circular linked list.1. Check the value of the tail of the circular list. If this value is not null it means we are inserting node at the end of the existing circular linked list. 8 7 tail
17. 17. Circular Lists (Linked Lists)Operations AddToCLLTail(x)2. An empty node 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 7 9 tail3. The nodes info member is initialized to a particular value.
18. 18. Circular Lists (Linked Lists)Operations AddToCLLTail(x) 4. Because the node is being included at the end of the list, the next member becomes a pointer to the first node on the list, i.e. the node to which next of tail pointing. 8 7 9 tail tail5. Since the new node will becomes the last node of the list, therefore the next of tail will set to point to the new node.6. Since the new node will becomes the last node of the list but this fact must be reflected in the list, therefore the tail will set to
19. 19. Implementation of Circular Lists In an implementation of a circular list we can use only one permanent pointer, tail.template<class T>class CircularLinkedList { private: Node<T> *tail; //pointer to last node of a list public: CircularLinkedList() { tail = 0; } ~CircularLinkedList(); Continue on next slide…
20. 20. Implementation of Circular Listsbool isEmpty() { if (tail == 0) return true //Circular list is empty;}void addToCLLHead(T el); //inserts an element in front of circular listvoid addToCLLTail(T el); //inserts an element at the end of circular listvoid deleteFromCLLHead(); //delete a node from the front of a circular listvoid deleteFromCLLTail(); //delete a node from the end of a circular listvoid CLLDelete(T el); //delete a node from circular list whose value is elNode *CLLFind(T el); //Find the node from circular list whose value is el //and returns its pointer.void CLLprints(); //prints the contents of a circular listbool CLLIsInList(T el); //check that an element el exists in a circular list or not};
21. 21. Implementation of Circular Listsvoid template<class T> CircularLinkedList::addToCLLHead(T el ){ if (isEmpty) //first node of a list tail { tail = new Node<T>(el); tail -> next = tail; 1 } else // Insert node in front of the circular list tail -> next = new Node<T>(el, tail->next); tail } 2 3 4 1
22. 22. Implementation of Circular Listsvoid template<class T> CircularLinkedList :: addToCLLTail(T el ){ if (isEmpty) //first node of a list tail { tail = new Node<T>(el); tail -> next = tail; 1 } else // Insert node at the end of the list { tail -> next = new Node<T>(el, tail->next); tail tail tail = tail -> next 2 3 4 } 1 }
23. 23. Circular List Variant There is one problem with the circular list we discussed so far. Guess What? Problem will be with the functions which deletes a node from circular list. A member function for the deletion of the tail node requires a loop so that tail can be set after deletion to its predecessor. A member function for the deletion of the node of particular value also requires a loop so that predecessor of that node will be connected to the successor of that node.
24. 24. Circular List Variant The use of loop is inefficient if circular list is very long or if delete operations are called frequently. Moreover processing data in the reverse order is not very efficient. To avoid the problem and still be able To insert and delete nodes at the front and at the end of the circular list without using a loop, a doubly linked circular list can be used.
25. 25. Doubly Linked Circular List This list forms two rings:  One going forward through next members and  One going backward through prev members tail a b c d
26. 26. Lab Exercise Implement the template class for circular list with following member functions.1. A function which inserts node in front of circular list.2. A function which inserts node at the end of linked circular list3. A function which removes a node from the front of circular list.4. A function which removes a node from the end of circular list.
27. 27. Lab Exercise5. A function which removes a node of particular value from circular list.6. A function which finds a node of particular value from circular list.7. A function which prints the contents of circular list.