Consider the following stack implemented using stack – #define SIZE 11 Struct…
2022
Consider the following stack implemented using stack – #define SIZE 11 Struct STACK { int arr[SIZE]; int top = -1; } What would be the maximum value of top that does not cause the overflow of the stack?
- A.
8
- B.
9
- C.
11
- D.
10
Attempted by 843 students.
Show answer & explanation
Correct answer: D
To determine the maximum value of top that does not cause overflow, consider the following:
1. The stack is implemented using an array of size 11, so valid indices are 0 to 10.
2. The top variable starts at -1, indicating an empty stack.
3. Each time an element is pushed, top is incremented by 1.
4. The maximum value of top occurs when the stack is full, which is when the topmost element is at index 10.
5. Therefore, the maximum value of top that does not cause overflow is 10.