🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
A queue is a linear data structure that follows the First In First Out (FIFO) principle. This means the first element added to the queue is the first to be removed. In this post, we'll implement a basic queue using arrays in C++.
2. Program Overview
Our queue implementation will provide the following operations:
1. enqueue: Adds an item to the rear of the queue.
2. dequeue: Removes the front item from the queue.
3. front: Returns the front item from the queue without removing it.
4. rear: Returns the rear item from the queue.
5. isEmpty: Checks if the queue is empty.
6. isFull: Checks if the queue is full.
3. Code Program
#include <iostream>
#define SIZE 1000
class Queue {
int front, rear;
public:
int arr[SIZE];
Queue() { front = -1; rear = -1; }
bool enqueue(int x);
int dequeue();
int getFront();
int getRear();
bool isEmpty();
bool isFull();
};
bool Queue::enqueue(int x) {
if (isFull()) {
std::cout << "Queue Overflow";
return false;
}
if (isEmpty()) {
front = rear = 0;
} else {
rear = (rear + 1) % SIZE;
}
arr[rear] = x;
return true;
}
int Queue::dequeue() {
if (isEmpty()) {
std::cout << "Queue Underflow";
return -1;
}
int item = arr[front];
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % SIZE;
}
return item;
}
int Queue::getFront() {
if (isEmpty()) return -1;
return arr[front];
}
int Queue::getRear() {
if (isEmpty()) return -1;
return arr[rear];
}
bool Queue::isEmpty() {
return (front == -1);
}
bool Queue::isFull() {
return ((rear + 1) % SIZE == front);
}
int main() {
Queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
std::cout << "Front element is " << q.getFront() << std::endl;
std::cout << "Dequeued element from queue is " << q.dequeue() << std::endl;
return 0;
}
Output:
Front element is 10 Dequeued element from queue is 10
4. Step By Step Explanation
The Queue class encapsulates all the necessary functions for basic queue operations. An array arr of size SIZE is used to store queue elements, while front and rear integers track the frontmost and rearmost elements in the queue.
1. The enqueue function adds an element to the rear of the queue.
2. The dequeue function removes and returns the frontmost element.
3. The getFront and getRear functions return the frontmost and rearmost elements, respectively, without removing them.
4. The isEmpty function checks if the queue is empty.
5. The isFull function checks if the queue is full.
In the main function, we demonstrate the use of these methods by enqueuing three elements to the queue, displaying the front element, and then dequeuing one element.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment