Queue Code Example C++ Programming




#include<iostream>
using namespace std;

int front=-1, rear=-1;
int queue[5];
bool isRunning=true;
int i;

int get_cmd(){
   int n;
   cout<<"===========Menu==========="<<endl;
   cout<<"1. Add"<<endl;
   cout<<"2. Remove"<<endl;
   cout<<"3. Show"<<endl;
   cout<<"4. Exit"<<endl;
   cout<<"Select option: ";
   cin>>n;
   return n;
}

void rearFunc(){
   if(rear==4){
          cout<<"Queue is full."<<endl;
   }else{
          rear++;
          cout<<"Input a value: ";
          cin>>queue[rear];
   }
}

void frontFunc(){
   if(front==rear){
          cout<<"Queue is empty."<<endl;
   }else{
          front++;
   }
   cout<<endl;
}

void showFunc(){
   if(rear==front){
          cout<<"Queue is empty."<<endl;
   }else{
          cout<<"Queue data: ";
          for(i=front+1;i<=rear;i++){
                 cout<<queue[i]<<"\t";
          }
   }
   cout<<endl;
}

int main(){
   do{
          switch(get_cmd()){
          case 1:
                 rearFunc();
                 break;
          case 2:
                 frontFunc();
                 break;
          case 3:
                 showFunc();
                 break;
          case 4:
                 isRunning=false;
                 break;
          default:
                 cout<<"Invalid option."<<endl;
          }
   }while(isRunning);
   return 0;
}