C++ Program for Implementation of Queue

// Implementation of Queue

#include 
using namespace std;

static int front, rear, size;
static int q[100];

void pop();
void push(int n);
void showQueue();

void push(int n) {
   if (rear==(size-1)){
     cout << "Queue full." << endl;
   } else if ((front==-1) &&
     (rear == -1)){
       front = rear = 0;
       q[front] = n;
   } else {
     q[++rear] = n;
   }
   showQueue();
}

   void pop() {
if (rear==-1 && front==-1)
     cout << "Queue empty." << endl;
   else if
     (front==rear && front!=-1)
       front = rear = -1;
   else
       for (int i=0; i<size-1; i++)
       q[i] = q[i+1];
     rear--;
     showQueue();
}

void showQueue() {
   cout << endl;
     if (front==-1 || rear==-1)
       cout << endl << "Queue is empty." << endl;
     else {
       cout << endl << "Queue: " << endl;
       int i;
       for (i=front; i<=rear; i++) {
         cout << q[i] << "\t";
       }

     }
   cout << endl;
}


int main() {

   cout << "** Demo for Queue **\n" << endl;

   front = rear = -1;
   size = 100;

   int exit = 0;
   while (exit == 0)
   {
     int choice;

     cout << endl << "Choose and Operation: " << endl;

     cout << "1. Insert " << endl;

     cout << "2. Delete " << endl;

     cout << "3. Exit" << endl;

     cout <> choice;

     switch(choice)
     {
       case 1:
       {
         cout <> n;
         push(n);
         break;
       }
       case 2:
       {
         pop();
         break;
       }
       case 3:
       {
         exit = 1;
         break;
       }
       default: break;
     }
   }
}


Advertisements