Tuesday, 20 January 2015

Queue Implementation using array - OOP concepts in c++


#include
#include
using namespace std;
const int LIMIT=10;  // Queue size.
void line()
{
   cout<<"\n________________________________________________________________________________\n";  
}
// Class Queue
class Queue
{
     int contents[LIMIT];
     int head,tail;
 public:
     Queue();
     bool isFull();
     bool isEmpty();
     void Enqueue(int x);    // Insert data...
     int Dequeue();          // delete/remove data...
};

/*****************************************/
      // CLASS FUNCTIONS IMPLEMENTATION
/*****************************************/
Queue::Queue()
{
    head=0;
    tail=0;      
}
/*****************************************/
bool Queue::isFull()
{
    return (tail==LIMIT);      
}
/*****************************************/
bool Queue::isEmpty()
{
    return (head==tail);
               
}
/*****************************************/
void Queue::Enqueue(int x)
{
    if(!isFull())
    {
       contents[tail]=x;
       tail++;        
    }
    else{
       cout<<"Queue is Full.\n";  
    }          
}
/*****************************************/
int Queue::Dequeue()
{
   if(!isEmpty())
   {
       int x=contents[head];
       head++;
       return x;          
   }
   else   // queue is empty...
   {
      return -1;  
   }          
}
/*****************************************/
//    MAIN STARTS FROM HERE.
/*****************************************/

int main()
{
  Queue q1;  // declaration of object q1 of class Queue.
  char ch;
  int element,deq;
back:    // defining of Label named back.
  system("cls");
  line();
  cout<<"\n\t\t\t\4\4 QUEUE IMPLEMENTATION \4\4\n";
  line();
  cout<<"\t\t1.ENQUEUE\n\t\t2.DEQUEUE";
  line();
  ch=getch();
  switch(ch)
  {
     case '1':
             system("cls");
             line();
             cout<<"\n\t\tEnter Element to Enqueue :   "; cin>>element;
             q1.Enqueue(element);
             line();
             cout<<"\n\tPRESS ENTER TO CONTINUE..... ";
             getch();
             goto back;
         break;
     case '2':
             system("cls");
             line();
             deq = q1.Dequeue();
             if(deq != -1)
                cout<<"\t\tElement Dequeue  :  "<
//  Run the code using devC++.

0 comments:

Post a Comment