Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<conio.h>
- #include<cstdio>
- #include<cstdlib>
- using namespace std;
- struct CIRCULAR_QUEUE
- {
- int data[5], front_f, rear;
- };
- CIRCULAR_QUEUE q;
- void init();
- void Enqueue(int item);
- void Dequeue();
- void Print();
- void Size();
- int main()
- {
- system("cls");
- int choice, input, i;
- cout << "Circular queue operation"<<endl;
- init();
- do{
- cout<<"1. Enqueue"<<endl;
- cout<<"2. Dequeue"<<endl;
- cout<<"3. Print"<<endl;
- cout<<"4. Clear"<<endl;
- cout<<"5. Exit"<<endl;
- cout<<endl;
- cout<<"Menu : ";
- cin>>choice;
- if (choice == 5)
- {
- break;
- }
- else
- {
- switch(choice)
- {
- case 1:
- {
- Enqueue(input);
- cout<<endl;
- break;
- }
- case 2:
- {
- Dequeue();
- cout<<endl;
- break;
- }
- case 3:
- {
- Print();
- cout << endl;
- break;
- }
- case 4:
- {
- init();
- cout<<"Queue is empty ! "<<endl;
- cout<<endl;
- break;
- }
- default:
- {
- cout<<"Wrong menu ! "<<endl;
- }
- }
- }
- } while(choice != 5);
- return 0;
- }
- void init()
- {
- q.front_f = q.rear = -1;
- }
- void Enqueue(int item)
- {
- if(q.front_f == ((q.rear + 1)%5))
- {
- cout<<"Queue is full !"<<endl;
- }
- else
- {
- cout<<"Enter the data : ";
- cin>>item;
- if(q.front_f == -1)
- {
- q.front_f = q.rear = 0;
- }
- else
- {
- q.rear = (q.rear+1)%5;
- }
- q.data[q.rear] = item;
- cout << "Position : " << q.rear << ", enqueued value : " << item << endl;
- }
- }
- void Dequeue()
- {
- if(q.front_f == -1 && q.rear == -1)
- {
- cout<<"Queue is empty !"<<endl;
- }
- else
- {
- cout<<"Dequeued data : "<<q.data[q.front_f]<< " position : "<<q.front_f<<endl;
- if(q.front_f == q.rear)
- {
- q.front_f = q.rear = -1;
- }
- else
- {
- q.front_f = (q.front_f + 1)%5;
- }
- }
- }
- void Print()
- {
- int i;
- if(q.front_f == -1 && q.rear == -1)
- {
- cout<<"Queue is empty !"<<endl;
- }
- else if(q.front_f <= q.rear )
- {
- cout << "Queue : ";
- for( i=q.front_f; i<= q.rear ; i++)
- {
- cout<<q.data[i]<<"("<<i<<") ";
- }
- cout << endl << "Queue size : " ;
- Size();
- cout << endl;
- }
- else
- {
- cout << "Queue : ";
- for( i=q.front_f; i< 5 ; i++)
- {
- cout<<q.data[i]<<"("<<i<<") ";
- }
- for( i=0; i<=q.rear ; i++)
- {
- cout<<q.data[i]<<"("<<i<<") ";
- }
- cout << endl << "Queue size : " ;
- Size();
- cout << endl;
- }
- }
- void Size()
- {
- if(q.rear >= q.front_f)
- {
- cout<<(q.rear - q.front_f) + 1;
- }
- else
- {
- cout<< (5 - (q.front_f - q.rear) + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement