The following piece of code incorrectly attempts to remove all even values from a stack of integers.

Question:

The following piece of code incorrectly attempts to remove all even values from a stack of integers. What is wrong with the code, and how would you fix it?

while (!s.isEmpty ()) { int n = s.pop (); if (n % 2 != 0) { s.push (n); // put back in stack if odd

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

Step by Step Answer:

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