Dsa521s Que 2
Dsa521s Que 2
What is a Queue?
A queue is a linear data structure that follows a rule for insertion and deletion of elements. The
insertion is done from one end and deletion is done from the other end. That means, the first
element into the queue is the first element out of the queue (FIFO) or the last element into the
queue, is the last element out of the queue (LILO).
Insertion in a queue is done at the rear / tail, and deletion is done at the front / head of the queue.
The technical name for insertion is enqueue, and for deletion is dequeue.
Below is a logical representation of a queue.
rear
0 1 2
dequeue 5 8 3 enqueue
583 583
front
Operations of a queue
Enqueue and dequeue are the two main operations of a queue. Below are some operations of a
queue.
Enqueue()
Dequeue()
Front() / Peek()
Display()
isFull()
isEmpty
The table below shows four main operations of a stack and their description.
Insert an Delete / remove an Returns the Return true if the Return true if the
element into element from the element at the queue is empty. queue is full.
the queue. queue. front of the isEmpty() isFull()
enqueue(5) dequeue() queue. It does
not remove the
element.
peek()
Applications of a queue
Shared resources e.g. Printer
o A printer will put jobs on a queue if many requests are sent to it
Call centres (customer care)
o A customer is put on hold on a queue if all representatives are busy
Processor
o Requests to the processor are put in a queue
Implementation of a queue
A queue can be implemented using an array, linked list or a stack.
rear
0 1 2
dequeue 5 8 3 enqueue
583 583
front
Given the queue above, let’s look at the implementation of queue using arrays.
Main Function
Note: We can write a pseudocode in the main function to allow the user to choose whether
to insert, delete, peek or to display elements in the queue, then feed the choice to a case
structure that will call the respective functions to perform that operation. Let’s start with
the main function below.
START
enqueue(5)
enqueue(8)
enqueue(3)
display()
peek()
dequeue()
peek()
display()
END
int size = 3
int queue[] = new int[size] //declaring array called queue
front = -1 //setting empty queue
rear = -1 //setting empty queue
enqueue(num){
IF(rear == size - 1)THEN //check if the queue is full
DISPLAY “Queue is full”
ELSE IF(front == -1 AND rear == -1)THEN //check if the queue empty
front = 0 //move front to index 0
rear = 0 //move rear to index 0
queue[rear] = num //insert element in queue
ELSE
rear ++ //move rear to next index position
queue[rear] = num //insert element in queue
ENDIF
}
Dequeue / delete element from queue
dequeue(){
IF(front == -1 AND rear == -1)THEN //check if the queue is empty
DISPLAY “Queue is empty”
ELSE IF(front == rear)
front = -1 //setting empty queue
rear = -1 //setting empty queue
ELSE
DISPLAY “Deleted element is: ” queue[front]
front ++ //delete element from queue
ENDIF
}
display(){
IF(front == -1 AND rear == -1)THEN //check if the queue is empty
DISPLAY “Queue is empty”
ELSE
FOR(i = front; i < rear + 1; i++)
DISPLAY queue[i] //display elements of queue
ENDFOR
ENDIF
}
peek(){
IF(front == -1 AND rear == -1)THEN //check if the queue is empty
DISPLAY “Queue is empty”
ELSE
DISPLAY queue[front] //display element in front of queue
ENDIF
}
Implementation of a queue (Using Linked List)
Implementing a queue using linked list means we are going use dynamic memory allocation,
while following the FIFO rule. Below is a logical representation of a singly linked list.
head
rear
300
front
Main Function
START
enqueue(5)
enqueue(2)
enqueue(8)
display()
dequeue()
peek()
END
Insertion in a queue is done from the rear. That means we must have a pointer to control
the insertion. The name of the pointer here is rear.
To display the elements of the queue, we need to maintain an extra pointer to help us to
traverse the queue. The name of this pointer in the example below is temp.
display(){
IF(front == null AND rear == null)THEN //check if queue is empty
DISPLAY “Queue is empty”
ELSE
temp = front //point temp to first node
WHILE(temp != null)
DISPALY temp -> data //display element
temp = temp -> next //move temp pointer to next node
ENDWHILE
ENDIF
}
Deletion in a queue is done from the front. That means we must have a pointer to control
the deletion. The name of the pointer here is front. We need an extra pointer to allow us to
completely delete the node. This pointer is temp in the example below.
dequeue(){
temp = front //point temp pointer to first node
IF(front == null AND rear == null)THEN //check if queue is empty
DISPLAY “Queue is empty”
ELSE
DISPLAY “The deleted element is: ” front -> data
front = front -> next //move front pointer to next node
temp = free //or use free(temp) to free the unused node
ENDIF
}
Peek the queue
peek(){
IF(front == null AND rear == null)THEN //check if queue is empty
DISPLAY “Queue is empty”
ELSE
DISPLAY “Front element is: ” front -> data //display element
ENDIF
}
Additional resources
1. https://www.geeksforgeeks.org/introduction-to-
queue-data-structure-and-algorithm-tutorials/
2. https://www.studytonight.com/data
structures/tests/2
3. https://ocw.mit.edu/courses/1-00-introduction-to-
computers-and-engineering-problem-solving-spring-
2012/86ffe1bca574b95472301b05d8469aad_MIT1_00S
12_Lec_35.pdf