SlideShare a Scribd company logo
Data Structures
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
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
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
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.
Circular Lists (Linked Lists)
Operations
    The common operations on circular lists are:
1.   AddToCLLHead(x) Inserts element x in front of the
     circular linked list .
2.   AddToCLLTail(x) Inserts element x at the end of
     the circular linked list.
3.   DeleteFromCLLHead() Deletes the first element
     of the circular linked list.
4.   DeleteFromCLLTail() Deletes the last element of
     the circular linked list.
5.   Delete(x) Deletes the node of value x from the
     circular linked list.
6.   Find(x) Find the entry x in the circular linked list.
7.   print() prints the contents of the circular linked list.
8.   And so on i.e.as many operations as you
     required
Circular Lists (Linked Lists)
 Operations AddToCLLHead(x)
                                                  tail
tail
                                       2                 1
       1

                        tail

                2              1
        3

                                       tail

                                   2          1
            4       3
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.
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.
Circular Lists (Linked Lists)
Operations AddToCLLHead(x)
3.   The node's info member is initialized to a particular
     value.
                     7

                                tail
4.   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.
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
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

                                                   tail
3.   The node's info member is initialized to a particular
     value.
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

                                                          tail
5.   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
Circular Lists (Linked Lists)
 Operations AddToCLLTail(x)
                                                  tail
tail
                                       1                 2
       1

                        tail

                2              3
        1

                                       tail

                                   3          4
            1       2
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.
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
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
                                     tail

3.   The node's info member is initialized to a particular
     value.
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                    tail

5.    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
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…
Implementation of Circular
           Lists
bool isEmpty() {
   if (tail == 0) return true                  //Circular list is empty;
}
void addToCLLHead(T el);             //inserts an element in front of circular list
void addToCLLTail(T el);             //inserts an element at the end of circular list
void deleteFromCLLHead();            //delete a node from the front of a circular list
void deleteFromCLLTail();            //delete a node from the end of a circular list
void CLLDelete(T el); //delete a node from circular list whose value is el
Node *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 list
bool CLLIsInList(T el); //check that an element el exists in a circular list or not
};
Implementation of Circular
             Lists
void 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
Implementation of Circular
             Lists
void 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
    }
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.
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.
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
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 list
3.   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.
Lab Exercise
5.   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.
Ad

More Related Content

What's hot (19)

Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory Allocation
Prof Ansari
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
Afaq Mansoor Khan
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Zidny Nafan
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5
Kumar
 
Unit 2 linked list and queues
Unit 2   linked list and queuesUnit 2   linked list and queues
Unit 2 linked list and queues
kalyanineve
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
Rajkiran Nadar
 
computer notes - Priority queue
computer notes -  Priority queuecomputer notes -  Priority queue
computer notes - Priority queue
ecomputernotes
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
kulachihansraj
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
swajahatr
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
Muhazzab Chouhadry
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
Linked list
Linked listLinked list
Linked list
akshat360
 
Linklist
LinklistLinklist
Linklist
SHEETAL WAGHMARE
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
Zidny Nafan
 
Sorting & Linked Lists
Sorting & Linked ListsSorting & Linked Lists
Sorting & Linked Lists
J.T.A.JONES
 
10 Linked Lists Sacks and Queues
10 Linked Lists Sacks and Queues10 Linked Lists Sacks and Queues
10 Linked Lists Sacks and Queues
Praveen M Jigajinni
 
linked list
linked list linked list
linked list
Narendra Chauhan
 
Data Structure (Double Linked List)
Data Structure (Double Linked List)Data Structure (Double Linked List)
Data Structure (Double Linked List)
Adam Mukharil Bachtiar
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queue
surya pandian
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory Allocation
Prof Ansari
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
Afaq Mansoor Khan
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Zidny Nafan
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5
Kumar
 
Unit 2 linked list and queues
Unit 2   linked list and queuesUnit 2   linked list and queues
Unit 2 linked list and queues
kalyanineve
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
Rajkiran Nadar
 
computer notes - Priority queue
computer notes -  Priority queuecomputer notes -  Priority queue
computer notes - Priority queue
ecomputernotes
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
kulachihansraj
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
swajahatr
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
Muhazzab Chouhadry
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
Zidny Nafan
 
Sorting & Linked Lists
Sorting & Linked ListsSorting & Linked Lists
Sorting & Linked Lists
J.T.A.JONES
 
10 Linked Lists Sacks and Queues
10 Linked Lists Sacks and Queues10 Linked Lists Sacks and Queues
10 Linked Lists Sacks and Queues
Praveen M Jigajinni
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queue
surya pandian
 

Viewers also liked (12)

дәрумендер
дәрумендердәрумендер
дәрумендер
Dan41k
 
Teaching and learning with iPads for high school students with disabilities (...
Teaching and learning with iPads for high school students with disabilities (...Teaching and learning with iPads for high school students with disabilities (...
Teaching and learning with iPads for high school students with disabilities (...
Joan E. Hughes, Ph.D.
 
Laboratory Method (EDUC206B)
Laboratory Method (EDUC206B)Laboratory Method (EDUC206B)
Laboratory Method (EDUC206B)
Crystal Gozon
 
The laboratory method (Principles of Teaching)
The laboratory method (Principles of Teaching)The laboratory method (Principles of Teaching)
The laboratory method (Principles of Teaching)
thrydent leatrcie Nunez
 
Teaching Method - Laboratory Method
Teaching Method - Laboratory Method Teaching Method - Laboratory Method
Teaching Method - Laboratory Method
Leni Jane Villanueva
 
Laboratory method
Laboratory methodLaboratory method
Laboratory method
Amrutha Ramakrishnan Nair
 
Laboratory method
Laboratory methodLaboratory method
Laboratory method
Rubylyn Figueroa
 
Laboratory Method Of Teaching
Laboratory Method Of TeachingLaboratory Method Of Teaching
Laboratory Method Of Teaching
Prashant Mehta
 
Laboratory method
Laboratory methodLaboratory method
Laboratory method
Mandeep Gill
 
Laboratory services in hospital by ihmr b
Laboratory services in hospital by ihmr bLaboratory services in hospital by ihmr b
Laboratory services in hospital by ihmr b
Ratnesh Pandey
 
The laboratory method of teaching
The laboratory method of teachingThe laboratory method of teaching
The laboratory method of teaching
Reynel Dan
 
Laboratory Method of Teaching
Laboratory Method of TeachingLaboratory Method of Teaching
Laboratory Method of Teaching
BSEPhySci14
 
дәрумендер
дәрумендердәрумендер
дәрумендер
Dan41k
 
Teaching and learning with iPads for high school students with disabilities (...
Teaching and learning with iPads for high school students with disabilities (...Teaching and learning with iPads for high school students with disabilities (...
Teaching and learning with iPads for high school students with disabilities (...
Joan E. Hughes, Ph.D.
 
Laboratory Method (EDUC206B)
Laboratory Method (EDUC206B)Laboratory Method (EDUC206B)
Laboratory Method (EDUC206B)
Crystal Gozon
 
The laboratory method (Principles of Teaching)
The laboratory method (Principles of Teaching)The laboratory method (Principles of Teaching)
The laboratory method (Principles of Teaching)
thrydent leatrcie Nunez
 
Teaching Method - Laboratory Method
Teaching Method - Laboratory Method Teaching Method - Laboratory Method
Teaching Method - Laboratory Method
Leni Jane Villanueva
 
Laboratory Method Of Teaching
Laboratory Method Of TeachingLaboratory Method Of Teaching
Laboratory Method Of Teaching
Prashant Mehta
 
Laboratory services in hospital by ihmr b
Laboratory services in hospital by ihmr bLaboratory services in hospital by ihmr b
Laboratory services in hospital by ihmr b
Ratnesh Pandey
 
The laboratory method of teaching
The laboratory method of teachingThe laboratory method of teaching
The laboratory method of teaching
Reynel Dan
 
Laboratory Method of Teaching
Laboratory Method of TeachingLaboratory Method of Teaching
Laboratory Method of Teaching
BSEPhySci14
 
Ad

Similar to Data Structure Lecture 7 (20)

Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6
Teksify
 
List data structure
List data structure List data structure
List data structure
Mahmoud Abuwarda
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1
SSE_AndyLi
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
Niraj Agarwal
 
Lecture 3 List of Data Structures & Algorithms
Lecture 3 List of Data Structures & AlgorithmsLecture 3 List of Data Structures & Algorithms
Lecture 3 List of Data Structures & Algorithms
haseebanjum2611
 
csc211_lecture_21.pptx
csc211_lecture_21.pptxcsc211_lecture_21.pptx
csc211_lecture_21.pptx
ASADAHMAD811380
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
ssuserd2f031
 
3.ppt
3.ppt3.ppt
3.ppt
ArifKamal36
 
3.ppt
3.ppt3.ppt
3.ppt
ArifKamal36
 
LinkedList1LinkedList1LinkedList1111.pdf
LinkedList1LinkedList1LinkedList1111.pdfLinkedList1LinkedList1LinkedList1111.pdf
LinkedList1LinkedList1LinkedList1111.pdf
timoemin50
 
Linked list
Linked list Linked list
Linked list
Arbind Mandal
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
Durga Devi
 
Data Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptxData Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptx
JoannahClaireAlforqu
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
Rai University
 
Linked List Presentation in data structurepptx
Linked List Presentation in data structurepptxLinked List Presentation in data structurepptx
Linked List Presentation in data structurepptx
nikhilcse1
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
Rai University
 
Team 10
Team 10Team 10
Team 10
Sathasivam Rangasamy
 
Linked List Representation of a Linked List.pptx
Linked List Representation of a Linked List.pptxLinked List Representation of a Linked List.pptx
Linked List Representation of a Linked List.pptx
AAUsH2
 
Stack implementation using linked list ppt
Stack implementation using linked list pptStack implementation using linked list ppt
Stack implementation using linked list ppt
JayasankarShyam
 
link list.pptx complete notes detailed ans
link list.pptx complete notes detailed anslink list.pptx complete notes detailed ans
link list.pptx complete notes detailed ans
IqraHanif27
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6
Teksify
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1
SSE_AndyLi
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
Niraj Agarwal
 
Lecture 3 List of Data Structures & Algorithms
Lecture 3 List of Data Structures & AlgorithmsLecture 3 List of Data Structures & Algorithms
Lecture 3 List of Data Structures & Algorithms
haseebanjum2611
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
ssuserd2f031
 
LinkedList1LinkedList1LinkedList1111.pdf
LinkedList1LinkedList1LinkedList1111.pdfLinkedList1LinkedList1LinkedList1111.pdf
LinkedList1LinkedList1LinkedList1111.pdf
timoemin50
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
Durga Devi
 
Data Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptxData Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptx
JoannahClaireAlforqu
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
Rai University
 
Linked List Presentation in data structurepptx
Linked List Presentation in data structurepptxLinked List Presentation in data structurepptx
Linked List Presentation in data structurepptx
nikhilcse1
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
Rai University
 
Linked List Representation of a Linked List.pptx
Linked List Representation of a Linked List.pptxLinked List Representation of a Linked List.pptx
Linked List Representation of a Linked List.pptx
AAUsH2
 
Stack implementation using linked list ppt
Stack implementation using linked list pptStack implementation using linked list ppt
Stack implementation using linked list ppt
JayasankarShyam
 
link list.pptx complete notes detailed ans
link list.pptx complete notes detailed anslink list.pptx complete notes detailed ans
link list.pptx complete notes detailed ans
IqraHanif27
 
Ad

More from Teksify (11)

HCTE C&C08(TDM)
HCTE C&C08(TDM) HCTE C&C08(TDM)
HCTE C&C08(TDM)
Teksify
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5
Teksify
 
Data Structure Lecture 4
Data Structure Lecture 4Data Structure Lecture 4
Data Structure Lecture 4
Teksify
 
Data Structure Lecture 3
Data Structure Lecture 3Data Structure Lecture 3
Data Structure Lecture 3
Teksify
 
Data Structure Lecture 2
Data Structure Lecture 2Data Structure Lecture 2
Data Structure Lecture 2
Teksify
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
Teksify
 
Ch3
Ch3Ch3
Ch3
Teksify
 
Ch1 2
Ch1 2Ch1 2
Ch1 2
Teksify
 
Variable power supply
Variable power supplyVariable power supply
Variable power supply
Teksify
 
Use of rib tool in pro e
Use of rib tool in pro eUse of rib tool in pro e
Use of rib tool in pro e
Teksify
 
Make lens of mobile by pro e
Make lens of mobile by pro eMake lens of mobile by pro e
Make lens of mobile by pro e
Teksify
 
HCTE C&C08(TDM)
HCTE C&C08(TDM) HCTE C&C08(TDM)
HCTE C&C08(TDM)
Teksify
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5
Teksify
 
Data Structure Lecture 4
Data Structure Lecture 4Data Structure Lecture 4
Data Structure Lecture 4
Teksify
 
Data Structure Lecture 3
Data Structure Lecture 3Data Structure Lecture 3
Data Structure Lecture 3
Teksify
 
Data Structure Lecture 2
Data Structure Lecture 2Data Structure Lecture 2
Data Structure Lecture 2
Teksify
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
Teksify
 
Variable power supply
Variable power supplyVariable power supply
Variable power supply
Teksify
 
Use of rib tool in pro e
Use of rib tool in pro eUse of rib tool in pro e
Use of rib tool in pro e
Teksify
 
Make lens of mobile by pro e
Make lens of mobile by pro eMake lens of mobile by pro e
Make lens of mobile by pro e
Teksify
 

Data Structure Lecture 7

  • 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. 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. 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. 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.
  • 6. Circular Lists (Linked Lists) Operations  The common operations on circular lists are: 1. AddToCLLHead(x) Inserts element x in front of the circular linked list . 2. AddToCLLTail(x) Inserts element x at the end of the circular linked list. 3. DeleteFromCLLHead() Deletes the first element of the circular linked list. 4. DeleteFromCLLTail() Deletes the last element of the circular linked list. 5. Delete(x) Deletes the node of value x from the circular linked list. 6. Find(x) Find the entry x in the circular linked list. 7. print() prints the contents of the circular linked list. 8. And so on i.e.as many operations as you required
  • 7. Circular Lists (Linked Lists) Operations AddToCLLHead(x) tail tail 2 1 1 tail 2 1 3 tail 2 1 4 3
  • 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. 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. Circular Lists (Linked Lists) Operations AddToCLLHead(x) 3. The node's info member is initialized to a particular value. 7 tail 4. 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. 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. 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 tail 3. The node's info member is initialized to a particular value.
  • 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 tail 5. 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. Circular Lists (Linked Lists) Operations AddToCLLTail(x) tail tail 1 2 1 tail 2 3 1 tail 3 4 1 2
  • 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. 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. 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 tail 3. The node's info member is initialized to a particular value.
  • 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 tail 5. 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. 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. Implementation of Circular Lists bool isEmpty() { if (tail == 0) return true //Circular list is empty; } void addToCLLHead(T el); //inserts an element in front of circular list void addToCLLTail(T el); //inserts an element at the end of circular list void deleteFromCLLHead(); //delete a node from the front of a circular list void deleteFromCLLTail(); //delete a node from the end of a circular list void CLLDelete(T el); //delete a node from circular list whose value is el Node *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 list bool CLLIsInList(T el); //check that an element el exists in a circular list or not };
  • 21. Implementation of Circular Lists void 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. Implementation of Circular Lists void 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. 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. 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. 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. 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 list 3. 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. Lab Exercise 5. 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.
  翻译: