Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Queues

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

QUEUES

DATA STRUCTURES AND ALGORITHM


WEEK 5
DIAGRAMATIC VIEW OF A DEFINITION
It is an ordered group
QUEUE
of homogeneous items
of elements.
Queues have two
ends:
• Elements are added at
one end.
• Elements are removed
from the other end.
The element added
first is also removed
first (FIFO: First In,
First Out).
OPERATIONS ON QUEUES
1.Enqueue: Add an element to the back of the queue.
2.Dequeue: Remove the element at the front of the
queue.
3.Peek: Return the element at the front of the queue
without removing it.
4.Size: Return the number of elements in the queue.
5.isEmpty: Check if the queue is empty.
OPERATIONS ON QUEUES
ENQUEUE DEQUEUE
In this process, the Dequeue operation consists of
following steps are the following steps:
performed: • Check if the queue is empty.
• Check if the queue is full. • If empty, display an underflow
• If full, produce overflow error and exit.
error and exit. • Else, the access element is
pointed out by ‘front’.
• Else, increment ‘rear’.
• Increment the ‘front’ to point to
• Add an element to the the next accessible data.
location pointed by ‘rear’. • Return success.
• Return success.
OPERATIONS ON QUEUES
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.

In the next figure, we add


element 2 to the queue by
moving the rear pointer ahead by
another increment.
OPERATIONS ON QUEUES
In the following figure, we add element
3 and move the rear pointer by 1.
At this point, the rear pointer has a value of 2
while the front pointer is at the 0th location.

Firstly, we remove the element at the front


pointer which is 1.

The first element added, 1, is the first to be


removed from the queue. After the first dequeue,
the front pointer moves ahead to the next
location, also 1.
ARRAY IMPLEMENTATION OF QUEUES
#include <iostream> // Displaying the size of the queue
#include <queue> cout << "Size of the queue: " <<
using namespace std; myQueue.size() << endl;
int main() { // Dequeue elements from the queue
// Creating a queue of integers cout << "Dequeue elements: ";
queue<int> myQueue; while (!myQueue.empty()) {
// Enqueue elements into the queue cout << myQueue.front() << "";
myQueue.push(10); myQueue.push(20); myQueue.pop(); } cout << endl;
myQueue.push(30);myQueue.push(40); // Check if the queue is empty after
myQueue.push(50); myQueue.push(60); dequeuing
// Displaying the front element of the if (myQueue.empty()) { cout <<
queue "The queue is empty now." << endl;
cout << "Front element of the queue: " } else {cout << "The queue is not
cout << myQueue.front() << endl; empty." << endl; } return 0; }
ARRAY IMPLEMENTATION OF
QUEUES
Operations on queues
Enqueue
 Function: Adds newItem to the rear of the queue.
 Preconditions: The Queue has been initialized and is not full.
 Postconditions: newItem is at the rear of the queue.
Dequeue
 Function: Removes the front item from the queue and returns it in the item.
 Preconditions: The Queue has been initialized and is not empty.
 Postconditions: The front element has been removed from the Queue and the
item is a copy of the removed element.
ARRAY IMPLEMENTATION OF
QUEUES
 Allocate memory for each new
element dynamically.
 Link the queue elements
together
 Use two pointers, Front and
Rear, to mark the front and rear
of the queue.
ARRAY IMPLEMENTATION OF
QUEUES
OPERATIONS ON QUEUES
ENQUEUE
 Function: Adds newItem to the rear of the queue.
 Preconditions: The Queue has been initialized and is not full.
 Postconditions: newItem is at the rear of the queue.
DEQUEUE
 Function: Removes the front item from the queue and returns it in the item.
 Preconditions: The Queue has been initialized and is not empty.
 Postconditions: The front element has been removed from the Queue and the item
is a copy of the removed element.
LINKED LIST IMPLEMENTATION OF
ARRAYS
 Allocate memory for each
new element dynamically.
 Link the queue elements
together
 Use two pointers, Front
and Rear, to mark the front
and rear of the queue.
LINKED LIST IMPLEMENTATION OF
QUEUES
 Create a class QNode with data members  Else set rear next to temp and then
integer data and QNode* next move rear to temp
 A parameterized constructor that takes an  Dequeue Operation:
integer x value as a parameter and sets data  If the front is set to NULL return(Base
equal to x and next as NULL Case)
 Create a class Queue with data members
 Initialize QNode temp with front and
QNode front and rear set front to its next
 Enqueue Operation with parameter x:
 If the front is equal to NULL then set
 Initialize QNode* temp with data = x the rear to NULL
 If the rear is set to NULL then set the front  Delete temp from the memory
and rear to temp and return(Base Case)
LINKED LIST IMPLEMENTATION OF
QUEUES
// C++ program for the approach in // Create a new LL node
the previous QNode* temp = new QNode(x);
#include <bits/stdc++.h> // If queue is empty, then
using namespace std; // new node is front and rear both
struct QNode { int data; if (rear == NULL) { front = rear =
QNode* next; temp;
QNode(int d) return;
{data = d; next = NULL; // Create a new LL node
} }; QNode* temp = new QNode(x);
struct Queue { QNode *front, *rear; // If the queue is empty, then
Queue() { front = rear = NULL; // new node is front and rear both
} if (rear == NULL) { front = rear =
void enQueue(int x){ temp; return; }
LINKED LIST IMPLEMENTATION OF QUEUES
// Add the new node at // If front becomes NULL, then
// the end of queue and change rear // change rear also as NULL
rear->next = temp; if (front == NULL)
rear = temp; } rear = NULL;
// Function to remove delete (temp); }};// Driver code
// a key from given queue int main( ){Queue q; q.enQueue(10);
void deQueue(){ q.enQueue(20); q.deQueue(); q.deQueue();
// If queue is empty, return NULL. q.enQueue(30); q.enQueue(40);
q.enQueue(50);q.deQueue();
if (front == NULL)
cout << "Queue Front : " <<
return; // Store previous front and
((q.front != NULL) ? (q.front)->data : -
// move front one node ahead 1)<< endl;
QNode* temp = front;
cout << "Queue Rear : " <<
front = front->next; ((q.rear != NULL) ? (q.rear)->data : -
APPLICATION OF QUEUES
 Task Scheduling: Queues can be used to schedule tasks based on priority or the
order in which they were received.
 Resource Allocation: Queues can be used to manage and allocate resources,
such as printers or CPU processing time.
 Batch Processing: Queues can be used to handle batch processing jobs, such as
data analysis or image rendering.
 Message Buffering: Queues can be used to buffer messages in communication
systems, such as message queues in messaging systems or buffers in computer
networks.
 Network protocols: Network protocols like TCP and UDP use queues to
manage packets that are transmitted over the network. Queues can help to ensure
that packets are delivered in the correct order and at the appropriate rate.
APPLICATION OF QUEUES
 Traffic Management: Queues can be used to manage traffic flow in
transportation systems, such as airport control systems or road networks.
 Operating systems: Operating systems often use queues to manage processes
and resources. For example, a process scheduler might use a queue to manage
the order in which processes are executed.
 Event Handling: Queues can be used to handle events in event-driven systems,
such as GUI applications or simulation systems.
 Printer queues: In printing systems, queues are used to manage the order in
which print jobs are processed. Jobs are added to the queue as they are
submitted, and the printer processes them in the order they were received.
 Web servers: Web servers use queues to manage incoming requests from
clients. Requests are added to the queue as they are received, and they are
processed by the server in the order they were received.
ISSUES IN APPLICATION OF LINKED LIST
 Queue overflow: If a queue has a fixed size, it can become full, leading to a
queue overflow. This can happen if elements are added to the queue faster
than they are removed. To prevent overflow, some implementations use
dynamic resizing or circular buffers.
 Queue underflow: If a queue is empty and an attempt is made to remove an
element, this can lead to a queue underflow. This can happen if elements are
removed from the queue faster than they are added. To prevent underflow,
some implementations use sentinel values or null pointers to represent an
empty queue.
 Blocking queues: In some applications, a queue may become blocked if it is
full or empty. This can cause delays in processing or deadlock. To address
this, some implementations use bounded queues or non-blocking queues.
ISSUES IN APPLICATION OF LINKED LIST
 Priority inversion: In some applications, a higher priority element can get
stuck behind a lower priority element in the queue. This can lead to priority
inversion and result in reduced performance. To prevent this, some
implementations use priority queues or multiple queues with different priorities.
 Synchronization issues: In concurrent applications, multiple threads may
access the same queue simultaneously. This can lead to synchronization issues
like race conditions, deadlocks, and livelocks. To address this, some
implementations use locking mechanisms like mutexes or semaphores.
 Memory management: In some implementations, a queue may allocate and
deallocate memory frequently, leading to memory fragmentation and reduced
performance. To address this, some implementations use memory pools or pre-
allocated buffers.

You might also like