Write a stand-alone method that receives an IntStack object as its parameter and
ID: 3831549 • Letter: W
Question
Write a stand-alone method that receives an IntStack object as its parameter and returns an IntStack with the same contents in the same order. Example: If the original stack had {1, 2, 3} with 3 at top, then the returned stack will also have {1, 2, 3} with 3 at the top. Be careful to use the IntStack class methods correctly- The original stack passed as argument must be restored to its original state before this method returns, Though it may get modified during the process. Declare any variables necessary. Note that the class description is different from what was used in test 2. public static IntStack copy (IntStack original) {IntStack result = new IntStack();Explanation / Answer
public static IntStack copy(IntStack original) {
IntStack result = new IntStack();
IntStack temp = new IntStack();
while(!original.empty()){
temp.push(original.pop());
}
while(!temp.empty())
{
Integer i = temp.pop();
result.push(i);
original.push(i);
}
return result;
}