Reputation: 1170
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
Reputation: 178441
10-3 = 7
elements, since 3 of the pops
did not change the state (and size) of the stack, so only 7 pops did.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
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