Circular Queue - DSA

Embed Size (px)

Citation preview

  • 8/12/2019 Circular Queue - DSA

    1/19

    Circular Queue - DSA

  • 8/12/2019 Circular Queue - DSA

    2/19

    Circular Based

  • 8/12/2019 Circular Queue - DSA

    3/19

    Create queue

    #define MAXQUEUE 10struct queue{

    int items[MAXQUEUE];

    int front, rear};struct queue q;q.front, q.rear=MAXQUEUE-1;

  • 8/12/2019 Circular Queue - DSA

    4/19

    Empty queue

    empty (pq)

    struct queue pq;

    {return ((pq.front==pq.rear)? TRUE:FALSE);

    }

  • 8/12/2019 Circular Queue - DSA

    5/19

    From main()

    if(empty (q))

    queue is empty

    Elsequeue is not empty

  • 8/12/2019 Circular Queue - DSA

    6/19

    Remove Operation

    remove (pq)struct queue pq;{

    if(empty (pq)){

    printf(queue underflow);exit(1);}

    if(pq.front==MAXQUEUE-1)pq.front=0;

    elsepq.front++;

    return (pq.items[pq.front]);}

  • 8/12/2019 Circular Queue - DSA

    7/19

    Insert Operationinsert (pq,x)

    struct queue pq;

    int x;

    {

    if(pq.rear==MAXQUEUE-1)

    pq.rear==0;

    elsepq.rear++;

    if(pq.rear==pq.front)

    {

    printf(queue overflow);

    exit(1);}

    pq.items[pq.front]=x;return;

    }

  • 8/12/2019 Circular Queue - DSA

    8/19

    Problems with Stack & Queue

    Both the stack and the queue are data structureswhose elements are ordered based on the sequencein which they have been inserted.

    The pop operation retrieves the last element insertedand the remove operation retrieves the first elementinserted.

    If there is an intrinsic order among the elementsthemselves (for example, numeric order or alphabeticorder), it is ignored in the stack or queue operations.

  • 8/12/2019 Circular Queue - DSA

    9/19

    Solution: Priority Queue

    The priority queue is a data structure in which

    the intrinsic ordering of the element does

    determine the results of its basic operations.

    Types

    Ascending priority queue

    Descending priority queue

  • 8/12/2019 Circular Queue - DSA

    10/19

    Ascending priority queue

    It is a collection of items into which items canbe inserted arbitrarily and from which onlythe smallest item can be removed.

    If apq is an ascending order priority queue,the operation pqinsert (apq,x) inserts element

    x into apq and pqmindelete (apq) removesthe minumum element from apq and returnits value.

  • 8/12/2019 Circular Queue - DSA

    11/19

    Descending priority queue

    It is similar but allows deletion of only the largest item.

    The operations applicable to a descending priority queue,dpq, are pqinsert(dpq,x)and pqmaxdelete (dpq).

    pqinsert(dpq,x) inserts element x into dpq and is logicallyidentical to pqinsertfor an ascending priority queue.

    pqmaxdelete(dpq) removes the maximum element fromdpq and returns its value

  • 8/12/2019 Circular Queue - DSA

    12/19

    empty(pq)

    This operation applies to both types of priority

    queue and determines whether a priority

    queue is empty.

    pqmindelete or pqmaxdelete can only be

    applied to a nonempty priority queue.

  • 8/12/2019 Circular Queue - DSA

    13/19

    Conditionascending pq

    Once pqmindelete has been applied to retrieve thesmallest element of an ascending priority queue, it canbe applied again to retrieve the next smallest, and soon.

    Thus the operation successively retrieves the elementsof a pqin ascending order.

    If a small element is inserted after several deletions,the next retrieval will return that small element, whichmay be smaller than a previously received elements.

  • 8/12/2019 Circular Queue - DSA

    14/19

    ConditionDescending pq

    Similar to ascending pq

  • 8/12/2019 Circular Queue - DSA

    15/19

    Possibility

    It is not that the pq will have only the number orcharacters.

    It may have complex structures for example telephone-book listings consist of last names, first names,addresses and phone numbers and are ordered by lastname.

    Some times it may not be even the part of the queuefor example in stack entry it may be required to view inthe form of the time of insertion.

  • 8/12/2019 Circular Queue - DSA

    16/19

    Array implementation of a PQ

    if (pq.rear>=maxpq)

    {

    printf(priority queue overflow);

    exit(1);

    }

    pq.itmes[pq.rear]=x;

    pq.rear++;

    Condition pq are maintained in positions 0 to n-1 of anarray pq.items of size maxpq, and suppose thatpq.rear equals the first empty array position, n.

  • 8/12/2019 Circular Queue - DSA

    17/19

    Problem

    As the insertion keep on going no issue.

    If deletion comes that is removal

    pqmindelete(pq) then

    Locate the smallest element that is it needs toread all the place.

    How is it possible to delete in the middle? If stackand queue having the certain procedures.

  • 8/12/2019 Circular Queue - DSA

    18/19

    Problems

    How shall we identify the free location?

  • 8/12/2019 Circular Queue - DSA

    19/19

    Thank You