DSA Assignment - 3
DSA Assignment - 3
A queue is like a line of people waiting for a service, such as a bus or a checkout counter. In
computer science, a queue is a similar concept but applied to data. It's a data structure that
holds a collection of elements in a specific order. The fundamental principle of a queue is called
"First In, First Out" (FIFO), which means that the element that has been in the queue the
longest is the first one to be removed.
A queue has two main operations: enqueue and dequeue. Enqueue adds an element to the back
of the queue, and dequeue removes the element from the front. This reflects the real-world
scenario of people entering a line from the back and leaving from the front.
Key characteristics of a queue include the front (where elements are removed), the rear (where
elements are inserted), and the size of the queue. The isEmpty operation checks if the queue is
empty, and the size operation tells us how many elements are in the queue. These features make
queues useful in various applications, from managing tasks in computer systems to handling
orders in a restaurant. The simplicity and efficiency of the queue data structure make it a
fundamental concept in computer science.
UNIK KHANAL
The different types of queues with their application are written below:
a) Linear Queue:
● They are used in operating system to manage the resources. A queue is used to handle
the use of pf shared resources like printer.
● Browsing history used linear queue to keep track of clients browsing history.
b) Circular Queue:
● Circular queue can be used in buffering system where data is transferred asynchronously
between two processes or system.
● They are used in real time event driven system like traffic light system where events are
processed in cyclic manner.
c) Priority Queue:
● They are used in task scheduling where task are performed in order of the priority level
of the task.
● Priority queue is used to find the shortest past using Dijkstra’s algorithm.
Efficient use of memory: In a circular queue, when the rear pointer reaches the end of the
queue, it wraps around to the beginning, which allows for efficient use of memory compared to a
linear queue
Easier for insertion-deletion: In the circular queue, if the queue is not fully occupied, then the
elements can be inserted easily in the vacant locations. But in the case of a linear queue,
insertion is not possible once the rear pointer reaches the last index even if there are empty
locations present in the queue.
Better performance: Circular queue offers better performance in situations where data is
frequently added and removed from the queue as compared to a linear queue.
Improved flexibility: With a circular queue, the front and rear pointers can move in either
direction, allowing for greater flexibility in implementing queue operations.
Reduced overhead: Since a circular queue requires a fixed amount of memory, it reduces the
overhead of memory allocation and deallocation as compared to a dynamic data structure like a
linked list-based linear queue.
UNIK KHANAL
In a circular queue, overflow and underflow conditions occur under certain circumstances:
1. Overflow Condition:
● Overflow in a circular queue happens when the rear pointer advances to a position
immediately following the front pointer (modulo the size of the queue). This means that
the queue is full, and no more elements can be enqueued without overwriting existing
data.
2. Underflow Condition:
The Double Ended Queue, also known as Deque is a linear data structure where the insertion and
deletion operations are performed from both ends. We can say that deque is a generalized
version of the queue. Though the insertion and deletion in a deque can be performed on both
ends, it does not follow the FIFO rule.
In input restricted queue, insertion operation can be performed at only one end, while deletion
can be performed from both ends.
UNIK KHANAL
In the output restricted queue, deletion operation can be performed at only one end, while
insertion can be performed from both ends.
UNIK KHANAL
A Queue is a linear data structure that stores elements using FIFO (First in, First Out) principle.
It is similar to the real-life entity Queue. The first element to enter a queue is the first one to
exit the queue.
A circular queue is a queue that uses an array as its underlying data structure, but treats it as a
circular buffer. This means that the front and rear pointers can wrap around the array when they
reach the end, and the queue can utilize the empty spaces at the beginning of the array. A
circular queue can perform the following operations:
Enqueue:
This operation adds an element to the rear of the queue. To do this, we first check if the queue
is full by comparing the front and rear pointers. If the queue is full, we display an error message
and return. Otherwise, we increment the rear pointer by one, modulo the size of the array. If the
rear pointer is equal to the size of the array, we set it to zero. Then, we assign the element to
the array at the rear pointer position.
Dequeue:
This operation removes an element from the front of the queue. To do this, we first check if the
queue is empty by comparing the front and rear pointers. If the queue is empty, we display an
error message and return. Otherwise, we store the element at the front pointer position in a
variable. Then, we increment the front pointer by one, modulo the size of the array. If the front
pointer is equal to the size of the array, we set it to zero. If the front and rear pointers become
equal after the increment, we set them both to -1, indicating an empty queue. Finally, we return
the stored element.
Front:
This operation returns the element at the front of the queue without removing it. To do this, we
first check if the queue is empty by comparing the front and rear pointers. If the queue is empty,
we display an error message and return. Otherwise, we return the element at the front pointer
position in the array.
UNIK KHANAL
Rear:
This operation returns the element at the rear of the queue without removing it. To do this, we
first check if the queue is empty by comparing the front and rear pointers. If the queue is empty,
we display an error message and return. Otherwise, we return the element at the rear pointer
position in the array.
IsEmpty:
This operation checks if the queue is empty by comparing the front and rear pointers. If the
queue is empty, we return true. Otherwise, we return false.
IsFull:
This operation checks if the queue is full by comparing the front and rear pointers. If the front
pointer is one position ahead of the rear pointer, or if the front pointer is zero and the rear
pointer is the last position in the array, we return true. Otherwise, we return false.
UNIK KHANAL
8. Compare Stack with Queue. How is Linear Queue different from Circular Queue?
Stacks and queues are both fundamental data structures used to manage collections of
elements, but they operate on different principles and have distinct characteristics:
1. Ordering:
Stack follows LIFO (Last In, First Out) order, while queue follows FIFO (First In, First Out)
order.
In a stack, insertions and deletions occur at the same end (top), whereas in a queue, insertions
occur at one end (rear) and deletions occur at the other end (front).
3. Operations:
Stacks support push, pop, and peek operations, while queues support enqueue, dequeue, and
front operations.
4. Use Cases:
Stacks are suitable for scenarios requiring backtracking, recursion, or maintaining state, while
queues are used for managing tasks, data buffering, or processing elements sequentially.
In summary, stacks and queues are both useful data structures with different characteristics
and applications. Stacks are ideal for managing data with LIFO behavior, while queues are
suitable for managing data with FIFO behavior.
UNIK KHANAL
Linear queue is differed from the circular queue from the following reasons:
● The memory space occupied by the linear queue is more than the circular queue.
● In a linear queue, insertion is done from the rear end, and deletion is done from the front
end.
● In the case of a linear queue, the element added in the first position is going to be
deleted in the first position. The order of operations performed on any element is fixed
i.e, FIFO.