Queue in C++
A queue is a linear dynamic data structure that follows First-In/First-Out (FIFO) principle. In a queue, addition of a new element and deletion of an element occurs at different end which implies that the element which is added first in the queue will be the first to be removed from the queue.
Features of queue
- It is a dynamic data structure.
- It has dynamic size.
- It uses dynamic memory allocation.
Operations of a queue
- isEmpty(): Checks whether the queue is empty or not.
- size(): Returns the size of the queue.
- frontElement(): Returns the front element of the queue. It is the element which will be dequeued next.
- rearElement(): Returns the rear element of the queue. It is the element behind which next element will be enqueued.
- EnQueue(x): Adds a new element ‘x’ from the rear side of the queue. Consequently, size of the queue increases by 1.
- DeQueue(): Deletes the front element of the queue. Consequently, size of the queue decreases by 1.
Implementation of queue
#include <iostream> using namespace std; #define MAX 100 class CreateQueue { int front; int rear; public: //assigning MAX size of the queue int queue[MAX]; CreateQueue() { rear = -1; front = -1; } void isEmpty(); int size(); void EnQueue(int x); void DeQueue(); int frontElement(); int rearElement(); }; // create a function to check whether // the queue is empty or not void CreateQueue::isEmpty() { if(rear == front) { cout<<"Queue is empty."<<"\n"; } else { cout<<"Queue is not empty."<<"\n"; } } //create a function to return size of the queue int CreateQueue::size() { return (rear - front); } //create a function to add new element void CreateQueue::EnQueue(int x){ if(rear == (MAX - 1)){ cout<<"Queue size limit reached."<<"\n"; } else { queue[++rear] = x; cout<<x<<" is added into the queue."<<"\n"; } } //create a function to delete front element void CreateQueue::DeQueue(){ if(rear == front){ cout<<"Queue is empty."<<"\n"; } else { int x = queue[++front]; cout<<x<<" is deleted from the queue."<<"\n"; } } //create a function to get front element int CreateQueue::frontElement() { if(rear == front) { cout<<"Queue is empty."<<"\n"; return 0; } else { return queue[front+1]; } } //create a function to get rear element int CreateQueue::rearElement() { if(rear == front) { cout<<"Queue is empty."<<"\n"; return 0; } else { return queue[rear]; } } // test the code int main() { class CreateQueue MyQueue; MyQueue.EnQueue(10); MyQueue.EnQueue(20); MyQueue.EnQueue(30); MyQueue.EnQueue(40); MyQueue.DeQueue(); MyQueue.isEmpty(); return 0; }
The above code will give the following output:
10 is added into the queue. 20 is added into the queue. 30 is added into the queue. 40 is added into the queue. 10 is deleted from the queue. Queue is not empty.
Recommended Pages
- C++ Program - To Check Prime Number
- C++ Program - Bubble Sort
- C++ Program - Selection Sort
- C++ Program - Maximum Subarray Sum
- C++ Program - Reverse digits of a given Integer
- C++ - Swap two numbers
- C++ Program - Fibonacci Sequence
- C++ Program - Insertion Sort
- C++ Program - Find Factorial of a Number
- C++ Program - Find HCF of Two Numbers
- C++ Program - To Check Whether a Number is Palindrome or Not
- C++ Program - To Check Whether a String is Palindrome or Not
- C++ Program - Heap Sort
- C++ Program - Quick Sort
- C++ - Swap Two Numbers without using Temporary Variable
- C++ Program - To Check Armstrong Number
- C++ Program - Counting Sort
- C++ Program - Radix Sort
- C++ Program - Find Largest Number among Three Numbers
- C++ Program - Print Floyd's Triangle