A stack is implemented with an array of ‘A[0...N – 1]’ and a variable ‘pos’.…

ISRO Scientist/Engineer SC · 2020 · Computer Science

A stack is implemented with an array of ‘A[0...N – 1]’ and a variable ‘pos’. The push and pop operations are defined by the following code.

push (x)
  A[pos] ← x
  pos ← pos – 1
end push

pop ( )
  pos ← pos + 1
  return A[pos]
end pop 

Which of the following will initialize an empty stack with capacity N for the above implementation ?

  1. A.

    pos ← –1

  2. B.

    pos ← 0

  3. C.

    pos ← 1

  4. D.

    pos ← N – 1

Attempted by 929 students.

Show answer

Correct answer: D

The worked solution is available to enrolled students.

Video solution available to enrolled students.

Explore the full course: Iocl Engineers Officers Grade A Paper 2

Loading lesson…