Add a method size() to both Stack (Program 4.3.4) and Queue (Program 4.3.6) that returns the number

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 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().

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: