0% found this document useful (0 votes)
12 views35 pages

CH 4 - Queues - New

Queues are an abstract data type that follow a first-in, first-out (FIFO) ordering principle. New items are added to the rear of the queue and items are removed from the front. Unlike stacks, queues allow access from both ends. A common implementation of queues uses a circular array, which avoids inefficient shifting of elements during removal and makes use of empty spaces. Elements are added at the rear position, calculated as (front index + number of elements) modulo the array size, and removed by incrementing the front index.

Uploaded by

rono aljehani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views35 pages

CH 4 - Queues - New

Queues are an abstract data type that follow a first-in, first-out (FIFO) ordering principle. New items are added to the rear of the queue and items are removed from the front. Unlike stacks, queues allow access from both ends. A common implementation of queues uses a circular array, which avoids inefficient shifting of elements during removal and makes use of empty spaces. Elements are added at the rear position, calculated as (front index + number of elements) modulo the array size, and removed by incrementing the front index.

Uploaded by

rono aljehani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

QUEUE

S
Queues – An Overview

Queues:
◦ Like stacks, Queues are an Abstract Data Type
◦ They are NOT built-in
◦ We must define them and their behaviours
◦ So what is a queue?
◦ A data structure that stores information in the form of a typical waiting line
◦ New items are added at the end of the queue
◦ Elements are removed from the front of the queue
◦ So unlike a stack
◦ A queue is accessible from both ends (front and end)
Queues
Access Policy:
◦ The first element that is inserted into the queue is the
first element that will leave the queue
◦ Therefore, in order for the last element to leave the queue, it must wait
until all elements preceding it are removed
◦ Known as the “First in, First out” access policy
◦ FIFO for short
◦ Real life example: waiting in line to be served
◦ When a customer arrives, they enter the line at the back
◦ They wait their turn
◦ Finally, they get to the front, are served, and exit the line
Queues
Basic Operations:
◦ enqueue:
◦ Inserts the element at the rear of the queue
◦ O(1) time
◦ dequeue:
◦ Removes the element at the front of the queue
◦ O(1) time
◦ peek:
◦ Looks at the element at the front of the queue without actually removing it
◦ O(1) time
Queues
Basic Operations:
◦ isEmpty:
◦ Checks to see if the queue is empty
◦ O(1) time
◦ isFull:
◦ Checks to see if the queue is full
◦ O(1) time
◦ clear:
◦ Clears the contents of the queue
◦ In “queue” order
◦ From front to back
◦ O(n) time
FIFO Nature of a Queue
Queues: Array
Implementation
Array Implementation:
◦ How would you implement a queue using an array?
◦ Think of what “stuff” you would need...
◦ Other than the actual array, what else do you need?
◦ Remember, you need to enqueue and dequeue
◦ Meaning:
◦ You need to ALWAYS know where the front and back of the queue are
brute-force method
Queues: Array
Implementation
Queues: Array
Implementation
Queues: Array
Implementation
What is wrong with the last example?
◦ enqueues run in O(1) time.
◦ This is a GOOD thing!
◦ But look at the dequeue
◦ How long does a dequeue take?
◦ The dequeue itself takes O(1) time
◦ However, after the first node is removed, ALL nodes, that remain in the
queue after the dequeue , must be moved forward one position in the
array
◦ Possibly n elements have to move after one dequeue
◦ This is O(n) time per deletion!
◦ And we know, conceptually, a dequeue should be O(1)
Queues: Circular Array Implementation
Circular arrays are a very common way of implementing an
array - based queue.
What is a circular array?
◦ It is a regular array
◦ We simply “view” it as being circular

In a circular implementation, the queue is considered to be


full whenever the front of the queue immediately precedes
the rear of the queue in the counter clockwise direction.
The examples on the following pages should help you to
visualize a “circular” array.
Queues: Circular Array Implementation
Queues: Circular Array Implementation
This implementation allows us to keep the elements in their
respective “cells” of the array
◦ We don’t need to move n elements during dequeues
◦ AND we also don’t have wasted space!

The circular “view” of the array allows us to “wrap” around


the array
◦ Assume the length of the array is SIZE
◦ It is NOT the case that the rear most stop at index[SIZE-1]
◦ meaning, the last element
◦ Rather, since the array wraps around, the front could be at a greater
index than the index of the rear!
Queues: Circular Array Implementation
The next several slides illustrate the operation of a circular
array based implementation of a queue.
The normal implementation (brute-force) is also shown for
comparative purposes.
However, remember that the brute force method is
extremely inefficient due to the amount of data movement
required by dequeue operations
Queues: Circular Array Implementation
Example:

Sequence of operations Sequence of operations


1 enqueuing 6 8 enqueuing 9
2 enqueuing 10 9 dequeue
3 dequeue 10 dequeue
4 enqueuing 18 11 dequeue
5 dequeue
6 dequeue
7 dequeue
Queues: Circular Array Implementation
Queues: Circular Array Implementation
Queues: Circular Array
Implementation
Queues: Circular Array
Implementation
Queues: Circular Array
Implementation
Queues: Circular Array
Implementation
Queues: Circular Array
Implementation
Queues: Circular Array
Implementation
Queues: Circular Array Implementation
Queues: Circular Array Implementation
Queues: Circular Array Implementation
Queues: Circular Array Implementation
How did we modify the position (index) of front and rear?
Did we just increment the front/rear indices as needed?
◦ Meaning, did we simply increment the index of rear every time an
enqueue occurs?
◦ And did we simply increment the index of front every time a
dequeue occurs?
◦ In a normal array, this is fine.

However, this is a circular array, and we must pay attention!


Simply incrementing will not cut it!
Queues: Circular Array Implementation
We find (and then modify) the index of front and rear using modulo arithmetic.
Ex: suppose we have the situation below

If we dequeue, the front will need to refer to the ‘10’ in index 0!


So how do we make this happen?
Can we simply increment front?
◦ Would make front refer to index 7 (out of bounds)

So, we increment front and then mod it by the queue size


◦ front = (front + 1) mod 7
◦ So now the new front refers to index 0.
Queues: Circular Array Implementation
Another method:
◦ We don’t need to save the index for the rear.
◦ Why?
◦ Because if we know the index to the front
◦ AND if we know the number of elements
◦ we can quickly determine the new enqueue position
◦ Ex: let’s say front was at index 7 and there are 2 elements
◦ This means the rear would be at index 8
◦ And the NEW enqueue position would be index 9
◦ So we see that the NEW enqueue position is found by simply adding the
index of the front and the number of elements
Queues: Circular Array Implementation
Assume we have a queue of size 10
◦ From index 0 to index 9

The front is currently index 4


And there are 6 elements already in the queue

The name of this array is myQueue


And the next operation is enqueue(g)
◦ Remember, enqueue is a function that we write in the program
◦ So this ‘g’ is sent over to the enqueue function as “char val”
Queues: Circular Array Implementation

We know that the next enqueue will go at index 0


But how do we do this in code (using mod)?
myQueue[(front + numElements)%SIZE] = val
myQueue[(4 + 6) % 10] = val
myQueue[0] = val
Circular Array implementation in Java
Circular Array implementation in Java
Circular Array implementation in Java
Circular Array implementation in Java

You might also like