das kinder
das kinder

Reputation: 1170

What is the current size of Stack?

Suppose an initially empty stack S has performed a total of 25 push operations, 12 top operations, and 10 pop operations, 3 of which returned null to indicate an empty stack. What is the current size of S?

I'm thinking that S.size =7 because 10 pop operations have 3 out of 10 returned null to indicate an empty stack but not really sure if it's correct or not

Can any one give the correct answer and explanation ?

Upvotes: 0

Views: 8089

Answers (3)

archit agarwal
archit agarwal

Reputation: 99

It is the difference between number of push and pop.

Upvotes: 0

amit
amit

Reputation: 178441

  • You popped a total number of 10-3 = 7 elements, since 3 of the pops did not change the state (and size) of the stack, so only 7 pops did.
  • You pushed a total of 25 elements.
  • Top operations do not change the state (and size) of the stack, and can be ignored.

Total size of the stack is 25-7 = 18 at the end.

Note that the order of operations does not matter, only the amount of "succesfull" pop()s, and amount of push()s.

Upvotes: 5

Soumitri Pattnaik
Soumitri Pattnaik

Reputation: 3556

25 pushes = 25 index

10 pops = 25 - 10 = 15

3 pops did not occur = 15 + 3 = 18

tops does not matter, so it should be 18

Upvotes: 0

Related Questions