1. 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?
2. Suppose an initally empty queue Q has performed a total of 32 enqueue operations, 10 first operations, and 15 dequeue operations, 5 of which returned null to indicate an empty queue. What is the current size of Q?
1
Expert's answer
2018-04-24T09:56:17-0400
1) For stack: - Push – adds item to collection and increases size of stack - Top – only returns last item from collection without removing it - Pop – returns last item from collection and decrease size of stack If 3 pop operations returned null then only 7 pop operations decrease size of stack. Current size of stack is 25 (push) - 7 = 18; 2) For queue: - enqueue – adds item to collection and increases size of queue - first – only first last item from collection without removing it - dequeue – returns first item from collection and decrease size of stack
The same is for queue : Current size of queue is -> 32 – 10 = 22.
Comments
Dear Naomi. Please find updated answer
Isn't 25-7 = 18 though? Why is the size of the Stack 16?
Leave a comment