Data Structure
Second Year Students
Department of Computer Science
Dr. Rehab Emad El-Dein Sayed
FCI- Minia University
Course Syllabus
01 Introduction to Data Structure
02 Arrays
03 Stacks
04 Queues
05 Linked List
06 Trees
07 Graphs
Queue
▪ Introduction to Queue.
▪ Types of Queue.
▪ Basic Operations of Queue.
▪ Applications of Queue.
▪ Implementation
Introduction
Introduction
Definition
▪ A queue is a linear data structure that is open at both ends and the operations are performed in First In First Out
(FIFO) order.
▪ A queue is a basic data structure just like a stack.
▪ In contrast to stack that uses the LIFO approach.
▪ A queue can be defined as an ordered list that enables insert operations to be performed at one end called REAR and delete
operations to be performed at another end called FRONT.
▪ A queue is referred to as the First In First Out list.
FIFO Principle of Queue:
▪ A Queue is like a line waiting to purchase tickets, where the first person in line is the first person served. (i.e. First come first
serve).
▪ The position of the entry in a queue ready to be served, that is, the first entry that will be removed from the queue, is called
the front of the queue (sometimes, head of the queue), similarly, the position of the last entry in the queue, that is, the one
most recently added, is called the rear (or the tail) of the queue. See the below figure.
What is Queue Data Structure
▪ In the above image, since 1 was kept in the queue before 2, it is the first to be removed
from the queue as well. It follows the FIFO rule.
▪ In programming terms, putting items in the queue is called enqueue, and removing
items from the queue is called dequeue.
Types of Queues
Types of Queues
1. Simple Queue.
2. Circular Queue.
3. Double-ended Queue.
4. Priority Queue.
Simple Queue
Simple Queue
Simple Queue
▪ In a simple queue, insertion takes place at the rear and removal occurs at the front.
▪ It strictly follows the FIFO (First in First out) rule.
Basic Operations of Queue
Operations of Queue
▪ A queue is a linear data structure.
▪ It is controlled by two operations: insertion and deletion.
▪ Insertion operation takes place from the rear end of the queue and
▪ deletion operation takes place from the front end of the queue.
▪ Insertion operation adds an element to the queue.
▪ Deletion operation removes an element from the queue.
▪ There are two variables FRONT and REAR.
▪ FRONT points to the beginning of the filled queue and takes care of the deletion operation.
▪ REAR points to the end of the queue and takes care of the insertion operation.
▪ These two operations implement the FIFO method.
Operations of Queue
A queue is an object (an abstract data structure - ADT) that allows the following operations:
▪ Enqueue: Add an element to the end of the queue (The addition of an item to the queue is
always done at the rear of the queue).
▪ Dequeue: Remove an element from the front of the queue (An item is removed or de-
queued always from the front of the queue).
▪ IsEmpty: Check if the queue is empty.
▪ IsFull: Check if the queue is full.
▪ Peek: Get the value of the front of the queue without removing it.
Operations of Queue
Queue Representation:
1. Array Representation of Queue:
▪ Like stacks, Queues can also be represented in an array: In this representation, the Queue is implemented using
the array. Variables used in this case are
▪ Queue: the name of the array storing queue elements.
▪ Front: the index where the first element is stored in the array representing the queue.
▪ Rear: the index where the last element is stored in an array representing the queue.
Enqueue Operation
▪ Initially when the queue is empty, FRONT and REAR can have any integer value other than any valid index
number of the array. Let FRONT = -1; REAR=-1;
▪ For the first element in the empty queue, set the value of both FRONT and REAR to 0.
▪ After this every insertion operation increase the REAR by 1.
▪ add the new element in the position pointed to by REAR.
▪ As arrays are fixed in length, elements can not be inserted beyond the maximum size of the array. Pushing data
beyond the maximum size of the queue (i.e. when REAR = MAX SIZE -1) results in “data overflow”.
▪ Check if the queue is full.
Enqueue Operation
This is an empty queue and thus we have the rear and empty set to -1.
Next, we add 1 to the queue and as a result, the rear pointer moves ahead by one location.
We add element 2 to the queue by moving the rear pointer ahead by another increment.
We add element 3 and move the rear pointer by 1.
Enqueue Operation
Enqueue Operation
Dequeue Operation
▪ Check if the queue is empty.
▪ Return the value pointed by FRONT.
▪ Increase the FRONT index by 1.
▪ For the last element, reset the values of FRONT and REAR to -1.
Dequeue Operation
Dequeue Operation
Implementation of Queue
Queue Implementation
Queue Implementation
Queue Implementation
Queue Implementation
Queue Implementation
Queue Implementation
Application of Queue
Applications of Queue
Applications of Queue:
▪ CPU scheduling, Disk Scheduling
▪ When data is transferred asynchronously between two processes. The queue is used for
synchronization. For example: IO Buffers, pipes, file IO, etc.
▪ Handling of interrupts in real-time systems.
▪ Call Center phone systems use Queues to hold people calling them in order.
Limitation of Queue
Simple Queue Limitation
▪ As you can see in the image below, after a bit of enqueuing and dequeuing, the size of the
queue has been reduced.
▪ And we can only add indexes 0 and 1 only when the queue is reset (when all the elements
have been dequeued).
▪ After REAR reaches the last index, if we can store extra elements in the empty spaces (0 and
1), we can make use of the empty spaces. This is implemented by a modified queue called
the circular queue.
Thank You