diff --git a/codes/cpp/include/PrintUtil.hpp b/codes/cpp/include/PrintUtil.hpp index b85da88ef..f326f78d1 100644 --- a/codes/cpp/include/PrintUtil.hpp +++ b/codes/cpp/include/PrintUtil.hpp @@ -210,4 +210,32 @@ class PrintUtil { printTree(root->left, trunk, false); } + + /** + * @brief Print a stack + * + * @tparam T + * @param stk + */ + template + static void printStack(stack &stk) { + // Reverse the input stack + stack tmp; + while(!stk.empty()) { + tmp.push(stk.top()); + stk.pop(); + } + // Generate the string to print + ostringstream s; + bool flag = true; + while(!tmp.empty()) { + if (flag) { + s << tmp.top(); + flag = false; + } + else s << ", " << tmp.top(); + tmp.pop(); + } + cout << "[" + s.str() + "]" << '\n'; + } };