Hey.
This is the code:
Code:
#define QUEUE_CAPACITY 4
typedef struct Queue
{
size_t head; // The current head index to be dequeued in the next call to dequeue
size_t tail; // The next element index to be inserted to the queue
// head and tail range is: [0, ..., QUEUE_CAPACITY -1]
size_t numOfElems; // The current amount of elements in the queue
int data[QUEUE_CAPACITY];
} Queue;
Queue gMyQueue;
// If numOfElems == QUEUE_CAPACITY - enqueue does nothing.
void enqueue(int elem) {
if (gMyQueue.numOfElems == QUEUE_CAPACITY) {
return;
}
gMyQueue.data[gMyQueue.tail] = elem;
gMyQueue.tail = ++gMyQueue.numOfElems;
}
// If the queue is empty - dequeue does nothing and returns 0.
int dequeue(void) {
if (gMyQueue.numOfElems == 0) {
return 0;
}
gMyQueue.numOfElems--;
gMyQueue.head++;
return gMyQueue.data[gMyQueue.head - 1];
}
void printQueue()
{
size_t i = gMyQueue.head, j =0;
for( ; j < gMyQueue.numOfElems; i = (i + 1) % QUEUE_CAPACITY, ++j)
{
printf("%d, ", gMyQueue.data[i]);
}
printf("\n");
printf("head: %lu, tail: %lu\n",
gMyQueue.head, gMyQueue.tail);
}
int main()
{
enqueue(3);
enqueue(4);
enqueue(5);
enqueue(6);
enqueue(7);
printQueue();
return 0;
}
Our university's platform is running Code Runner which includes its automatic tests, and my code has failed in one of the tests which runs this main code.
In my CLion cygwin platform the program prints the expected result (which is 4), and in the school test the tail equals somehow to 0...
Does someone have an idea?