Zhejiang University Edition "Data Structure (2nd Edition)" problem set exercises -3.12

Exercise 3.12 Alternative Cyclic Queue (20point(s))

If a cyclic array is used to represent the queue, and only the head pointer of the queue is set to Front, the tail pointer is not set to Rear, but a separate Count is set to record the number of elements in the queue. Please write an algorithm to implement the enqueue and dequeue operations of the queue.

Example:

bool AddQ( Queue Q, ElementType X )
{
    if(Q->Count == Q->MaxSize) {
        printf("Queue Full\n");
        return false;
    }
    Q->Data[(Q->Front+Q->Count++)%Q->MaxSize] = X;
    return true;
}
ElementType DeleteQ( Queue Q )
{
    ElementType res;
    if(Q->Count == 0) {
        printf("Queue Empty\n");
        return ERROR;
    }
    res = Q->Data[Q->Front];
    Q->Front = (Q->Front+1) % Q->MaxSize;
    Q->Count--;
    return res;
}

Ideas:

Use count to calculate rear

 

Guess you like

Origin blog.csdn.net/u012571715/article/details/113034733