Queue
Queue
Queue
SY BSc IT
UNIT - 02
DATA STRUCTURE
01 - Ansari Mariya.
41 - Samiksha Patil (KL).
45 - Shakshi P Mishra.
Introduction to Queue
•
Queue Data Structure is a linear data structure that follows FIFO
(First In First Out) Principle, so the first element inserted is
the first to be popped out.
•
A queue is also defined as a list that is open at both ends.
•
The addition of new items happens at one end, called the “rear”
(tail) and the removal of existing items occurs at the other end,
commonly called the “front” (head).
•
Items in a queue are called elements. They can be of any data type.
•
Example: Ticket Counter, etc.
•
Basic Operations
in Queue
Some of the basic operations for Queue in Data Structure are:
•
Enqueue( ): Adds (or stores) an element to the end of the queue.
•
Dequeue( ): Removal of elements from the queue.
•
Peek ( ): Acquires the data element available at the front node of
the queue without deleting it.
•
isFull ( ): Validates if the queue is full.
•
isEmpty ( ): Checks if the queue is empty.
•
size( ): Returns the number of elements in the queue.
•
Overflow & Underflow Condition
•
Overflow:
Overflow occurs when we attempt to add an element to a queue that is
already full (i.e., it has reached its maximum capacity).
In other words, if the queue cannot accommodate any more elements due
to space limitations, we say it has overflowed.
•
•
Underflow:
Underflow happens when we try to remove an element from an empty
queue (i.e., a queue with no elements).
Essentially, if there are no items in the queue, we cannot dequeue any
further, leading to an underflow condition.
•
●
Types of Queues
There are 4 types of Queues:
1. Simple Queue:
Simple Queue simply follows FIFO Structure. We can only insert the
element at the back and remove the element from the front of the
queue.
1.
2. Circular Queue:
Circular queue is a special type of queue where the last position
is connected back to the first position.
It is also called as Ring Buffer(Cyclic Buffer).
1.
3. Priority Queue:
A priority queue is a type of queue where the elements are accessed
based on the priority assigned to them.
While removing an element from a priority queue, the data item with
the highest priority is removed first.
In priority queue, the insertion is performed on the order of arrival and
deletion is performed based on priority.
•
Using the contiguous memory like in array:
In this representation the queue is implemented using the array.
Variables used in this case are
•
•
Using the Contiguous memory like in
Array
•
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 the array
representing the queue.
•
MAX - defining that how many elements (maximum count) can be
stored in the array representing the queue.
Using the Non-
Contiguous Memory
•
In this representation the queue is implemented using the
dynamic data structure Linked list.
•
Using linked list for creating a queue makes it flexible in terms of
size and storage.
•
You don't have to define the maximum number of elements in
the queue.
Pointers(links) to store addresses of nodes for defining a queue are.
FRONT – address of the first element of the Linked list storing
the queue.
REAR - address of the last element of the linked list storing the queue.
• Elements are stored in separate memory
locations, linked via Pointers.
• Dynamic size, growing as needed without reallocation.
• Elements are stored in a continuous block of memory.
• Fixed size, requiring resizing or reallocation when capacity is
exceeded.
Array Representation of Queue
Similar to stack , Queue is a linear data structure that follows a
particular order in which the operations are performed for storing
data. i.e. first – in, first – out.
•
In this method , elements are stored in a fixed – size array two
pointers, called front and rear, in this front keeps track of
first element and rear keeps the track of last elements in the
queue.
•
Front and rear variables point to the position from where
insertions and deletions are performed in a queue.
•
Initially , the value of front and rear is -1 which represents an
empty queue.
QUEUE
•
Sequential Allocation: It can be implemented using an array. A queue
implemented using an array can organize only a limited number of elements.
Enqueue adds an element to the rear , dequeue removes an element from
the front.
•
Link list Allocation: It can be implemented using a linked list. A queue
implemented using a linked list can organize unlimited elements. It uses a
linked node where enqueue adds a node at the rear dequeue removes a node
Tofrom the front.
perform enqueue & dequeue these operations are required:
•
Peek ( ) - Returns the element at the front of the queue without removing it.
•
isFull ( ) - Checks if the queue is empty.
•
isEmpty ( ) - Checks if the queue is full.
•
Algorithm & Code for Enqueue
STEP 1: Check if the queue is full.
int enqueue(int data)
STEP 2: If the queue is full , produce {
overflow error and exit if(isfull ( ))
Return 0;
STEP 3: If the queue is not full, rear rear + 1;
increment rear flow to point the next queue[rear] = data;
empty space.
return 1;
STEP 4: Add data element to the queue
}
location , where the rear is pointing. end procedure