Sunday, November 30, 2014

Queue As An Array

For section (SuTuTh):

As we wrote in class the code of the Enqueue method for the queue as an array, we forgot to handle an important case, which is when the queue is empty.

It is not enough in this case to increment last; we have to increment also first, since both first and last are -1 when the queue is empty.

The code should look as follows:

template <class T>
void QueueArray<T>::Enqueue(T& el)
{
    if(IsFull()){
        printf("\n Can't enqueue into a full queue!");
        return;
    }

    if(IsEmpty())
        first = last = 0;
    else if(last == capacity-1)
        last = 0; 
    else
        last++;
    
    data[last] = el;
    size++;

}

No comments:

Post a Comment