Reverse an array using Stack
Given an array arr[] of size n, the task is to reverse the array using Stack.
Examples:
Input: arr = [10, 20, 30, 40, 50]
Output: 50 40 30 20 10
Explanation: Upon reversing the array becomes [50, 40, 30, 20, 10]. Therefore, the output is 50 40 30 20 10.Input: arr = [ 1 ]
Output: 1
Explanation: Reversing the array does not changes the array as it only has a single element
Also read: Array Reverse – Complete Tutorial
The idea is to push all elements of the array into the stack. Since a stack follows the Last In, First Out (LIFO) principle, popping elements from the stack and storing them back into the array results in a reversed order. This efficiently reverses the array without using extra loops for swapping.
Follow the steps given below to reverse an array using stack.
- Create an empty stack.
- One by one push all elements of the array to stack.
- One by one pop all elements from the stack and push them back to the array.
#include <bits/stdc++.h>
using namespace std;
void reverseArray(vector<int> &arr)
{
int n = arr.size();
//declare a stack
stack<int> st;
// push all elements of array into stack
for (int i = 0; i < n; i++)
{
st.push(arr[i]);
}
// update the elements of the array
while (!st.empty())
{
int top = st.top();
st.pop();
arr[i] = top;
i++;
}
return;
}
int main()
{
vector<int> arr = {100, 200, 300, 400};
reverseArray(arr);
for (auto it : arr)
cout << it << " ";
return 0;
}
using namespace std;
void reverseArray(vector<int> &arr)
{
int n = arr.size();
//declare a stack
stack<int> st;
// push all elements of array into stack
for (int i = 0; i < n; i++)
{
st.push(arr[i]);
}
// update the elements of the array
while (!st.empty())
{
int top = st.top();
st.pop();
arr[i] = top;
i++;
}
return;
}
int main()
{
vector<int> arr = {100, 200, 300, 400};
reverseArray(arr);
for (auto it : arr)
cout << it << " ";
return 0;
}
import java.util.*;
class GfG {
static void reverseArray(int arr[])
{
int n = arr.length;
//declare a stack
Stack<Integer> st = new Stack<>();
//push array elements into stack
for (int i = 0; i < n; i++) {
st.push(arr[i]);
}
// update elements of array
int i = 0;
while (st.empty() == false) {
int top = st.peek();
st.pop();
arr[i] = top
i++;
}
return;
}
public static void main(String args[])
{
int[] a = new int[] { 100, 200, 300, 400 };
reverseArray(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
def reverseArray(arr):
n = len(arr)
st = []
# push all elements of array into stack
for i in range(n):
st.append(arr[i])
# update the elements of array
i = 0
while(len(st) > 0):
top = st.pop()
arr[i] = top
i += 1
n = 4
arr = [100, 200, 300, 400]
reverseArray(arr)
for i in range(n):
print(arr[i], end=" ")
using System;
using System.Collections.Generic;
class GfG {
public static void reverseArray(int[] arr)
{
int n = arr.Length;
Stack<int> st = new Stack<int>();
// pushing array elements into stack
for (int i = 0; i < n; i++) {
st.Push(arr[i]);
}
// Reverse the array elements
for (int i = 0; i < n; i++) {
int top = st.Peek();
st.Pop();
arr[i] = top;
}
return;
}
public static void Main()
{
int n = 4;
int[] a = new int[] { 100, 200, 300, 400 };
reverseArray(a);
for (int i = 0; i < n; i++) {
Console.Write(a[i] + " ");
}
}
}
function reverseArray(arr)
{
let n = arr.length;
// Declare a stack
const st = [];
// push all elements of array into stack
for (let i = 0; i < n; i++) {
st.push(arr[i]);
}
let i = 0;
//updating elements of array
while (st.length) {
// get top element of stack and pop it
const top = st.pop();
// update ith index of array and move to next index
arr[i] = top;
i++;
}
return;
}
const n = 4;
const arr = [ 100, 200, 300, 400 ];
reverseArray(arr);
console.log(arr.join(" "))
Output
400 300 200 100