/* Write a C++ program to implement circular queue ADT using an array */
#include<iostream> #include<conio.h> #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]<< " "; } } } }; 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); } } } |
OUTPUT
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice1
enter element4
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice1
enter element5
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice1
enter element3
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice3
4 5 3
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice2
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice3
5 3
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice4
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Resources #Terms of Use
Copyright©2012 electrofriends.com All Rights Reserved
Contact:[email protected]
The code is wrong,the insert and delete function working properly for the first time but in the case :
when queue is full,you cant insert more cause even the first element is occupied now when u delete an element the first element does get deleted but when u insert now the value is inserted in the end itself,basically its like u have shifted the entire queue contents by one (decreased the index) and the insertion takes place at the end itself