Queue: Didih Rizki Chandranegara
Queue: Didih Rizki Chandranegara
Time Complexity
Average Worst
Access Search Insertion Deletion Access Search Insertion Deletion
θ(n) θ(n) θ(1) θ(1) O(n) O(n) O(1) O(1)
Queue Representation
As we now understand that in queue, we access both ends
for different reasons. The following diagram given below
tries to explain queue representation as data structure −
Queue Representation
As in stacks, a queue can also be implemented like, Arrays
and Linked-lists. For the sake of simplicity, we shall
implement queues using one-dimensional array.
Basic Operations
Queue operations may involve initializing or defining the
queue, utilizing it, and then completely erasing it from the
memory. Here we shall try to understand the basic
operations associated with queues −
1. enqueue() − add (store) an item to the queue.
2. dequeue() − remove (access) an item from the queue.
Basic Operations
Few more functions are required to make the above-
mentioned queue operation efficient. These are −
1. peek() − Gets the element at the front of the queue
without removing it.
2. isfull() − Checks if the queue is full.
3. isempty() − Checks if the queue is empty.
Basic Operations
In queue, we always dequeue (or access) data, pointed by
front pointer and while enqueueing (or storing) data in the
queue we take help of rear pointer.
peek()
This function helps to see the data at the front of the queue.
The algorithm of peek() function is as follows −
begin procedure peek
return queue[front]
end procedure
peek()
Implementation of peek() function in C programming
language −
int peek() {
return queue[front];
}
isfull()
As we are using single dimension array to implement
queue, we just check for the rear pointer to reach at
MAXSIZE to determine that the queue is full. In case we
maintain the queue in a circular linked-list, the algorithm
will differ. Algorithm of isfull() function −
isfull()
begin procedure isfull
end procedure
isfull()
Implementation of isfull() function in C programming
language −
bool isfull() {
if(rear == MAXSIZE - 1)
return true;
else
return false;
}
isempty()
Algorithm of isempty() function −
begin procedure isempty
if front is less than MIN OR
front is greater than rear
return true
else
return false
endif
end procedure
isempty()
If the value of front is less than MIN or 0, it tells that the
queue is not yet initialized, hence empty. Here's the C
programming code −
bool isempty() {
if(front < 0 || front > rear)
return true;
else
return false;
}
Enqueue Operation
Queues maintain two data pointers, front and rear.
Therefore, its operations are comparatively difficult to
implement than that of stacks.
The following steps should be taken to enqueue (insert)
data into a queue −
Enqueue Operation
Step 1 − Check if the queue is full.
Step 2 − If the queue is full, produce overflow error and exit.
Step 3 − If the queue is not full, increment rear pointer to
point the next empty space.
Step 4 − Add data element to the queue location, where the
rear is pointing.
Step 5 − return success.
Enqueue Operation
There is queue like this :
3 2 1 0
C B A
Rear Front
Enqueue Operation
Insert D in queue
D 3 2 1 0
C B A
Rear Front
Enqueue Operation
D insert at last queue
3 2 1 0
D C B A
Rear Front
Enqueue Operation
Then, set D as Rear and result is here :
3 2 1 0
D C B A
Rear Front
Algorithm for enqueue operation
procedure enqueue(data)
if queue is full
return overflow
endif
rear ← rear + 1
queue[rear] ← data
return true
end procedure
Implementation of enqueue() in C
programming language
int enqueue(int data)
if(isfull())
return 0;
rear = rear + 1;
queue[rear] = data;
return 1;
end procedure
Dequeue Operation
Accessing data from the queue is a process of two tasks −
access the data where front is pointing and remove the data
after access. The following steps are taken to perform
dequeue operation −
Dequeue Operation
Step 1 − Check if the queue is empty.
Step 2 − If the queue is empty, produce underflow error and
exit.
Step 3 − If the queue is not empty, access the data where front is
pointing.
Step 4 − Increment front pointer to point to the next available
data element.
Step 5 − Return success.
Dequeue Operation
There is queue like this :
3 2 1 0
D C B A
Rear Front
Dequeue Operation
Dequeue operation would be removed the first element or
FIFO
3 2 1 0
D C B A
Rear Front
Dequeue Operation
Dequeue operation would be removed the first element or
FIFO
3 2 1 0
D C B
A
Rear Front
Dequeue Operation
Then set, B as front
3 2 1 0
D C B
A
Rear Front
Dequeue Operation
The result is :
3 2 1 0
D C B
Rear Front
Dequeue Operation
The value of the front variable is B, therefore, we can not
reinsert the values in the place of already deleted element
before the position of front. That much space of the array is
wasted and can not be used in the future (for this queue).
3 1 0
D C B
Rear Front
Algorithm for dequeue operation
procedure dequeue
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure
Implementation of dequeue() in C
programming language
int dequeue() {
if(isempty())
return 0;
return data;
}
Queue Using Array
We can easily represent queue by using linear arrays. There
are two variables i.e. front and rear, that are implemented in
the case of every queue.
One of the most common problem with array
implementation is the size of the array which requires to be
declared in advance.
Queue Using Array
Initially, the value of front and queue is -1 which represents
an empty queue. Array representation of a queue containing
5 elements along with the respective values of front and
rear, is shown in the following figure.
front rear
H E L L O
0 1 2 3 4 5
Queue Using Array
The below figure shows the queue of characters forming
the English word "HELLO". Since, No deletion is
performed in the queue till now, therefore the value of front
remains -1 .
front rear
H E L L O
0 1 2 3 4 5
Queue Using Array
However, the value of rear increases by one every time an
insertion is performed in the queue.
front rear
H E L L O
0 1 2 3 4 5
Queue Using Array
After inserting an element into the queue shown in the
below figure, the queue will look something like following.
The value of rear will become 5 while the value of front
remains same. Insert G in queue
front rear
H E L L O G
0 1 2 3 4 5
Queue Using Array
After deleting an element, the value of front will increase
from -1 to 0. however, the queue will look something like
following.
front rear
E L L O G
0 1 2 3 4 5
Queue Using Array
Check if the queue is already full by comparing rear to max
- 1. if so, then return an overflow error.
If the item is to be inserted as the first element in the list, in
that case set the value of front and rear to 0 and insert the
element at the rear end.
Otherwise keep increasing the value of rear and insert each
element one by one having rear as the index.
Queue Using Array
Although, the technique of creating a queue is easy, but
there are some drawbacks of using this technique to
implement a queue.
Memory wastage : The space of the array, which is used to
store queue elements, can never be reused to store the
elements of that queue because the elements can only be
inserted at front end and the value of front might be so high
so that, all the space before that, can never be filled.
Queue Using Array
The below figure shows how the memory space is wasted
in the array representation of queue. In the figure, a queue
of size 10 having 3 elements, is shown.
Queue Using Array
The value of the front variable is 5, therefore, we can not
reinsert the values in the place of already deleted element
before the position of front. That much space of the array is
wasted and can not be used in the future (for this queue).
To resolve this problem, we can use Deciding the array
size
Queue Using Array
Deciding the array size
On of the most common problem with array
implementation is the size of the array which requires to be
declared in advance. Due to the fact that, the queue can be
extended at runtime depending upon the problem, the
extension in the array size is a time taking process and
almost impossible to be performed at runtime since a lot of
reallocations take place.
Queue Using Array
Deciding the array size
Due to this reason, we can declare the array large enough
so that we can store queue elements as enough as possible
but the main problem with this declaration is that, most of
the array slots (nearly half) can never be reused. It will
again lead to memory wastage.
Queue Using Array
Enqueue(Q, x) Dequeue(Q, x)
if isFull(Q) if isEmpty(Q)
Error “Queue Overflow” Error “Queue Underflow”
else else
Q[Q.tail] = x x = Q[Q.head]
if Q.tail == Q.size if Q.head == Q.size
Q.tail = 1 Q.head = 1
else else
Q.tail = Q.tail+1 Q.head = Q.head+1
return x
Queue Using Linked List
As we know that a linked list is a dynamic data structure
and we can change the size of it whenever it is needed.
So, we are not going to consider that there is a maximum
size of the queue and thus the queue will never overflow.
However, one can set a maximum size to restrict the linked
list from growing more than that size.
Head = front, Tail = rear
Queue Using Linked List
As told earlier, we are going to maintain a head and a tail
pointer to the queue. In the case of an empty queue, head
will point to NULL.
Queue Using Linked List
We will point the head pointer to the first element of the
linked list and the tail pointer to the last element of it as
shown in the picture given below.
1 5 9 4 null
head tail
Queue Using Linked List
The enqueue operation simply adds a new element to the
last of a linked list.
1 5 9 4 10 null
head tail
Queue Using Linked List
The enqueue operation simply adds a new element to the
last of a linked list.
1 5 9 4 10 null
head tail
Queue Using Linked List
However, if the queue is empty, we will simply make the
new node head and tail of the queue.
Head, Tail
Queue Using Linked List
To dequeue, we need to remove the head of the linked list.
To do so, we will first store its data in a variable because
we will return it at last and then point head to its next
element.
1 5 9 4 10 null
head tail
Queue Using Linked List
To dequeue, we need to remove the head of the linked list.
To do so, we will first store its data in a variable because
we will return it at last and then point head to its next
element.
5 9 4 10 null
head tail
Queue Using Linked List
ENQUEUE(Q, n) DEQUEUE(Q, n)
if IS_EMPTY(Q) if IS_EMPTY(Q)
Q.head = n Error "Queue Underflow"
Q.tail = n else
else x = Q.head.data
Q.tail.next = n Q.head = Q.head.next
Q.tail = n return x
Adding References
https://www.tutorialspoint.com/data_structures_algorithms/
dsa_queue.htm
https://www.javatpoint.com/data-structure-queue
https://www.geeksforgeeks.org/queue-data-structure/
https://www.codesdope.com/course/data-structures-queue/
https://www.studytonight.com/data-structures/queue-data-s
tructure
Adding References
https://www.javatpoint.com/array-representation-of-queue
https://www.javatpoint.com/linked-list-implementation-of-
queue
https://www.hackerearth.com/practice/data-structures/queu
es/basics-of-queues/tutorial/
https://www.programiz.com/dsa/queue
EMAIL
[email protected] (work)
[email protected] (personal)
PHONE : 081349254787