-
Queue class check
Here is a queue class that I need someone to check and correct anything wrong since I've got really bored.
I'm using a vacant location to check if queue is empty or full ,but it is not working.
enum Error_code{success,overflow,underflow};
const int maxqueue= 10;
typedef char Queue_entry;
class Queue{
public:
Queue();
bool empty()const;
bool full()const;
Error_code serve();
Error_code append(const Queue_entry &item);
Error_code retrieve(Queue_entry &item)const;
protected:
int front,rear;
Queue_entry entry[maxqueue];
};
Queue::Queue()
{
front=0;
rear=maxqueue -1;
}
Error_code Queue::append (const Queue_entry &item)
{
//if there is space in the queue,item will be added at the end, otherwise an error_code of overflow is returned
if ((rear+1)%max+1==front
return overflow;
else
{rear=((rear+1)==max)?0:rear+1
entry[rear]=item;
return successs;
}
Error_code Queue::serve()
{
//if the queue is not empty, the 1st element is removed,otherwise an error_code of overflow is returned
front=((front+1)==max)?0:front+1
if (rear+1)%max==front
return overflow;
return success;
}
Error_code Queue::retrieve (Queue_entry &item)const
{
if ((rear+1)%max==front)
return underflow;
else
{item=entr[front];
return success;}
}
bool Queue::empty() const
{
if ((rear+1)%maxqueue ==front);
return true ; //empty queue
return false; //not empty
}
bool Queue::full() const
{
((rear+1)%maxqueue +1==front);
return true ; //full queue
return false; //not full
}
-
Is this a school assignment? If you actually need this, you're better off using an STL queue template.
-
yes it is
and I should not use STL but build the class from scratch and as u see I'm facing some little problems.;)
-
1 Attachment(s)
I'm very confused by your start/end usage. I'd use a back-linked list (single-linked, but used from back to end)
(attached)
-
never mind these start/close stuff were done on the forum
now I'm in the mood and it seems it is gonna work .As soon as I finish it I'll send it for anyone who needs it.
thanx