queue
operations performed on queue
queue applications
Example to enqueue
Algorithm To enqueue() / add () An Element (ITEM )In The Queue
Example to dequeue
Algorithm To dequeue() / remove() An Element (ITEM )In The Queue
PRIORITY Queue
PRIORITY Queue Representation
2. CONteNtsCONteNts
queue
OperatiONs perfOrmed ON queue
queue appliCatiONs
example tO eNqueue
algOrithm tO enqueue() / add () aN elemeNt
(item )iN the queue
example tO dequeue
algOrithm tO dequeue() / remove() aN elemeNt
(item )iN the queue
priOritY queue
priOritY queue represeNtatiON
01/31/18 BY MS. SHAISTA QADIR 2
3. queuequeue
01/31/18 BY MS. SHAISTA QADIR 3
A queue is simply a waiting line that grows by adding
elements to its end and shrinks by taking elements from its
front.
Unlike a stack, a queue is a structure in which both ends
are used.
one for adding new elements and one for removing them.
Therefore, the last element has to wait until all elements
preceding it on the queue are removed.
A queue is an FIFO structure: first in/first out.
4. OperatiONs perfOrmed ONOperatiONs perfOrmed ON
queuequeue
01/31/18 BY MS. SHAISTA QADIR 4
Queue operations are similar to stack operations. The
following operations are needed to properly manage a
queue:
clear() : Clear the queue.
isEmpty() : Check to see if the queue is empty.
enqueue(el): Put the element el at the end of the queue.
dequeue() : Take the first element from the queue.
firstEl() : Return the first element in the queue without
removing it.
5. operations performed onoperations performed on
QUeUeQUeUe
01/31/18 BY MS. SHAISTA QADIR 5
A series of enqueue and dequeue operations is shown in Figure .
This time—unlike for stacks—the changes have to be monitored
both at the beginning of the queue and at the end.
The elements are enqueued on one end and dequeued from the
other.
For example, after enqueuing 10 and then 5, the dequeue
operation removes 10 from the queue
6. QUeUe applicationsQUeUe applications
01/31/18 BY MS. SHAISTA QADIR 6
APPLICATIONS OF QUEUE:
◦ Waiting Line
◦ Access to Shared recourses (Example: Printer)
◦ Multiprogramming
◦ Client-Server system
7. 01/31/18 BY MS. SHAISTA QADIR 7
Example: FOR enqueue() / add()
array size is 8,
therefore, the index of the array will be from 0 to 7.
Elements inside array are 1, 7, 5 and 2
When we enqueue(6), 6 has been inserted in the queue.
Now, the rear index is containing 4 while the front has the
same 0 index.
example to enQUeUeexample to enQUeUe
8. 01/31/18 BY MS. SHAISTA QADIR 8
Example: FOR enqueue() / add()
When we enqueue(8) another element 8 is inserted in the
queue.
example to enQUeUeexample to enQUeUe
9. Algorithm toAlgorithm to enqueue() / add ()enqueue() / add ()
An ElEmEnt (itEm )in thEAn ElEmEnt (itEm )in thE
QuEuEQuEuE
01/31/18 BY MS. SHAISTA QADIR 9
Algorithm To enqueue() / add () An Element (ITEM )In The
Queue:
1. If (REAR == max) Then
2. Print: Overflow
3. Else
4. If (FRONT and REAR == 0) Then
(a) Set FRONT = 1
(b) Set REAR = 1
5. Else
6. Set REAR = REAR + 1 [End of Step 4 If]
7. QUEUE[REAR] = ITEM
8. Print: ITEM inserted [End of Step 1 If]
9. Exit
10. 01/31/18 BY MS. SHAISTA QADIR 10
PROGRAM LOGIC:
void enqueue(int j)
{
if(rear == maxsize-1)
rear = -1;
qarr[++rear] = j;
cnt++;
}
Algorithm toAlgorithm to enqueue() / add ()enqueue() / add ()
An ElEmEnt (itEm )in thEAn ElEmEnt (itEm )in thE
QuEuEQuEuE
11. 01/31/18 BY MS. SHAISTA QADIR 11
Example To dequeue ()/ remove () an element from
queue.
Element is deleted from front end.
After a call of dequeue() method:
Another call of dequeue() method:
ExAmplE to dEQuEuEExAmplE to dEQuEuE
12. Algorithm toAlgorithm to dequeue() / remove()dequeue() / remove()
An ElEmEnt (itEm )in thEAn ElEmEnt (itEm )in thE
QuEuEQuEuE
01/31/18 BY MS. SHAISTA QADIR 12
Algorithm to dequeue()/ remove () an element (ITEM )
from the queue:
1. If (FRONT == 0) Then
2. Print: Underflow // Check for underflow
3. Else
4. ITEM = QUEUE[FRONT]
5. If (FRONT == REAR) Then // Check if only one element in Queue
(a) Set FRONT = 0
(b) Set REAR = 0
6. Else
7. Set FRONT = FRONT + 1 [End of Step 5 If]
8. Print: ITEM deleted [End of Step 1 If]
9. Exit
13. Algorithm toAlgorithm to dequeue() / remove()dequeue() / remove()
An ElEmEnt (itEm )in thEAn ElEmEnt (itEm )in thE
QuEuEQuEuE
01/31/18 BY MS. SHAISTA QADIR 13
PROGRAM LOGIC:
int dequeue()
{
int temp = qarr[front];
front++;
if(front == maxsize)
front = 0;
cnt--;
return temp;
}
14. PrioritY QuEuEPrioritY QuEuE
01/31/18 BY MS. SHAISTA QADIR 14
• In many situations, simple queues are inadequate, as when
first in/first out scheduling has to be overruled using some
priority criteria.
• In a post office example, a handicapped person may have
priority over others.
• Therefore, when a clerk is available, a handicapped person is
served instead of someone from the front of the queue.
• In a sequence of processes, process P2 may need to be
executed before process P1 for the proper functioning of a
system, even though P1 was put on the queue of waiting
processes before P2.
• In situations like these, a modified queue, or priority queue,
is needed. In priority queues, elements are dequeued
according to their priority and their current queue position.
15. PrioritY QuEuE rEPrEsEntAtionPrioritY QuEuE rEPrEsEntAtion
01/31/18 BY MS. SHAISTA QADIR 15
• Priority queues can be represented by two variations of
linked lists.
• In one type of linked list, all elements are entry ordered,
• And in another, order is maintained by putting a new
element in its proper position according to its priority.
• In both cases, the total operational times are O(n)
• because, for an unordered list, adding an element is
immediate but searching is O(n),
• And in a sorted list, taking an element is immediate but
adding an element is O(n).