# Reverse an array using Stack

• Last Updated : 18 Jan, 2021

Given an array arr[] of size N, the task to reverse the array using Stack.

Examples:

Input: arr[] = { 10, 20, 30, 40, 50 }
Output: 50 40 30 20 10
Explanation:
Reversing the array modifies arr[] to { 50, 40, 30, 20, 10 }
Therefore, the required output is 50 40 30 20 10.

Input: arr[] = { 1 }
Output: 1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Iterative and Recursive Approach: Refer the article reverse an array to solve this problem iteratively or recursively.
Time Complexity: O(N)
Auxiliary Space: O(1)

Stack-based Approach: Follow the steps below to solve the problem:

• Initialize a Stack to store the array elements.
• Traverse the array and push all the array elements into the stack.
• >Pop the elements from the stack and insert into the array.
• Finally, print the array.
• Below is the implementation of the above approach:

## C++

 `// C++ program to implement``// the above approach``#include ``using` `namespace` `std;`` ` `// Structure of stack``class` `Stack {``public``:``    ``// Stores index of top``    ``// element of a stack``    ``int` `top;`` ` `    ``// Stores maximum count of``    ``// elements stored in a stack``    ``unsigned capacity;`` ` `    ``// Stores address of``    ``// array element``    ``int``* array;``};`` ` `// Function to Initialize a stack``// of given capacity.``Stack* createStack(unsigned capacity)``{``    ``Stack* stack = ``new` `Stack();``    ``stack->capacity = capacity;``    ``stack->top = -1;``    ``stack->array = ``new` `int``[(stack->capacity``                            ``* ``sizeof``(``int``))];``    ``return` `stack;``}`` ` `// Function to check if``// the stack is full or not``int` `isFull(Stack* stack)``{``    ``return` `stack->top``           ``== stack->capacity - 1;``}`` ` `// Function to check if``// the stack is empty or not``int` `isEmpty(Stack* stack)``{``    ``return` `stack->top == -1;``}`` ` `// Function to insert an element``// into the stack.``void` `push(Stack* stack, ``int` `item)``{`` ` `    ``// If stack is full``    ``if` `(isFull(stack))``        ``return``;`` ` `    ``// Insert element into stack``    ``stack->array[++stack->top] = item;``}`` ` `// Function to remove an element``// from stack.``int` `pop(Stack* stack)``{`` ` `    ``// If stack is empty``    ``if` `(isEmpty(stack))``        ``return` `-1;`` ` `    ``// Pop element from stack``    ``return` `stack->array[stack->top--];``}`` ` `// Function to reverse the array elements``void` `reverseArray(``int` `arr[], ``int` `n)``{`` ` `    ``// Initialize a stack of capacity n``    ``Stack* stack = createStack(n);`` ` `    ``for` `(``int` `i = 0; i < n; i++) {`` ` `        ``// Insert arr[i] into the stack``        ``push(stack, arr[i]);``    ``}`` ` `    ``// Reverse the array elements``    ``for` `(``int` `i = 0; i < n; i++) {`` ` `        ``// Update arr[i]``        ``arr[i] = pop(stack);``    ``}`` ` `    ``// Print array elements``    ``for` `(``int` `i = 0; i < n; i++)``        ``cout << arr[i] << ``" "``;``}`` ` `// Driver Code``int` `main()``{`` ` `    ``int` `arr[] = { 100, 200, 300, 400 };`` ` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``reverseArray(arr, N);``    ``return` `0;``}`

## C

 `// C program to implement``// the above approach``#include ``#include ``#include ``#include `` ` `// Structure of stack``struct` `Stack {`` ` `    ``// Stores index of top``    ``// element of a stack``    ``int` `top;`` ` `    ``// Stores maximum count of``    ``// elements stored in a stack``    ``unsigned capacity;`` ` `    ``// Stores address of``    ``// array element``    ``int``* array;``};`` ` `// Function to Initialize a stack``// of given capacity.``struct` `Stack* createStack(unsigned capacity)``{``    ``struct` `Stack* stack``        ``= (``struct` `Stack*)``malloc``(``            ``sizeof``(``struct` `Stack));``    ``stack->capacity = capacity;``    ``stack->top = -1;``    ``stack->array``        ``= (``int``*)``malloc``(``            ``stack->capacity``            ``* ``sizeof``(``int``));``    ``return` `stack;``}`` ` `// Function to check if``// the stack is full or not``int` `isFull(``struct` `Stack* stack)``{``    ``return` `stack->top``           ``== stack->capacity - 1;``}`` ` `// Function to check if``// the stack is empty or not``int` `isEmpty(``struct` `Stack* stack)``{``    ``return` `stack->top == -1;``}`` ` `// Function to insert an element``// into the stack.``void` `push(``struct` `Stack* stack, ``int` `item)``{`` ` `    ``// If stack is full``    ``if` `(isFull(stack))``        ``return``;`` ` `    ``// Insert element into stack``    ``stack->array[++stack->top] = item;``}`` ` `// Function to remove an element``// from stack.``int` `pop(``struct` `Stack* stack)``{`` ` `    ``// If stack is empty``    ``if` `(isEmpty(stack))``        ``return` `-1;`` ` `    ``// Pop element from stack``    ``return` `stack->array[stack->top--];``}`` ` `// Function to reverse the array elements``void` `reverseArray(``int` `arr[], ``int` `n)``{`` ` `    ``// Initialize a stack of capacity n``    ``struct` `Stack* stack = createStack(n);`` ` `    ``for` `(``int` `i = 0; i < n; i++) {`` ` `        ``// Insert arr[i] into the stack``        ``push(stack, arr[i]);``    ``}`` ` `    ``// Reverse the array elements``    ``for` `(``int` `i = 0; i < n; i++) {`` ` `        ``// Update arr[i]``        ``arr[i] = pop(stack);``    ``}`` ` `    ``// Print array elements``    ``for` `(``int` `i = 0; i < n; i++)``        ``printf``(``"%d "``, arr[i]);``}`` ` `// Driver Code``int` `main()``{`` ` `    ``int` `arr[] = { 100, 200, 300, 400 };`` ` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``reverseArray(arr, N);``    ``return` `0;``}`

## Java

 `// Java program to implement``// the above approach``import` `java.util.*;`` ` `// Structure of stack``class` `Stack {`` ` `    ``// Stores maximum count of``    ``// elements stored in a stack``    ``int` `size;`` ` `    ``// Stores index of top``    ``// element of a stack``    ``int` `top;`` ` `    ``// Stores address of``    ``// array element``    ``int``[] a;`` ` `    ``// Function to check if``    ``// the stack is empty or not``    ``boolean` `isEmpty()``    ``{``        ``return` `(top < ``0``);``    ``}`` ` `    ``// Function to Initialize``    ``// a stack of given capacity.``    ``Stack(``int` `n)``    ``{``        ``top = -``1``;``        ``size = n;``        ``a = ``new` `int``[size];``    ``}`` ` `    ``// Function to push``    ``// an element into Stack``    ``boolean` `push(``int` `x)``    ``{`` ` `        ``// If Stack is full``        ``if` `(top >= size) {``            ``System.out.println(``                ``"Stack Overflow"``);``            ``return` `false``;``        ``}``        ``else` `{`` ` `            ``// Insert element``            ``// into stack``            ``a[++top] = x;``            ``return` `true``;``        ``}``    ``}`` ` `    ``// Function to remove an element``    ``// from stack.``    ``int` `pop()``    ``{`` ` `        ``// If stack is empty``        ``if` `(top < ``0``) {``            ``System.out.println(``                ``"Stack Underflow"``);``            ``return` `0``;``        ``}`` ` `        ``// Pop element from stack``        ``else` `{``            ``int` `x = a[top--];``            ``return` `x;``        ``}``    ``}``}`` ` `// Driver Code``class` `Main {`` ` `    ``// Function to reverse the array elements``    ``public` `static` `void` `reverse(``int` `arr[], ``int` `n)``    ``{`` ` `        ``// Initialize a stack of capacity n``        ``Stack obj = ``new` `Stack(n);`` ` `        ``for` `(``int` `i = ``0``; i < n; i++) {`` ` `            ``// Insert arr[i] into the stack``            ``obj.push(arr[i]);``        ``}`` ` `        ``// Reverse the array elements``        ``for` `(``int` `i = ``0``; i < n; i++) {`` ` `            ``// Update arr[i]``            ``arr[i] = obj.pop();``        ``}`` ` `        ``// Print array elements``        ``for` `(``int` `i = ``0``; i < n; i++) {``            ``System.out.print(arr[i] + ``" "``);``        ``}``    ``}`` ` `    ``// Driver function``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `n = ``4``;`` ` `        ``// Create a new array``        ``int``[] a = ``new` `int``[] { ``100``, ``200``, ``300``, ``400` `};`` ` `        ``// Call reverse method``        ``reverse(a, n);``    ``}``}`

Output:

```400 300 200 100
```

Time Complexity: O(N)
Auxiliary Space: O(N).

My Personal Notes arrow_drop_up