Sunday, March 31, 2013

Array Implementation Of Queue

The queue works on the principle of first in first out...FIFO.. , where insertion takes place at the "rear" end of the queues and deletions take place at the "front" end of the queues.
Queue is much and same as a line of people waiting for their turn to vote.First person will be the first to vote and a new person can join the queue , at the rear end of it....



Program:

#include<iostream>
#define MAX 100
using namespace std;

class Queue{
               int arr[MAX];
               int rear,front;
               public:
               Queue()
               {
                   front=rear=-1;
               }
               void insert(int num)
               {
                   if(rear==MAX-1)
                   cout<<"Queue full!!!!!!!";
                   if(rear==-1&&front==-1)
                   {
                       front=0;
                       arr[++rear]=num;
                   }
                   else
                   arr[++rear]=num;
               }
               int remove()
               {
                   int val;
                   if(front==-1)
                   val=-1;
                   else
                   val=arr[front++];
                   if(rear==front)
                   rear=-1;
                   return val;
               }
               void display()
               {
                   for(int i=front;i<=rear;++i)
                   cout<<arr[i]<<"   ";
               }
}a;

int main()
{
    system("cls");
    system("color ec");
    cout<<"\n\n\n\t\t\t";
    a.insert(10);
    a.insert(20);
    a.insert(30);
    a.insert(40);
    a.insert(50);

    cout<<a.remove()<<"  ";
    cout<<a.remove()<<"  ";
    cout<<a.remove()<<"  ";
    cout<<a.remove()<<"  ";
    cout<<a.remove()<<"  ";
    cout<<"\n\n\n\n\n\n";
    return 0;
}


Output:


 

0 comments:

Post a Comment