How to Reverse a Stack in C++? Last Updated : 22 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, stacks are containers that store the elements in the last in-first out order(LIFO). In, this article, we will learn how we can reverse a stack in C++. Example Input: stack<int> S ={5,4,3,2,1} store Output: // Reversed Stack stack<int> S ={1,2,3,4,5}Reverse a Stack in C++We can reverse a stack in C++ using anther set or any other sequential data container. We just have to reverse the order of the elements present in the stack. The below approach shows how to reverse stack using another stack. ApproachDeclare a temporary stack named tempStack.Till the current stack is empty. Do the following:Push the top of currentStack to tempStack.Pop the top of the currentStack.The order of the elements will be reversed in the tempStackAssign the tempStack to currentStack.C++ Program to Reverse a StackThe following program illustrates how we can reverse a stack in C++: C++ // C++ Program to illustrate how we can reverse a stack #include <iostream> #include <stack> using namespace std; // utility function to print stack void printStack(stack<int> st) { while (!st.empty()) { cout << st.top() << " "; st.pop(); } cout << endl; } int main() { // Initialize a stack stack<int> currentStack; // Push some elements into the stack currentStack.push(1); currentStack.push(2); currentStack.push(3); currentStack.push(4); currentStack.push(5); // Print the original stack cout << "Original stack: "; printStack(currentStack); // Reverse the stack stack<int> tempStack; while (!currentStack.empty()) { tempStack.push(currentStack.top()); currentStack.pop(); } currentStack = tempStack; // Print the reversed stack cout << "Reversed stack: "; printStack(currentStack); return 0; } OutputOriginal stack: 5 4 3 2 1 Reversed stack: 1 2 3 4 5 Time Complexity: O(N) where N is the size of the stack.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Reverse a Stack in C++? A akshitsaxena2 Follow Improve Article Tags : C++ Programs C++ STL cpp-stack cpp-stack-functions CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Reverse a String in C++? Reversing a string means replacing the first character with the last character, second character with the second last character and so on. In this article, we will learn how to reverse a string in C++.ExamplesInput: str = "Hello World"Output: dlroW olleHExplanation: The last character is replaced by 2 min read How to Reverse a List in C++ STL? In C++, std::list is a sequence container that allows non-contiguous memory allocation. As such, it is a doubly linked list that can be traversed in both directions. In this article, we will learn how to reverse a list in C++. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Reversed List: 50 2 min read How to Reverse a Word Using Stack in C++? In C++, we have a stack data structure that follows the Last In First Out (LIFO) principle. In this article, we will learn how to reverse a word using a stack in C++. Example: Input:word = "GeeksforGeeks"Output:Reversed Word: skeeGrofskeeGReverse a String Using Stack in C++To reverse a word using a 2 min read How to Reverse a Vector Using a Stack in C++? In C++, the stack container follows the LIFO (Last In First Out) order of operation. It means that the element first inserted will come out at last while the element that was last inserted will come out first. In this article, we will learn how we can use stack to reverse a vector in C++ Example Inp 2 min read How to Reverse a Deque in C++? In C++ STL, we have a container called deque(short for double-ended queue) that allows fast insertion and deletion operations at both the beginning and end. In this article, we will learn how to reverse a deque in C++. Example: Input: myDeque = {1, 2, 3, 4, 5}; Output: Reversed Deque: 5 4 3 2 1Rever 2 min read How to Copy a Stack in C++? In C++, stacks are a type of container adaptor with a LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. In this article, we are going to discuss how to copy a stack in C++. Copying a Stack to Another in C++To copy a s 2 min read How to Reverse a String in Place in C++? In C++, reversing a string is a basic operation in programming that is required in various applications, from simple and complex algorithms. Reversing a string in place involves changing the characters of the string directly without using input-dependent additional storage. In this article, we learn 2 min read How to Reverse a Vector using STL in C++? Reversing the vector means the swapping last element and first element, second last element and second element, and so on. In this article, we will learn how to reverse a vector using STL in C++.The most efficient method to reverse the vector is by using reverse() function. Letâs take a look at a si 3 min read How to Reverse an Array using STL in C++? Reversing an array means rearranging its elements so that the first element becomes the last, the second element becomes the second last, and so on. In this article, we will learn how to reverse an array using STL in C++.The most efficient way to reverse an array using STL is by using reverse() func 2 min read How to Clear a Stack in C++? In C++, clearing a stack means removing all element from the stack container leaving it empty. In this article, we will learn how to clear a stack in C++.The most efficient method to clear a stack is by assigning the new empty stack to our original stack container. Let's take a look at the code exam 3 min read Like