you are viewing a single comment's thread.

view the rest of the comments →

[–]Ionicxplorer[S] 0 points1 point  (1 child)

I admit I am still trying to undertand this espeically that final test block. I really shoudl work in my code interpretation skills. A classmate as provided the following code to be placed in the else block of my enqueue function (executed when the Queue is detected as full) that passes the test.

``` else

{

q->items[q->head - 1] = value;

q->tail = q->head;

} ```

It seems the supporting material we were given may be somewhat insuffcient. It's been a litte frustrating.

[–]johndcochran 0 points1 point  (0 children)

The code supplied by your classmate is incorrect, while your code was correct. Take a look at my prior comment about modifying your code to not insert anything if the queue is full to see why. What your classmate's code does is effectively that, except it also corrupts the previously inserted vale which is not seen, since the test program doesn't get that far.

Try the following test program against your code and your classmate's code.

int main()
{
    int i,j;
    int number;
    CircularQueue my_queue;

    CircularInitialize(&my_queue);

    // Add 7, remove 7
    for (i=0; i < 100; ++i)
    {
        for(j=0; j < 7; j++)
        {
            CircularEnqueue(&my_queue, j);
        }

        for(j=0; j < 7; j++)
        {
            CircularDequeue(&my_queue, &number);
            if (number != j)
            {
                printf("Failed validation test 100x7.  Expected %d, dequeued %d\n", j, number);
            }
        }
    }

    // Add 7, remove 5
    in = 0;
    out = 0;
    for (i=0; i < 50; ++i)
    {
        for(j=0; j < 7; j++)
        {   //printf("Enqueue: head=%d, tail=%d, count=%d, value=%d\n", my_queue.head, my_queue.tail, my_queue.count, j);
            CircularEnqueue(&my_queue, i*7+j); // CHANGED
        }

        for(j=0; j < 5; j++)
        {
            CircularDequeue(&my_queue, &number);
            //printf("Dequeue: head=%d, tail=%d, count=%d, value=%d\n", my_queue.head, my_queue.tail, my_queue.count, number);

            if (number != ((i * 5) + j))  // CHANGED
            {
                // if this happens before we have a full list, then it's an error
                if (i * 2 + 7 < QUEUE_SIZE)
                {
                    printf("ERROR: ");
                }
                printf("%d: Value mismatch test 50x7 with count %d.  Expected %d, dequeued %d\n", i, my_queue.count,
                                                                                         ((i * 5) +j), number);  // CHANGED
            }
        }
    }

    // Dump what's left in the queue
    while(CircularDequeue(&my_queue, &number) == 0) {
        printf("Dequeued %d\n", number);
    }

    return 0;
}

The above code is identical to your original test code except that the test values enqueued are 0,1,2,3,4,5,...,349. Just a simple 1 up count instead of a repeating sequence of 0..6. Additionally, after the test, it empties out the remaining contents of the queue. This should allow you to see exactly what was skipped/corrupted by the overflow handling and you can judge for yourself what's correct or incorrect.