Update PrintUtil.hpp

Reverse the stack before printing.
pull/40/head
Yudong Jin 2 years ago committed by GitHub
parent 26f9d6363e
commit a8afc963b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -210,34 +210,32 @@ class PrintUtil {
printTree(root->left, trunk, false); printTree(root->left, trunk, false);
} }
/** /**
* @brief Get the Stack String object * @brief Print a stack
* *
* @tparam T * @tparam T
* @param stack * @param stk
* @return string
*/ */
template <typename T> template <typename T>
static string getStackString(stack<T> stack) { static void printStack(stack<T> &stk) {
ostringstream s; // Reverse the input stack
if(!stack.empty()){ stack<T> tmp;
s << stack.top(); while(!stk.empty()) {
stack.pop(); tmp.push(stk.top());
stk.pop();
} }
while(!stack.empty()){ // Generate the string to print
s << ", " << stack.top(); ostringstream s;
stack.pop(); bool flag = true;
while(!tmp.empty()) {
if (flag) {
s << tmp.top();
flag = false;
}
else s << ", " << tmp.top();
tmp.pop();
} }
return "top->" + s.str() + "]"; cout << "[" + s.str() + "]" << '\n';
}
/**
* @brief Print a stack
*
* @tparam T
* @param stack
*/
template <typename T>
static void printStack(stack<T> &stack) {
cout << getStackString(stack) << '\n';
} }
}; };

Loading…
Cancel
Save