Saturday, February 7, 2015

Queue implementation and operation in C++

#include<iostream>
#include<cstdlib>
using namespace std;
class queue
{ public:
    int size;
    int elmnt[50];
    int front;
    int rear;
    int isfull();
int isempty();
    void enqueue(int e);
    int dequeue();
    queue()
    {size=5;
     front=-1;
     rear=-1;
    }
};
int queue::isfull()
{if((rear+1)%size==front)
  {cout<<"Full";
   return 1;
  }
  else
   return 0;
}
int queue::isempty()
{if(front==-1)
  {cout<<"Empty";
   return 1;
  }
  else
  return 0;
}
void queue::enqueue(int e)
{ if(isfull())
   exit(1);
  else
   { rear=(rear+1)%size;
     elmnt[rear]=e;
     if(front==-1)
     front++;
   }
}
int queue::dequeue()
{ int c;
  if(isempty())
   exit(1);
  else
   {c=elmnt[front];
    if(front==rear)
    {front=-1;
     rear=-1;
    }
    else
     front=(front+1)%size;
    return(c);
   }
}
int main()
{queue s;int d,i;
  s.enqueue(4);
  s.enqueue(5);
  for(i=0;i<3;i++)
  {d=s.dequeue();
   cout<<d<<endl;
  }
}

No comments:

Post a Comment

Contributors

Translate