Question: Add a method size() to both Stack (Program 4.3.4) and Queue (Program 4.3.6) that returns the number of items in the collection. Hint: Make sure
Add a method size() to both Stack (Program 4.3.4) and Queue (Program 4.3.6) that returns the number of items in the collection. Hint: Make sure that your method takes constant time by maintaining an instance variable \(n\) that you initialize to 0 , increment in push () and enqueue (), decrement in \(\operatorname{pop}()\) and dequeue (), and return in size().
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
