Queue
Queue
Lahore Campus
Lab- 10 Manual
Lab Instructor: Sameer Ahmed
Department of Software Engineering
Email: Sameer_ahmed@umt.edu.pk
What is a Queue in DSA?
A queue is a linear data structure that adheres to the First In, First Out (FIFO) principle. This principle
dictates that the element added first to the queue will be the first one to be removed, akin to a queue of
people waiting in line at a bank or a ticket counter. The FIFO concept ensures that the order of
processing remains consistent with the order of arrival, making queues an essential structure for many
real-world applications.
Advantages of a Queue
Uses:
Queues are widely utilized in scenarios where items need to be processed sequentially. For
instance:
Structure of a Queue:
Implementation:
1. Arrays: Fixed-size arrays can be used to implement queues, although this method can be
limited by the array's capacity.
2. Linked Lists: Linked lists provide a dynamic implementation of queues, allowing for
flexible resizing and efficient memory usage.
3. STL (Standard Template Library) in C++: The std::queue container in C++ STL
offers a built-in implementation of queues, providing a convenient and efficient way to
manage queue operations.
Operations on a Queue:
1. Enqueue (Insertion): This operation adds an element to the rear of the queue, increasing
its size.
2. Dequeue (Deletion): This operation removes an element from the front of the queue,
decreasing its size.
3. Front: This operation allows access to the first element in the queue without removing it,
providing a peek at the oldest element.
4. IsEmpty: This operation checks whether the queue is empty, returning a boolean value.
5. Size: This operation retrieves the total number of elements currently in the queue,
providing insight into the queue's occupancy.
These components and operations make queues a versatile and essential data structure in computer
science, used to manage and process data in a structured and orderly manner.
#include <iostream>
using namespace std;
class Que {
int front, rear, size;
int* queue; // Pointer to dynamically allocated array
public:
// Constructor to initialize the queue with a dynamic
size
Que(int s) {
size = s;
front = -1;
rear = -1;
queue = new int[size]; // Dynamically allocate
memory for the queue
}
q.enque(1);
q.display();
q.enque(2);
q.display();
q.enque(3);
q.display();
q.enque(4);
q.display();
q.deque();
q.display();
q.deque();
q.display();
q.deque();
q.display();
q.deque();
q.display();
q.deque();
q.display();
return 0;
}
Tasks:
You are required to implement a queue data structure in C++ using a dynamic array. Your task is
to write a user-defined program that performs the following operations on the queue. The menu
should offer five options: Insert, Delete, Display, Peek, and Exit.
Menu Options: