Circular queue using Arrays
Write a C++ Menu Driven Program to implement circular queue using Arrays. Here’s simple C++ Menu Driven Program to implement circular queue using Arrays in C++ Programming Language.
What is Queue ?
Queue is also an abstract data type or a linear data structure, in which the first element is inserted from one end called REAR, and the deletion of existing element takes place from the other end called as FRONT.
This makes queue as FIFO data structure, which means that element inserted first will also be removed first.
Basic Operations : :
- enqueue() − add (store) an item to the queue.
- dequeue() − remove (access) an item from the queue.
- peek() − Gets the element at the front of the queue without removing it.
- isfull() − Checks if the queue is full.
- isempty() − Checks if the queue is empty.
Below is the source code for C++ Menu Driven Program to implement circular queue using Arrays which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C++ Menu Driven Program to implement circular queue using Arrays */ #include<iostream> #include<stdlib.h> using namespace std; class cqueue { int q[5],front,rare; public: cqueue() { front=-1; rare=-1; } void push(int x) { if(front ==-1 && rare == -1) { q[++rare]=x; front=rare; return; } else if(front == (rare+1)%5 ) { cout <<" Circular Queue over flow"; return; } rare= (rare+1)%5; q[rare]=x; } void pop() { if(front==-1 && rare== -1) { cout <<"under flow"; return; } else if( front== rare ) { front=rare=-1; return; } front= (front+1)%5; } void display() { int i; if( front <= rare) { for(i=front; i<=rare;i++) cout << q[i]<<" "; } else { for(i=front;i<=4;i++) { cout <<q[i] << " "; } for(i=0;i<=rare;i++) { cout << q[i]<< " "; } } } }; int main() { int ch; cqueue q1; while( 1) { cout<<"\n1.INSERT 2.DELETE 3.DISPLAY 4.EXIT\nEnter ur choice"; cin >> ch; switch(ch) { case 1: cout<<"enter element"; cin >> ch; q1.push(ch); break; case 2: q1.pop(); break; case 3: q1.display(); break; case 4: exit(0); } } return 0; }
OUTPUT : :
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice1 enter element1 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice1 enter element2 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice1 enter element3 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice1 enter element4 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice3 1 2 3 4 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice2 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice3 2 3 4 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice4 Exit code: 0
Above is the source code and output for C++ Program to implement queue using standard template library which is successfully compiled and run on Windows System to produce desired output.
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.
Thanks for reading the post….