Queue
1. A queue is a linear data structure that follows the FIFO (First-In-First-Out) principle.
2. Elements are added to the end of the queue (enqueue) and removed from the front of the queue
(dequeue).
3. Queues are useful for job scheduling, print queues, and handling requests in a network.
Circular Queue
1. A circular queue is a variation of a queue where the last element is connected to the first element,
forming a circle.
2. When the queue is full and a new element is added, the oldest element is overwritten, or the queue
can be designed to wrap around to the beginning.
3. Circular queues are useful for buffering data, handling interrupts, and implementing ring buffers.
Priority Queue
1. A priority queue is a data structure where elements are ordered based on their priority.
2. Elements with higher priority are served before elements with lower priority.
3. Priority queues are useful for scheduling tasks, handling interrupts, and implementing algorithms like
Dijkstra's algorithm and Huffman coding.
Key Differences
1. *Order of Elements*: Queues follow FIFO, while priority queues follow priority-based ordering.
2. *Element Removal*: In queues, elements are removed from the front, while in priority queues,
elements are removed based on priority.
3. *Use Cases*: Queues are suitable for job scheduling and print queues, while priority queues are
suitable for task scheduling and interrupt handling.
Each data structure has its strengths and weaknesses, and the choice of which one to use depends on
the specific problem and requirements.
#include <iostream>
class Queue {
private:
int* arr;
int capacity;
int front;
int rear;
int size;
public:
// Constructor
Queue(int capacity) {
this->capacity = capacity;
this->front = 0;
this->rear = -1;
this->size = 0;
arr = new int[capacity];
// Destructor
~Queue() {
delete[] arr;
}
// Check if queue is empty
bool isEmpty() {
return (size == 0);
// Check if queue is full
bool isFull() {
return (size == capacity);
// Enqueue an element
void enqueue(int value) {
if (isFull()) {
std::cout << "Queue is full. Cannot enqueue." << std::endl;
return;
rear = (rear + 1) % capacity;
arr[rear] = value;
size++;
// Dequeue an element
int dequeue() {
if (isEmpty()) {
std::cout << "Queue is empty. Cannot dequeue." << std::endl;
return -1; // or throw an exception
int value = arr[front];
front = (front + 1) % capacity;
size--;
return value;
// Get the front element
int getFront() {
if (isEmpty()) {
std::cout << "Queue is empty." << std::endl;
return -1; // or throw an exception
return arr[front];
// Get the size of the queue
int getSize() {
return size;
// Display the queue elements
void display() {
if (isEmpty()) {
std::cout << "Queue is empty." << std::endl;
return;
int temp = front;
for (int i = 0; i < size; i++) {
std::cout << arr[temp] << " ";
temp = (temp + 1) % capacity;
std::cout << std::endl;
};
int main() {
Queue queue(5);
// Enqueue elements
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);
// Display queue elements
std::cout << "Queue elements: ";
[Link]();
// Dequeue an element
std::cout << "Dequeued element: " << [Link]() << std::endl;
// Display queue elements after dequeue
std::cout << "Queue elements after dequeue: ";
[Link]();
return 0;