Sunteți pe pagina 1din 15

Variations for Queue

Implementation
FORMS OF QUEUES
 Linear queues(simple queues)
 Circular queues (queues as circular lists)

 Priority queues
QUEUE WITH FIXED ARRAY SIZE

#define MAX 5
struct queue
{ int front=0, rear=-1;
int items[MAX];
}q ;
Full
if(q.rear==MAX-1) Full
Insert
if not Full
q.items[++q.rear]=x;
Empty
if(q.rear<q.front) Empty
Remove
If not Empty
x=q.items[q.front++];
DISADV. OF THIS APPROACH
When rear=front=last element, we can not add any new item
though the Q has some slots vacant.
Solution: On deletion, move all elements to the left by one
position and decrement rear by one.
Then we don’t need front indicator at all, because it is always 0
Modified version of delete()
if(q.rear==-1)
It is empty
x=q.items[0];
for(i=0;i<q.rear;i++)
q.items[i]=q.items[i+1];
q.rear--;
SHIFT()
#define MAX 5
struct queue

{ int front=0, rear=-1;


int items[MAX];
}q ;
Full
if(q.rear==MAX-1) Full
Insert
if not Full
q.items[++q.rear]=x;
Empty
if(q.rear==-1) Empty
Remove
x=q.items[0];
shift(q);
for(i=0;i<q.rear;i++)
{
q.items[i]=q.items[i+1]; }
QUEUE WITH INFINITE ARRAY
struct queue
{ int front=0, rear=-1;
int items[100];
}q ;
Insert
q.items[++q.rear]=x;
Empty
if(q.rear<q.front) Empty
Remove
If not Empty
x=q.items[q.front++];
No. elements
(q.Rear -q.front + 1)
QUEUE AS A FIXED SIZE CIRCULAR ARRAY
struct queue
{ int front, rear; Insert
int items[5]; if(q.rear==MAX-1)
}q ; q.rear=0;
Initially else
q.rear=q.front=MAX-1=4 q.rear++;
Empty 3
if(q.rear==q.front) Empty 4 2
if(q.rear==q.front)
Remove {if(q.rear==0) 0 1
If not Empty q.rear=MAX-1;
{if(q.front==MAX-1) else
q.front=0; q.rear--;
else }
q.front++; else
return (q.items[q.front]; q.items[q.rear]=x;
}
CIRCULAR QUEUE OPERATIONS
q.item q.item

4 E q.rear=4 E
4
3 D D
3
2 C q.front=2 2 C q.front=2
1 1
0 0 F q.rear=0
q.item q.item

E q.front=4 q.front=4
4 E
3
2
1 G q.rear=1
0 F q.rear=0 F
CIRCULAR QUEUE OPERATIONS CONTINUED
q.item

G q.rear=1
F q.front=0

It is difficult to determine when the queue is empty under this representation


The condition q.rear<q.front is no longer valid
The alternative is: since q.rear is the index of the last element of the queue, the
Condition q.front=q.rear implies that the queue is empty.
In this implementation, q.front and q.rear are initialized to the last index
of the array instead of 0 or -1, because the last element of the array
immediately precedes the first one within the queue under this
Representation. Since q.rear equals q.front, the queue is initially empty.
PRIORITY QUEUES
 A priority queue is a collection of elements such that each element has been
assigned a priority and such that the order in which elements are deleted and
processed comes from the following rules:
 An element of higher priority is processed before any element of lower
priority
 Two elements with the same priority are processed according to the oder in
which they were added to the queue
 one way to maintain a priority queue in memory is by means of a one-way list
 Each node in the list will contain three items of information: a data field, a
priority number and a link
 A node x precedes a node y in the list when (1) x has higher priority than y
or (2) when both have the same priority but x was added to the list before y.
This means that the order in the one-way list corresponds to the order of the
priority queue
 THE LOWER THE PRIORITY NUMBER, THE HIGHER THE PRIORITY
PRIORITY QUEUES CONTD……
 The intrinsic ordering of elements determines the results of
insert or delete operations
 Two types of priority queues exist:
 Ascending
 Descending
start

A 1 B 2 C 4

D 5 E 7 F 7 0

Adding an element to the priority queue is much more


complicated than deleting an element from the queue as
we need to find the correct place to insert the element
QUEUE ALGORITHMS (CIRCULAR)
Algorithm Insert(item) Alternate Code:
rear = (rear+1) mod n;
if(rear = n-1) then
if(front=rear) then rear=0;
{ else
rear=rear+1;
write(“queue is full”);
if(front=0)then rear=n-1;
else rear=rear-1;
return false;
}
else
{ q[rear]=item;
return true;
}
Algorithm Delete( )
if(front=rear) then
{
write(“Queue is empty”);
return false;
}
else
{
front=(front+1) mod n;
item = q[front];
return true;
}
QUEUE ALGORITHM COMPLEXITY
 Addition and deletion for queues can be carried out
in a fixed amount of time or O(1)
GROWABLE ARRAY-BASED QUEUE

12/08/21 01:07
 In an enqueue operation, when the array is
full, instead of throwing an exception, we can

Queues
replace the array with a larger one
 Similar to what we did for an array-based
stack
 The enqueue operation has amortized
running time
 O(n) with the incremental strategy
 O(1) with the doubling strategy

15

S-ar putea să vă placă și