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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Algorithm Design Questions!