Sie sind auf Seite 1von 19

Circular Queue - DSA

Circular Based
Create queue
#define MAXQUEUE 10
struct queue
{
int items[MAXQUEUE];
int front, rear
};
struct queue q;
q.front, q.rear=MAXQUEUE-1;

Empty queue
empty (pq)
struct queue pq;
{
return ((pq.front==pq.rear)? TRUE:FALSE);
}
From main()
if(empty (q))
queue is empty
Else
queue is not empty

Remove Operation
remove (pq)
struct queue pq;
{
if(empty (pq))
{
printf(queue underflow);
exit(1);
}
if(pq.front==MAXQUEUE-1)
pq.front=0;
else
pq.front++;
return (pq.items[pq.front]);
}
Insert Operation
insert (pq,x)
struct queue pq;
int x;
{
if(pq.rear==MAXQUEUE-1)
pq.rear==0;
else
pq.rear++;
if(pq.rear==pq.front)
{
printf(queue overflow);
exit(1);
}
pq.items[pq.front]=x;
return;
}

Problems with Stack & Queue
Both the stack and the queue are data structures
whose elements are ordered based on the sequence
in which they have been inserted.

The pop operation retrieves the last element inserted
and the remove operation retrieves the first element
inserted.

If there is an intrinsic order among the elements
themselves (for example, numeric order or alphabetic
order), it is ignored in the stack or queue operations.
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
Ascending priority queue

It is a collection of items into which items can
be inserted arbitrarily and from which only
the 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) removes
the minumum element from apq and return
its value.


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 logically
identical to pqinsert for an ascending priority queue.

pqmaxdelete(dpq) removes the maximum element from
dpq and returns its value
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.
Condition ascending pq
Once pqmindelete has been applied to retrieve the
smallest element of an ascending priority queue, it can
be applied again to retrieve the next smallest, and so
on.

Thus the operation successively retrieves the elements
of a pq in ascending order.

If a small element is inserted after several deletions,
the next retrieval will return that small element, which
may be smaller than a previously received elements.
Condition Descending pq
Similar to ascending pq
Possibility
It is not that the pq will have only the number or
characters.

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

Some times it may not be even the part of the queue
for example in stack entry it may be required to view in
the form of the time of insertion.
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 an
array pq.items of size maxpq, and suppose that
pq.rear equals the first empty array position, n.

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 to
read all the place.

How is it possible to delete in the middle? If stack
and queue having the certain procedures.
Problems
How shall we identify the free location?

Thank You

Das könnte Ihnen auch gefallen