Question: Consider the following stack: + - - - - + | 1 6 | < - top + - - - - + | 2

Consider the following stack:
+----+
|16|<- top
+----+
|20|
+----+
|12|
+----+
|19|<- bottom
+----+
Of the functions below, which ones return a stack whose contents match the stack depicted above?
There might be more than one correct answer. Be sure to select all that apply.
functionA()
Stack functionA()
{
Stack s;
s.push(16);
s.push(20);
s.push(12);
s.push(19);
return s;
}
functionB()
// Note: This function is identical to functionA(), except the
// initial four values are pushed in a different order.
Stack functionB()
{
Stack s;
s.push(19);
s.push(12);
s.push(20);
s.push(16);
return s;
}
functionC()
Stack functionC()
{
Stack s;
s.push(16);
s.push(20);
s.push(12);
s.push(19);
s.peek();
s.peek();
s.peek();
s.peek();
return s;
}
functionD()
// Note: This function is identical to functionC(), except the
// initial four values are pushed in a different order.
Stack functionD()
{
Stack s;
s.push(19);
s.push(12);
s.push(20);
s.push(16);
s.peek();
s.peek();
s.peek();
s.peek();
return s;
}
functionE()
Stack functionE()
{
Stack s;
s.push(16);
s.push(20);
s.push(12);
s.push(19);
s.pop();
s.pop();
s.pop();
s.pop();
return s;
}
functionF()
// Note: This function is identical to functionE(), except the
// initial four values are pushed in a different order.
Stack functionF()
{
Stack s;
s.push(19);
s.push(12);
s.push(20);
s.push(16);
s.pop();
s.pop();
s.pop();
s.pop();
return s;
}
functionG()
Stack functionG()
{
Stack s;
s.push(16);
s.push(20);
s.push(12);
s.push(19);
// The return value from s.peek() is pushed onto s.
s.push(s.peek());
return s;
}
functionH()
// Note: This function is identical to functionG(), except the
// initial four values are pushed in a different order.
Stack functionH()
{
Stack s;
s.push(19);
s.push(12);
s.push(20);
s.push(16);
// The return value from s.peek() is pushed onto s.
s.push(s.peek());
return s;
}
functionI()
Stack functionI()
{
Stack s;
s.push(16);
s.push(20);
s.push(12);
s.push(19);
// The return value from s.pop() is pushed onto s.
s.push(s.pop());
return s;
}
functionJ()
// Note: This function is identical to functionI(), except the
// initial four values are pushed in a different order.
Stack functionJ()
{
Stack s;
s.push(19);
s.push(12);
s.push(20);
s.push(16);
// The return value from s.pop() is pushed onto s.
s.push(s.pop());
return s;
}
functionK()
Stack functionK()
{
Stack s1;
Stack s2;
s1.push(16);
s1.push(20);
s1.push(12);
s1.push(19);
while (!s1.isEmpty())
{
// Pop value from s1 and push it onto s2.
s2.push(s1.pop());
}
return s2;
}
functionL()
// Note: This function is identical to functionK(), except the
// initial four values are pushed in a different order.
Stack functionL()
{
Stack s1;
Stack s2;
s1.push(19);
s1.push(12);
s1.push(20);
s1.push(16);
while (!s1.isEmpty())
{
// Pop value from s1 and push it onto s2.
s2.push(s1.pop());
}
return s2;
}

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 Databases Questions!