Print Stack Elements from Bottom to Top

Given a stack s, the task is to print the elements of the stack from bottom to top, such that the elements are still present in the stack without their order being changed in the stack.

Examples:

Input : 
       
       
|   4    |
|   3    |
|   2    |
|   1    |
|________|

Output :1 2 3 4


Approach 1 (Recursion):The idea is to pop the element of the stack and call the recursive function PrintStack. Once the stack becomes empty start printing the element which was popped last and the last element that was popped was the bottom most element. Thus elements will be printed from bottom to top. Now push back the element that was printed, this will preserve the order of the elements in the stack.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print the elements of a
// stack from bottom to top
#include <bits/stdc++.h>
using namespace std;
  
// Recursive function to print stack elements
// from bottom to top without changing
// their order
void PrintStack(stack<int> s)
{
    // If stack is empty then return
    if (s.empty()) 
        return;
      
  
    int x = s.top();
  
    // Pop the top element of the stack
    s.pop();
  
    // Recursively call the function PrintStack
    PrintStack(s);
  
    // Print the stack element starting
    // from the bottom
    cout << x << " ";
  
    // Push the same element onto the stack
    // to preserve the order
    s.push(x);
}
  
// Driver code
int main()
{
    // Stack s
    stack<int> s;
  
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
  
    PrintStack(s);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print the elements of a 
// stack from bottom to top
import java.util.*;
  
class GfG 
  
// Recursive function to print stack elements 
// from bottom to top without changing 
// their order 
static void PrintStack(Stack<Integer> s) 
    // If stack is empty then return 
    if (s.isEmpty()) 
        return
      
    int x = s.peek(); 
  
    // Pop the top element of the stack 
    s.pop(); 
  
    // Recursively call the function PrintStack 
    PrintStack(s); 
  
    // Print the stack element starting 
    // from the bottom 
    System.out.print(x + " "); 
  
    // Push the same element onto the stack 
    // to preserve the order 
    s.push(x); 
  
// Driver code 
public static void main(String[] args) 
    // Stack s 
    Stack<Integer> s = new Stack<Integer> (); 
  
    s.push(1); 
    s.push(2); 
    s.push(3); 
    s.push(4); 
  
    PrintStack(s); 
}
  
// This code is contributed by Prerna Saini.

chevron_right


Output:

1 2 3 4

Approach 2 (Using another stack): The idea is to push every element into another temporary stack and then print elements of the temporary stack.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print the elements of a
// stack from bottom to top
#include <bits/stdc++.h>
using namespace std;
  
// Recursive function to print stack elements
// from bottom to top without changing
// their order
void PrintStack(stack<int> s)
{
    stack<int> temp;
    while (s.empty() == false)
    {
        temp.push(s.top());
        s.pop();
    }   
  
    while (temp.empty() == false)
    {
        int t = temp.top();
        cout << t << " ";
        temp.pop();
  
        // To restore contents of
        // the original stack.
        s.push(t);  
    }
}
  
// Driver code
int main()
{
    // Stack s
    stack<int> s;
  
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
  
    PrintStack(s);
  
    return 0;
}

chevron_right


Output:

1 2 3 4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : prerna saini