2. z
Queues
A Queue is a special kind of list, where
items are inserted at one end (the rear)
And deleted at the other end (the front).
Accessing the elements of queues follows
a First In First Out (FIFO) order.
Example
Like customers standing in a check-out
line in a store, the first customer in is
the first customer served.
2
3. z
Common Operations on Queues
getFRONT(Q): Returns the first element on
Queue Q.
ENQUEUE(x,Q): Inserts element x at the
end of Queue Q.
DEQUEUE(Q): Deletes the first element of
Q.
ISEMPTY(Q): Returns true if and only if Q
is an empty queue.
ISFULL(Q): Returns true if and only if Q is
full.
3
4. z
Enqueue and Dequeue
Primary queue operations: Enqueue and
Dequeue
Enqueue – insert an element at the rear of
the queue.
Dequeue – remove an element from the
front of the queue.
4
5. z
Queues Implementations
Static:
Queue is implemented by an array, and size
of queue remains fix
Dynamic:
A queue can be implemented as a linked
list and expand or shrink with each enqueue
or dequeue operation.
5
7. z
Dynamic Implementation of Queues
Dynamic implementation is done using pointers.
• Front : A pointer to the first element of the
queue.
• Rear : A pointer to the last element of the
queue.
7
x y z
Front
Rear
.
12. z Enqueue( ) Function
Void enqueue(int x)
{
/*Linked list node*/
queueNode *n = new Node();
n->setData(x);
n->Next= NULL;
12
if (front == NULL)
{
front = n;
rear = n;
size++;
}
else
{
rear->Next=n;
rear = n;
size++;
}
}
13. z
Dequeue( ) Function
int dequeue()
{
if(front == NULL)
{
cout<<“Queue is Empty“;
}
else
{ queueNode *temp=front;
int x = front->getData();
front = front->getNext();
Delete temp;
return x;
}
}
13
15. z
Display( ) Function
void display()
{
if(isEmpty())
cout<<“Queue is empty“;
else
{
for (int i=0; i<counter; i++)
cout<<queue[(front+ i)% maxSize];
}
}
15
16. z
Use of Queues
Out of the numerous uses of the queues, one of the most
useful is simulation.
A simulation program attempts to model a real-world
phenomenon.
Many popular video games are simulations, e.g., SimCity,
Flight Simulator.
Each object and action in the simulation has a counterpart in
real world.
16
17. z
Uses of Queues
If the simulation is accurate, the result of the program should
mirror the results of the real-world event.
Thus it is possible to understand what occurs in the real-world
without actually observing its occurrence.
17