Update PrintUtil.hpp
Reverse the stack before printing.
This commit is contained in:
		| @@ -210,34 +210,32 @@ class PrintUtil { | |||||||
|          |          | ||||||
|             printTree(root->left, trunk, false); |             printTree(root->left, trunk, false); | ||||||
|         } |         } | ||||||
|         /** |      | ||||||
|          * @brief Get the Stack String object |  | ||||||
|          *  |  | ||||||
|          * @tparam T  |  | ||||||
|          * @param stack  |  | ||||||
|          * @return string  |  | ||||||
|          */ |  | ||||||
|         template <typename T> |  | ||||||
|         static string getStackString(stack<T> stack) { |  | ||||||
|             ostringstream s; |  | ||||||
|             if(!stack.empty()){ |  | ||||||
|                 s << stack.top(); |  | ||||||
|                 stack.pop(); |  | ||||||
|             } |  | ||||||
|             while(!stack.empty()){ |  | ||||||
|                 s << ", " << stack.top(); |  | ||||||
|                 stack.pop(); |  | ||||||
|             } |  | ||||||
|             return "top->" + s.str() + "]"; |  | ||||||
|         } |  | ||||||
|         /** |         /** | ||||||
|          * @brief Print a stack |          * @brief Print a stack | ||||||
|          *  |          *  | ||||||
|          * @tparam T  |          * @tparam T  | ||||||
|          * @param stack |          * @param stk | ||||||
|          */ |          */ | ||||||
|         template <typename T> |         template <typename T> | ||||||
|         static void printStack(stack<T> &stack) { |         static void printStack(stack<T> &stk) { | ||||||
|             cout << getStackString(stack) << '\n'; |             // 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'; | ||||||
|         } |         } | ||||||
| }; | }; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user