0% found this document useful (0 votes)
6 views

10 -Queues Using Array_074810

Uploaded by

nailapp2023
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

10 -Queues Using Array_074810

Uploaded by

nailapp2023
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Queues in C++

Queues

• Queue: list of homogeneous elements


• Elements are:
− Added at one end (the back or rear)
− Deleted from the other end (the front)
• First In First Out (FIFO) data structure
− Middle elements are inaccessible
‫العناصر الوسطى ال يمكن الوصول إليها‬
• Example:
− Waiting line in a bank

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

• You need at least four (member) variables:


− An array to store the queue elements
− queueFront and queueRear
• To keep track of first and last elements
− maxQueueSize
• To specify the maximum size of the queue

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

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 11


Implementation of Queues as Arrays
(continued)
• To advance the index in a (logically) circular
array:

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

• Returns the first element of the queue

int cqueue::getfront()

{
return queue[front];
}

22
Back

• Returns the last element of the queue

int cqueue::getrear()

{
return queue[rear];
}

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 23


addQueue

void cqueue::inqueue(int el) void cqueue::inqueue(int el)


{ {
if (! isfull()) if (! isfull())
{ {
if(rear+1==size)
rear =0; // give index 0 to rear rear = ((rear+1)==size ? 0 : rear+1);
else queue[rear]= el;
rear = rear +1; count ++;
queue[rear]=el; }
count++; else
} cout<<"queue is full"<<endl;
else }
cout<<"queue is full"<<endl;
}

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

You might also like