E7
question :
rewrite the methods for queue processing from the text , using a flag to indicate a full queue instead of keeping a count of the entries in the queue.
answer :
#include<iostream>
#include<queue>
using namespace std;
#include"Queue.cpp"
typedef int Queue_entry;
enum Error_code(success,underflow,overflow);
int main()
{
const int maxq=10;
class Queue
{
public:
Queue();
bool empty()const;
Error_code serve();
Error_code append(const Queue_entry &item);
Error_code retrieve(Queue_entry &item)const;
protected:
int front;
int rear;
int flag;
Queue_entry[maxqueue];
}
Queue::Queue()
{
flag=1;
rear=maxqueue-1;
front=0;
}
bool Queue::empty()const
{
return flag=1;
}
Error_code Queue::append(const Queue_entry &item)
{
if(flag==1)
{
rear=(rear+1)%maxqueue;
entry[rear]=item;
return success;
}
else return overflow;
}
Error_code Queue::serve()
{
if(flag!=1)
{
front=(front+1)%maxqueue;
return success;
}
else return underflow;
}
Error_code Queue::retrieve(Queue_entry&item)const
{
if(flag!=1)
{
item=entry[front];
return success;
}
else return underflow;
}