Reverse an array using Stack

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 10Explanation: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
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.
Finally, print the array.
Below is the implementation of the above approach:
C++
C
Java
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;  // Structure of stackclass 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 notint isFull(Stack* stack){    return stack->top           == stack->capacity - 1;}  // Function to check if// the stack is empty or notint 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 elementsvoid 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 Codeint main(){      int arr[] = { 100, 200, 300, 400 };      int N = sizeof(arr) / sizeof(arr[0]);    reverseArray(arr, N);    return 0;}
// C program to implement// the above approach#include <limits.h>#include <stdio.h>#include <stdlib.h>#include <string.h>  // Structure of stackstruct 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 notint isFull(struct Stack* stack){    return stack->top           == stack->capacity - 1;}  // Function to check if// the stack is empty or notint 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 elementsvoid 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 Codeint main(){      int arr[] = { 100, 200, 300, 400 };      int N = sizeof(arr) / sizeof(arr[0]);    reverseArray(arr, N);    return 0;}
// Java program to implement// the above approachimport java.util.*;  // Structure of stackclass 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 Codeclass 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);    }}
400 300 200 100

Time Complexity: O(N)Auxiliary Space: O(N).
Reverse
Technical Scripter 2020
Arrays
Stack
Technical Scripter
Arrays
Stack
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Window Sliding Technique
Search, insert and delete in an unsorted array
What is Data Structure: Types, Classifications and Applications
Chocolate Distribution Problem
Stack in Python
Stack Class in Java
Check for Balanced Brackets in an expression (well-formedness) using Stack
Introduction to Data Structures
Queue using Stacks