TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
circular_queue_using_array.cpp
1#include <iostream>
2using std::cin;
3using std::cout;
4
5int queue[10];
6int front = 0;
7int rear = 0;
8int count = 0;
9
10void Enque(int x) {
11 if (count == 10) {
12 cout << "\nOverflow";
13 } else {
14 queue[rear] = x;
15 rear = (rear + 1) % 10;
16 count++;
17 }
18}
19
20void Deque() {
21 if (front == rear) {
22 cout << "\nUnderflow";
23 }
24
25 else {
26 cout << "\n" << queue[front] << " deleted";
27 front = (front + 1) % 10;
28 count--;
29 }
30}
31
32void show() {
33 for (int i = 0; i < count; i++) {
34 cout << queue[(i + front) % 10] << "\t";
35 }
36}
37
38int main() {
39 int ch, x;
40 do {
41 cout << "\n1. Enque";
42 cout << "\n2. Deque";
43 cout << "\n3. Print";
44 cout << "\nEnter Your Choice : ";
45 cin >> ch;
46 if (ch == 1) {
47 cout << "\nInsert : ";
48 cin >> x;
49 Enque(x);
50 } else if (ch == 2) {
51 Deque();
52 } else if (ch == 3) {
53 show();
54 }
55 } while (ch != 0);
56
57 return 0;
58}
Definition queue.hpp:9
int main()
Main function.