Table of contents | |
Introduction | |
What is a Queue? | |
Queue Operations | |
Implementing a Queue in C++ | |
Sample Problems and Solutions |
Queues are an essential data structure in computer science, used to store and retrieve elements in a specific order. They follow the First-In-First-Out (FIFO) principle, where the element that is inserted first is the one that gets removed first. In this article, we will explore the concept of queues and how to implement them in C++.
A queue is a linear data structure that allows elements to be inserted at one end (rear) and removed from the other end (front). The order in which elements are inserted is the order in which they are processed or retrieved.
The primary operations performed on a queue are:
There are different ways to implement a queue in C++, such as using the Standard Template Library (STL) container or implementing it from scratch using arrays or linked lists. Let's explore these implementations with code examples.
Code Examples and Explanation:
Example 1: Basic Queue Operations
#include <iostream>
#include <queue>
int main() {
std::queue<int> myQueue;
myQueue.push(10);
myQueue.push(20);
myQueue.push(30);
std::cout << "Front element: " << myQueue.front() << std::endl;
myQueue.pop();
std::cout << "Front element after dequeue: " << myQueue.front() << std::endl;
std::cout << "Queue size: " << myQueue.size() << std::endl;
return 0;
}
Output:
Front element: 10
Front element after dequeue: 20
Queue size: 2
Explanation:
#include <iostream>
#include <queue>
int main() {
std::queue<std::string> myQueue;
myQueue.push("Apple");
myQueue.push("Banana");
myQueue.push("Orange");
while (!myQueue.empty()) {
std::cout << myQueue.front() << " ";
myQueue.pop();
}
return 0;
}
Output:
Apple Banana Orange
Explanation:
#include <iostream>
const int MAX_SIZE = 100;
class Queue {
private:
int arr[MAX_SIZE];
int front;
int rear;
public:
Queue() {
front = -1;
rear = -1;
}
bool isEmpty() {
return (front == -1 && rear == -1);
}
bool isFull() {
return (rear == MAX_SIZE - 1);
}
void enqueue(int data) {
if (isFull()) {
std::cout << "Queue is full. Cannot enqueue element." << std::endl;
return;
}
if (isEmpty()) {
front = 0;
}
rear++;
arr[rear] = data;
}
void dequeue() {
if (isEmpty()) {
std::cout << "Queue is empty. Cannot dequeue element." << std::endl;
return;
}
if (front == rear) {
front = -1;
rear = -1;
} else {
front++;
}
}
int getFront() {
if (isEmpty()) {
std::cout << "Queue is empty." << std::endl;
return -1;
}
return arr[front];
}
int getSize() {
return (rear - front + 1);
}
};
int main() {
Queue myQueue;
myQueue.enqueue(10);
myQueue.enqueue(20);
myQueue.enqueue(30);
std::cout << "Front element: " << myQueue.getFront() << std::endl;
myQueue.dequeue();
std::cout << "Front element after dequeue: " << myQueue.getFront() << std::endl;
std::cout << "Queue size: " << myQueue.getSize() << std::endl;
return 0;
}
Output:
Front element: 10
Front element after dequeue: 20
Queue size: 2
Explanation:
Write a C++ program to reverse the elements of a queue using a stack.
#include <iostream>
#include <queue>
#include <stack>
void reverseQueue(std::queue<int>& q) {
std::stack<int> s;
while (!q.empty()) {
s.push(q.front());
q.pop();
}
while (!s.empty()) {
q.push(s.top());
s.pop();
}
}
int main() {
std::queue<int> myQueue;
myQueue.push(10);
myQueue.push(20);
myQueue.push(30);
myQueue.push(40);
myQueue.push(50);
std::cout << "Original Queue: ";
while (!myQueue.empty()) {
std::cout << myQueue.front() << " ";
myQueue.pop();
}
std::cout << std::endl;
reverseQueue(myQueue);
std::cout << "Reversed Queue: ";
while (!myQueue.empty()) {
std::cout << myQueue.front() << " ";
myQueue.pop();
}
return 0;
}
Output:
Original Queue: 10 20 30 40 50
Reversed Queue: 50 40 30 20 10
Explanation:
Queues are fundamental data structures used in various applications for handling tasks in a specific order. In this article, we explored the concept of queues and how to implement them in C++. We discussed different approaches, including using the STL container and implementing a queue from scratch using arrays. By understanding and practicing queue operations, you can enhance your problem-solving skills and efficiently solve real-world challenges.
Remember to always consider the FIFO principle while working with queues, as it is the key principle that defines their behavior. With this knowledge, you are now equipped to use queues effectively in your C++ programs.
153 videos|115 docs|24 tests
|
|
Explore Courses for Software Development exam
|