Software-Engineering
December 26, 2023
Relational-databases
December 26, 2023
Software-Engineering
December 26, 2023
Relational-databases
December 26, 2023

Queues-and-Stacks

Question 17
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()

pospos+1

Return A[pos]

end pop

Which of the following will initialize an empty stack with capacity N for the above implementation?
A
pos ← -1
B
pos ← 0
C
pos ← 1
D
pos ←N-1
Question 17 Explanation: 
If you check push function, for every push we are decrementing pos by one.
Now if you imagine an array of length N-1, for every push the empty indices are getting decremented.
To explain in detail,
consider an empty array, so number of empty indices are N-1 ……(1)
When we push one element into it, the empty indices will reduce by 1 (i.e. decreented.)
Hence, From equation 1 we can say that pos is initialised to N-1
Correct Answer: D
Question 17 Explanation: 
If you check push function, for every push we are decrementing pos by one.
Now if you imagine an array of length N-1, for every push the empty indices are getting decremented.
To explain in detail,
consider an empty array, so number of empty indices are N-1 ……(1)
When we push one element into it, the empty indices will reduce by 1 (i.e. decreented.)
Hence, From equation 1 we can say that pos is initialised to N-1

Leave a Reply

Your email address will not be published. Required fields are marked *