Hi. I've been thinking about a program logic, but I cannot draw a conclusion to my problem.
Here, I've implemented stack and queue operations to a fixed array.
bool StackPush( int x )
if ( top >= size ) return false;
A[top++] = x;
int StackTop( )
if ( top <= 0 ) return false;
A[--top] = 0;
bool QueuePush( int x )
if ( rear >= size ) return false;
A[rear++] = x;
int QueueFront( )
if ( front >= rear ) return false;
A[front++] = 0;
It is presumed(or obvious) that the bottom of the stack and the front of the queue is pointing at the same location, and vice versa(top of the stack points the same location as rear of the queue).
For example, integer 1 and 2 is inside an array in order of writing. And if I call StackPop(), the integer 2 will be popped out, and if I call QueuePop(), the integer 1 will be popped out.
My problem is that I don't know what happens if I do both stack and queue operations on the same array. The example above is easy to work out, because there are only two values involved. But what if there are more than 2 values involved?
For example, if I call
what values will be returned in the order of bottom(front) from the final array?
I know that if I code a program, I would receive a quick answer. But the reason I'm asking this is because I want to hear a logical explanations from a human being, not a computer.