10 -Queues Using Array_074810
10 -Queues Using Array_074810
Queues
2
Queue Operations
• Some of the queue operations are:
− initializeQueue
− isEmptyQueue
− isFullQueue
− front
− back
− addQueue
− deleteQueue
• Abstract class queueADT defines these
operations
3
Implementation of Queues as Arrays
6
Implementation of Queues as Arrays
(continued)
• To add an element to the queue:
− Advance queueRear to next array position
− Add element to position pointed to by
queueRear
• Example: array size is 100; originally empty
7
Implementation of Queues as Arrays
(continued)
• To delete an element from the queue:
− Retrieve element pointed to by queueFront
− Advance queueFront to next queue element
8
Implementation of Queues as Arrays
(continued)
• Will this queue design work?
− Suppose A stands for adding an element to
the queue
− And D stands for deleting an element from the
queue
− Consider the following sequence of
operations:
• AAADADADADADADADA...
9
Implementation of Queues as Arrays
(continued)
• The sequence AAADADADADADADADA...
would eventually set queueRear to point to
the last array position
− Giving the impression that the queue is full
10
Implementation of Queues as Arrays
(continued)
• Solution 1:
− When the queue overflows to the rear (i.e.,
queueRear points to the last array position):
• Check value of queueFront
• If value of queueFront indicates that there is
room in the front of the array, slide all of the
queue elements toward the first array position
حرك كل عناصر قائمة االنتظار نحو موضع بداية الطابور
• Problem: too slow for large queues
• Solution 2: assume that the array is circular
12
Implementation of Queues as Arrays
(continued)
if(rear+1==size)
rear =0; // give index 0 to rear
else
rear = rear +1;
queue[rear]=el;
count++;
13
Implementation of Queues as Arrays
(continued)
• Case 1:
if(front +1 == size)
front =0; // give index 0 to front
else
front=front+1;
count --;
14
Implementation of Queues as Arrays
(continued)
• Case 2:
if(rear+1==size)
rear =0; // give index 0 to rear
else
rear = rear +1;
queue[rear]=el;
count++; 15
Implementation of Queues as Arrays
(continued)
• Problem:
− Figures 19-47 and 19-49 have identical values
for queueFront and queueRear
− However, the former represents an empty
queue, whereas the latter shows a full queue
• Solution?
queueRear لها قيم متطابقة لقائمة االنتظار و49-19 و47-19 األشكال
بينما ُيظهر األخير قائمة انتظار، يمثل األول قائمة انتظار فارغة، ومع ذلك
كاملة
16
Implementation of Queues as Arrays
(continued)
• Solution 1: keep a count
− Incremented when a new element is added to
the queue
− Decremented when an element is removed
− Initially, set to 0
− Very useful if user (of queue) frequently needs
to know the number of elements in the queue
• We will implement this solution
يساعد في معرفة عدد العناصر في
قائمة االنتظار
17
Implementation of Queues as Arrays
(continued)
• Solution 2: let queueFront indicate index of
the array position preceding the first element
− queueRear still indicates index of last one
− Queue empty if:
• queueFront == queueRear
− Slot indicated by queueFront is reserved
• Queue can hold 99 (not 100) elements
− Queue full if the next available space is the
reserved slot indicated by queueFront
18
Implementation of Queues as Arrays
(continued)
19
Empty Queue and Full Queue
20
Initialize Queue
21
Front
int cqueue::getfront()
{
return queue[front];
}
22
Back
int cqueue::getrear()
{
return queue[rear];
}
24
deleteQueue
25
searchQueue
26
27
print()
Output:
B
S
Y
K
O
28
Code of queue using array
10
20
30
40
50
29