In this program, we are going to share how to implement a circular queue using c++ programming language. If you are a beginner and want to start learning the c++ programming, then keep your close attention in this tutorial as I am going to share a c++ program to implement circular queue.
Copy the below c++ program and execute it with the help of turbo c++ compiler. At the end of this program, We have shared the output of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
#include <iostream> #define MAX 5 using namespace std; /* * Class Circular Queue */ class Circular_Queue { private: int *cqueue_arr; int front, rear; public: Circular_Queue() { cqueue_arr = new int [MAX]; rear = front = -1; } /* * Insert into Circular Queue */ void insert(int item) { if ((front == 0 && rear == MAX-1) || (front == rear+1)) { cout<<"Queue Overflow \n"; return; } if (front == -1) { front = 0; rear = 0; } else { if (rear == MAX - 1) rear = 0; else rear = rear + 1; } cqueue_arr[rear] = item ; } /* * Delete from Circular Queue */ void del() { if (front == -1) { cout<<"Queue Underflow\n"; return ; } cout<<"Element deleted from queue is : "<<cqueue_arr[front]<<endl; if (front == rear) { front = -1; rear = -1; } else { if (front == MAX - 1) front = 0; else front = front + 1; } } /* * Display Circular Queue */ void display() { int front_pos = front, rear_pos = rear; if (front == -1) { cout<<"Queue is empty\n"; return; } cout<<"Queue elements :\n"; if (front_pos <= rear_pos) { while (front_pos <= rear_pos) { cout<<cqueue_arr[front_pos]<<" "; front_pos++; } } else { while (front_pos <= MAX - 1) { cout<<cqueue_arr[front_pos]<<" "; front_pos++; } front_pos = 0; while (front_pos <= rear_pos) { cout<<cqueue_arr[front_pos]<<" "; front_pos++; } } cout<<endl; } }; /* * Main */ int main() { int choice, item; Circular_Queue cq; do { cout<<"1.Insert\n"; cout<<"2.Delete\n"; cout<<"3.Display\n"; cout<<"4.Quit\n"; cout<<"Enter your choice : "; cin>>choice; switch(choice) { case 1: cout<<"Input the element for insertion in queue : "; cin>>item; cq.insert(item); break; case 2: cq.del(); break; case 3: cq.display(); break; case 4: break; default: cout<<"Wrong choice\n"; }/*End of switch*/ } while(choice != 4); return 0; } |
Liked this program? Do Like & share with your friends 🙂
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: c++ programs for substraction, c++ programs to circular queue, c++ programs with example, overloaded function in c++ programs