Unit 3 Stack Queue - NC
Unit 3 Stack Queue - NC
Concept of stack
• Stack Data Structure is a linear data structure that follows LIFO (Last In First
Out) Principle , so the last element inserted is the first to be popped out.
Applications of Stack
Recursion
Infix to Postfix
Infix to prefix
Postfix Evaluation
Prefix Evaluation
Recursion
• Step 1: Reverse the infix expression. Note while reversing each ‘(‘ will
become ‘)’ and each ‘)’ becomes ‘(‘.
• Step 2: Convert the reversed infix expression to “nearly” postfix
expression.
• While converting to postfix expression, instead of using pop operation
to pop operators with greater than or equal precedence, here we will
only pop the operators from stack that have greater precedence.
• Step 3: Reverse the postfix expression.
• a+b
queuename.size()
Parameters :
No parameters are passed
Returns :
Number of elements in the container
Array implementation of queue
To implement a queue using a simple array,
• create an array arr of size n and
• take two variables front and rear which are initialized as 0 and -1
respectively
• rear is the index up to which the elements are stored in the array
including the rear index itself. We mainly add an item by
incrementing it.
• front is the index of the first element of the array. We mainly
remove the element at this index in Dequeue operation.
Class Structure
#include <iostream>
using namespace std;
class Queue {
private: int front, rear, size;
int arr[MAX];
public:
Queue()
{
front = 0;
rear = -1;
size = 0;
}
};
enqueue
void enqueue(int value) {
if (size == MAX) {
cout << "Queue Overflow\n";
return;
}
rear = (rear + 1) % MAX;
arr[rear] = value;
size++;
cout << "Enqueued " << value << endl;
}
dequeue
int dequeue()
{
if (size == 0)
{
cout << "Queue Underflow\n";
return -1;
}
int value = arr[front];
front = (front + 1) % MAX;
size--;
cout << "Dequeued " << value << endl;
return value;
}
// Function to get the front element of the queue
int peek()
{
if (size == 0)
{
cout << "Queue is Empty\n";
return -1;
}
return arr[front];
}
// Function to check if the queue is empty bool
isEmpty()
{
return size == 0;
}
// Function to get the size of the queue
int getSize()
{
return size;
}
Circular Queue
• A Circular Queue is an extended
version of a normal queue where the
last element of the queue is
connected to the first element of the
queue forming a circle.
• The operations are performed based
on FIFO (First In First Out) principle.
It is also called ‘Ring Buffer’.
• In a normal Queue, we can insert
elements until queue becomes full.
But once queue becomes full, we
can not insert the next element even
if there is a space in front of queue.
Operations on Circular Queue
• Front: Get the front item from the queue.
• Rear: Get the last item from the queue.
• enQueue(value) This function is used to insert an element into the
circular queue. In a circular queue, the new element is always inserted
at the rear position.
• Check whether the queue is full – [i.e., the rear end is in just before the front
end in a circular manner].
• If it is full then display Queue is full.
• If the queue is not full then, insert an element at the end of the queue.
• deQueue() This function is used to delete an element from the
circular queue. In a circular queue, the element is always deleted from
the front position.
• Check whether the queue is Empty.
• If it is empty then display Queue is empty.
• If the queue is not empty, then get the last element and remove it from the queue.
Implement Circular Queue using Array
1.Initialize an array queue of size n, where n is the maximum number of
elements that the queue can hold.
2.Initialize two variables front and rear to -1.
3.Enqueue: To enqueue an element x into the queue, do the following:
1. Increment rear by 1.
1. If rear is equal to n, set rear to 0.
2. If front is -1, set front to 0.
3. Set queue[rear] to x.
4.Dequeue: To dequeue an element from the queue, do the following:
1. Check if the queue is empty by checking if front is -1.
1. If it is, return an error message indicating that the queue is empty.
2. Set x to queue[front].
3. If front is equal to rear, set front and rear to -1.
4. Otherwise, increment front by 1 and if front is equal to n, set front to 0.
5. Return x.
Double Ended Queue (Deque)
• Deque or Double Ended Queue is a generalized version of
Queue data structure that allows insert and delete at both
ends.
Operations on
Deque
Insertion at the front end
In this operation, the element is inserted from the front end of the
queue. Before implementing the operation, we first have to check
whether the queue is full or not. If the queue is not full, then the
element can be inserted from the front end by using the below
conditions -
• If the queue is empty, both rear and front are initialized with 0.
Now, both will point to the first element.
• Otherwise, check the position of the front if the front is less than 1
(front < 1), then reinitialize it by front = n - 1, i.e., the last index of
the array.
Insertion at the rear end
In this operation, the element is inserted from the rear end of the
queue. Before implementing the operation, we first have to check
again whether the queue is full or not. If the queue is not full, then
the element can be inserted from the rear end by using the below
conditions
• If the queue is empty, both rear and front are initialized with 0. Now,
both will point to the first element.
• Otherwise, increment the rear by 1. If the rear is at last index (or size
- 1), then instead of increasing it by 1, we have to make it equal to 0.
Deletion at the front end
In this operation, the element is deleted from the front end of the
queue. Before implementing the operation, we first have to check
whether the queue is empty or not.
• If the queue is empty, i.e., front = -1, it is the underflow condition,
and we cannot perform the deletion. If the queue is not full, then the
element can be inserted from the front end by using the below
conditions
• If the deque has only one element, set rear = -1 and front = -1.
• Else if front is at end (that means front = size - 1), set front = 0.
• Else increment the front by 1, (i.e., front = front + 1).
Deletion at the rear end
In this operation, the element is deleted from the rear end of the
queue. Before implementing the operation, we first have to check
whether the queue is empty or not.
• If the queue is empty, i.e., front = -1, it is the underflow condition,
and we cannot perform the deletion.
• If the deque has only one element, set rear = -1 and front = -1.
• If rear = 0 (rear is at front), then set rear = n - 1.
• Else, decrement the rear by 1 (or, rear = rear -1).
Check empty & Full
• Empty operation is performed to check whether the deque is
empty or not. If front = -1, it means that the deque is empty.
• Full operation is performed to check whether the deque is full or
not. If front = rear + 1, or front = 0 and rear = n - 1 it means that the
deque is full.
Applications of queue
• Task scheduling in operating systems
• Data transfer in network communication
• Simulation of real-world systems (e.g., waiting lines)
• Priority queues for event processing queues for event processing
Priority Queue
• A priority queue is an abstract data type that behaves similarly to the
normal queue except that each element has some priority, i.e., the
element with the highest priority would come first in a priority queue.
The priority of the elements in a priority queue will determine the
order in which elements are removed from the priority queue.
• The priority queue supports only comparable elements, which means
that the elements are either arranged in an ascending or descending
order.
Characteristics of a Priority queue
• Every element in a priority queue has some priority associated with it.
• An element with the higher priority will be deleted before the
deletion of the lesser priority.
• If two elements in a priority queue have the same priority, they will
be arranged using the FIFO principle.
Example
• 1, 3, 4, 8, 14, 22
All the values are arranged in ascending order. Now, we will observe how the
priority queue will look after performing the following operations:
• poll(): This function will remove the highest priority element from the priority
queue. In the above priority queue, the '1' element has the highest priority, so it
will be removed from the priority queue.
• add(2): This function will insert '2' element in a priority queue. As 2 is the
smallest element among all the numbers so it will obtain the highest priority.
• poll(): It will remove '2' element from the priority queue as it has the highest
priority queue.
• add(5): It will insert 5 element after 4 as 5 is larger than 4 and lesser than 8, so it
will obtain the third highest priority in a priority queue.
Types of Priority Queue
• Ascending order priority
queue: In ascending order
priority queue, a lower
priority number is given as a
higher priority in a priority.
For example, we take the
numbers from 1 to 5 arranged
in an ascending order like
1,2,3,4,5; therefore, the
smallest number, i.e., 1 is
given as the highest priority in
a priority queue.
• Descending order priority
queue: In descending order
priority queue, a higher
priority number is given as a
higher priority in a priority.
For example, we take the
numbers from 1 to 5
arranged in descending order
like 5, 4, 3, 2, 1; therefore,
the largest number, i.e., 5 is
given as the highest priority
in a priority queue.
n-gl.com