Queues in C++
2
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
‫إليها‬ ‫الوصول‬ ‫يمكن‬ ‫ال‬ ‫الوسطى‬ ‫العناصر‬
3
Queue Operations
• Some of the queue operations are:
− initializeQueue
− isEmptyQueue
− isFullQueue
− front
− back
− addQueue
− deleteQueue
• Abstract class queueADT defines these operations
6
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
7
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
8
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
9
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...
10
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
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 11
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)
• To advance the index in a (logically) circular
array:
13
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++;
14
Implementation of Queues as Arrays
(continued)
• Case 1:
if(front +1 == size)
front =0; // give index 0 to front
else
front=front+1;
count --;
15
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++;
16
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?
‫األشكال‬
19
-
47
‫و‬
19
-
49
‫و‬ ‫االنتظار‬ ‫لقائمة‬ ‫متطابقة‬ ‫قيم‬ ‫لها‬
queueRear
‫انتظار‬ ‫قائمة‬ ‫األخير‬ ‫ظهر‬ُ‫ي‬ ‫بينما‬ ، ‫فارغة‬ ‫انتظار‬ ‫قائمة‬ ‫األول‬ ‫يمثل‬ ، ‫ذلك‬ ‫ومع‬
‫كاملة‬
17
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
‫في‬ ‫يساعد‬
‫قائمة‬ ‫في‬ ‫العناصر‬ ‫عدد‬ ‫معرفة‬
‫االنتظار‬
18
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
19
Implementation of Queues as Arrays
(continued)
20
Empty Queue and Full Queue
21
Initialize Queue
22
Front
• Returns the first element of the queue
int cqueue::getfront()
{
return queue[front];
}
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 23
Back
• Returns the last element of the queue
int cqueue::getrear()
{
return queue[rear];
}
24
addQueue
void cqueue::inqueue(int el)
{
if (! isfull())
{
rear = ((rear+1)==size ? 0 : rear+1);
queue[rear]= el;
count ++;
}
else
cout<<"queue is full"<<endl;
}
void cqueue::inqueue(int el)
{
if (! isfull())
{
if(rear+1==size)
rear =0; // give index 0 to rear
else
rear = rear +1;
queue[rear]=el;
count++;
}
else
cout<<"queue is full"<<endl;
}
25
deleteQueue
searchQueue
26
27
print()
28
Output:
B
S
Y
K
O
Code of queue using array
29
10
20
30
40
50

10 -queues using array_07485555555510.ppt

  • 1.
  • 2.
    2 Queues • Queue: listof 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 ‫إليها‬ ‫الوصول‬ ‫يمكن‬ ‫ال‬ ‫الوسطى‬ ‫العناصر‬
  • 3.
    3 Queue Operations • Someof the queue operations are: − initializeQueue − isEmptyQueue − isFullQueue − front − back − addQueue − deleteQueue • Abstract class queueADT defines these operations
  • 6.
    6 Implementation of Queuesas 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
  • 7.
    7 Implementation of Queuesas 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
  • 8.
    8 Implementation of Queuesas Arrays (continued) • To delete an element from the queue: − Retrieve element pointed to by queueFront − Advance queueFront to next queue element
  • 9.
    9 Implementation of Queuesas 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...
  • 10.
    10 Implementation of Queuesas Arrays (continued) • The sequence AAADADADADADADADA... would eventually set queueRear to point to the last array position − Giving the impression that the queue is full
  • 11.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 11 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.
    12 Implementation of Queuesas Arrays (continued) • To advance the index in a (logically) circular array:
  • 13.
    13 Implementation of Queuesas Arrays (continued) if(rear+1==size) rear =0; // give index 0 to rear else rear = rear +1; queue[rear]=el; count++;
  • 14.
    14 Implementation of Queuesas Arrays (continued) • Case 1: if(front +1 == size) front =0; // give index 0 to front else front=front+1; count --;
  • 15.
    15 Implementation of Queuesas Arrays (continued) • Case 2: if(rear+1==size) rear =0; // give index 0 to rear else rear = rear +1; queue[rear]=el; count++;
  • 16.
    16 Implementation of Queuesas 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? ‫األشكال‬ 19 - 47 ‫و‬ 19 - 49 ‫و‬ ‫االنتظار‬ ‫لقائمة‬ ‫متطابقة‬ ‫قيم‬ ‫لها‬ queueRear ‫انتظار‬ ‫قائمة‬ ‫األخير‬ ‫ظهر‬ُ‫ي‬ ‫بينما‬ ، ‫فارغة‬ ‫انتظار‬ ‫قائمة‬ ‫األول‬ ‫يمثل‬ ، ‫ذلك‬ ‫ومع‬ ‫كاملة‬
  • 17.
    17 Implementation of Queuesas 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 ‫في‬ ‫يساعد‬ ‫قائمة‬ ‫في‬ ‫العناصر‬ ‫عدد‬ ‫معرفة‬ ‫االنتظار‬
  • 18.
    18 Implementation of Queuesas 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
  • 19.
    19 Implementation of Queuesas Arrays (continued)
  • 20.
  • 21.
  • 22.
    22 Front • Returns thefirst element of the queue int cqueue::getfront() { return queue[front]; }
  • 23.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 23 Back • Returns the last element of the queue int cqueue::getrear() { return queue[rear]; }
  • 24.
    24 addQueue void cqueue::inqueue(int el) { if(! isfull()) { rear = ((rear+1)==size ? 0 : rear+1); queue[rear]= el; count ++; } else cout<<"queue is full"<<endl; } void cqueue::inqueue(int el) { if (! isfull()) { if(rear+1==size) rear =0; // give index 0 to rear else rear = rear +1; queue[rear]=el; count++; } else cout<<"queue is full"<<endl; }
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
    Code of queueusing array 29 10 20 30 40 50