Merge pull request #40 from qualifier1024/patch-9

Update PrintUtil.hpp
This commit is contained in:
Yudong Jin 2022-11-28 20:51:57 +08:00 committed by GitHub
commit 5343ad9f24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -210,4 +210,32 @@ class PrintUtil {
printTree(root->left, trunk, false);
}
/**
* @brief Print a stack
*
* @tparam T
* @param stk
*/
template <typename T>
static void printStack(stack<T> &stk) {
// Reverse the input stack
stack<T> 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';
}
};