Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Using pseudocode or C++ code, and the following ADT stack operations, write a

ID: 3664835 • Letter: 1

Question

1. Using pseudocode or C++ code, and the following ADT stack operations, write a procedure

void AppendStack( StackType &S, StackType &T )

to append, in order, the elements of a stack T to the top of a stack S. For example, if S contains the elements “Justin”, “Bieber”, “sux”, in that particular order, from bottom to top, and T contains the elements “big”, “time”, in that particular order, from bottom to top, then, after the execution of AppendStack( S, T ), S contains the elements, “Justin”, “Bieber”, “sux”, “big”, “time”, in that particular order, from bottom to top. Note that you may not access the internal data members of the stack ADT.

bool IsEmpty()const

bool IsFull() const

void Push( ItemType item )

void Pop()

ItemType Top() const

Explanation / Answer

public static void revertStack(Stack s) { if (s.isEmpty()) { return; } else { Integer a = s.pop(); revertStack(s); appendStack(s, a); } } public static void appendStack(Stack s, Integer a) { if (s.isEmpty()) { s.push(a); return; } else { Integer o = s.pop(); appendStack(s, a); s.push(o); } }