0% found this document useful (0 votes)
16 views

Queue Implementation in C++ - Techie Delight

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Queue Implementation in C++ - Techie Delight

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

5/18/2021 Queue Implementation in C++ – Techie Delight

TECHIE DELIGHT Search Techie Delight...


Ace your Coding Interview

All Problems Array Tree  Linked List DP Graph Backtracking Matrix Heap

D&C String Sorting Stack Queue Binary Puzzles IDE

Queue Implementation in C++

A queue is a linear data structure that serves as a container of objects that are inserted and
removed according to the FIFO (First–In, First–Out) principle.

Queue has three main operations: enqueue , dequeue , and peek . We have already covered
these operations and C implementation of queue data structure using an array and linked list.
In this post, we will cover queue implementation in C++ using class and STL.

The following queue implementation in C++ covers the following operations:

Enqueue: Inserts a new element at the rear of the queue.


Dequeue: Removes the front element of the queue.
Peek: Returns the front element present in the queue without dequeuing it.
IsEmpty: Checks if the queue is empty.
IsFull: Checks if the queue is full.
Size: Returns the total number of elements present in the queue.

Queue Implementation using an array:

1 #include <iostream>
2 #include <cstdlib>
3 using namespace std;
4
5 // Define the default capacity of a queue
6 #define SIZE 10
7
8 // A class to store a queue
9 class queue
This website{ uses cookies. By using this site you agree to the use of cookies, our
10 Close and accept
policies,
11 copyright terms
int and other conditions.
*arr; // array Read our Privacy
to store queue Policy
elements
https://www.techiedelight.com/queue-implementation-cpp/ 1/5
5/18/2021 Queue Implementation in C++ – Techie Delight

12 int capacity; // maximum capacity of the queue


13 int front; // front points to the front element in the queue (if
14 any)
15 int rear; // rear points to the last element in the queue
16 int count; // current size of the queue
17
18 public:
19 queue(int size = SIZE); // constructor
20 ~queue(); // destructor
21
22 void dequeue();
23 void enqueue(int x);
24 int peek();
25 int size();
26 bool isEmpty();
27 bool isFull();
28 };
29
30 // Constructor to initialize a queue
31 queue::queue(int size)
32 {
33 arr = new int[size];
34 capacity = size;
35 front = 0;
36 rear = -1;
37 count = 0;
38 }
39
40 // Destructor to free memory allocated to the queue
41 queue::~queue() {
42 delete[] arr;
43 }
44
45 // Utility function to dequeue the front element
46 void queue::dequeue()
47 {
48 // check for queue underflow
49 if (isEmpty())
50 {
51 cout << "Underflow\nProgram Terminated\n";
52 exit(EXIT_FAILURE);
53 }
54
55 cout << "Removing " << arr[front] << endl;
56
57 front = (front + 1) % capacity;
58 count--;
59 }
60
61 // Utility function to add an item to the queue
62 void queue::enqueue(int item)
63 {
64 // check for queue overflow
65 if (isFull())
66 {
67 cout << "Overflow\nProgram Terminated\n";
68 exit(EXIT_FAILURE);
69 }
70
71
This website cout
uses << "Inserting
cookies. By using this"site
<<you
item << to
agree endl;
the use of cookies, our Close and accept
72 copyright terms and other conditions. Read our Privacy Policy
policies,
https://www.techiedelight.com/queue-implementation-cpp/ 2/5
5/18/2021 Queue Implementation in C++ – Techie Delight

73 rear = (rear + 1) % capacity;


74 arr[rear] = item;
75 count++;
76 }
77
78 // Utility function to return the front element of the queue
79 int queue::peek()
80 {
81 if (isEmpty())
82 {
83 cout << "Underflow\nProgram Terminated\n";
84 exit(EXIT_FAILURE);
85 }
86 return arr[front];
87 }
88
89 // Utility function to return the size of the queue
90 int queue::size() {
91 return count;
92 }
93
94 // Utility function to check if the queue is empty or not
95 bool queue::isEmpty() {
96 return (size() == 0);
97 }
98
99 // Utility function to check if the queue is full or not
100 bool queue::isFull() {
101 return (size() == capacity);
102 }
103
104 int main()
105 {
106 // create a queue of capacity 5
107 queue q(5);
108
109 q.enqueue(1);
110 q.enqueue(2);
111 q.enqueue(3);
112
113 cout << "The front element is " << q.peek() << endl;
114 q.dequeue();
115
116 q.enqueue(4);
117
118 cout << "The queue size is " << q.size() << endl;
119
120 q.dequeue();
121 q.dequeue();
122 q.dequeue();
123
124 if (q.isEmpty()) {
125 cout << "The queue is empty\n";
126 }
127 else {
128 cout << "The queue is not empty\n";
129 }
130
131 return 0;
}
This website uses cookies. By using this site you agree to the use of cookies, our Close and accept
policies, copyright terms and other conditions. Read our Privacy Policy
https://www.techiedelight.com/queue-implementation-cpp/ 3/5
5/18/2021 Queue Implementation in C++ – Techie Delight

Download Run Code

Output:

Inserting 1
Inserting 2
Inserting 3
The front element is 1
Removing 1
Inserting 4
The queue size is 3
Removing 2
Removing 3
Removing 4
The queue is empty

The time complexity of all the above queue operations is O(1).

Using std::queue :

C++’s STL provides a std::queue template class which is restricted to only enqueue/dequeue
operations. It also provides std::list which has push_back and pop_front operations
with LIFO semantics. Java’s library contains Queue interface that specifies queue operations.

std::queue

1 #include <iostream>
2 #include <queue>
3 using namespace std;
4
5 // Queue implementation in C++ using `std::queue`
6 int main()
7 {
8 queue<string> q;
9
10 q.push("A"); // Insert `A` into the queue
11 q.push("B"); // Insert `B` into the queue
12 q.push("C"); // Insert `C` into the queue
13 q.push("D"); // Insert `D` into the queue
This website
14 uses cookies. By using this site you agree to the use of cookies, our Close and accept
policies, copyright terms and other conditions. Read our Privacy Policy
https://www.techiedelight.com/queue-implementation-cpp/ 4/5
5/18/2021 Queue Implementation in C++ – Techie Delight

15 // Returns the total number of elements present in the queue


16 cout << "The queue size is " << q.size() << endl;
17
18 // Prints the front of the queue (`A`)
19 cout << "The front element is " << q.front() << endl;
20
21 // Prints the rear of the queue (`D`)
22 cout << "The rear element is " << q.back() << endl;
23
24 q.pop(); // removing the front element (`A`)
25 q.pop(); // removing the next front element (`B`)
26
27 cout << "The queue size is " << q.size() << endl;
28
29 // check if the queue is empty
30 if (q.empty()) {
31 cout << "The queue is empty\n";
32 }
33 else {
34 cout << "The queue is not empty\n";
35 }
36
37 return 0;
38 }

Download Run Code

std::list

Output:

The queue size is 4


The front element is A
The rear element is D
The queue size is 2
The queue is not empty

Also See:

Queue Implementation in Java

Queue Implementation in Python

Techie Delight © 2021 All Rights Reserved. Privacy Policy Contact us Tools
This website uses cookies. By using this site you agree to the use of cookies, our Close and accept
policies, copyright terms and other conditions. Read our Privacy Policy
https://www.techiedelight.com/queue-implementation-cpp/ 5/5

You might also like