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++
#include <bits/stdc++.h>
using namespace std;
void PrintStack(stack< int > s)
{
if (s.empty())
return ;
int x = s.top();
s.pop();
PrintStack(s);
cout << x << " " ;
s.push(x);
}
int main()
{
stack< int > s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
PrintStack(s);
return 0;
}
|
Java
import java.util.*;
class GfG
{
static void PrintStack(Stack<Integer> s)
{
if (s.isEmpty())
return ;
int x = s.peek();
s.pop();
PrintStack(s);
System.out.print(x + " " );
s.push(x);
}
public static void main(String[] args)
{
Stack<Integer> s = new Stack<Integer> ();
s.push( 1 );
s.push( 2 );
s.push( 3 );
s.push( 4 );
PrintStack(s);
}
}
|
Python3
import sys
class Stack:
def __init__( self ):
self .s = []
def push( self , data):
self .s.append(data)
def pop( self ):
return self .s.pop()
def peek( self ):
return self .s[ - 1 ]
def count( self ):
return len ( self .s)
def printStack(s):
if s.count() = = 0 :
return
x = s.peek()
s.pop()
printStack(s)
print ( "{} " . format (x), end = "")
s.push(x)
if __name__ = = '__main__' :
s = Stack()
s.push( 1 )
s.push( 2 )
s.push( 3 )
s.push( 4 )
printStack(s)
|
C#
using System;
using System.Collections.Generic;
class GfG
{
static void PrintStack(Stack< int > s)
{
if (s.Count == 0)
return ;
int x = s.Peek();
s.Pop();
PrintStack(s);
Console.Write(x + " " );
s.Push(x);
}
public static void Main()
{
Stack< int > s = new Stack< int > ();
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
PrintStack(s);
}
}
|
Javascript
<script>
function PrintStack(s)
{
if (s.length == 0)
return ;
var x = s[s.length - 1];
s.pop();
PrintStack(s);
document.write(x + " " );
s.push(x);
}
var s = [];
s.push(1);
s.push(2);
s.push(3);
s.push(4);
PrintStack(s);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Approach 2 (Using another stack): The idea is to push every element into another temporary stack and then print elements of the temporary stack.
C++
#include <bits/stdc++.h>
using namespace std;
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();
s.push(t);
}
}
int main()
{
stack< int > s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
PrintStack(s);
return 0;
}
|
Java
import java.util.*;
class Main{
public static void PrintStack(Stack<Integer> s)
{
Stack<Integer> temp = new Stack<Integer>();
while (s.empty() == false )
{
temp.push(s.peek());
s.pop();
}
while (temp.empty() == false )
{
int t = temp.peek();
System.out.print(t + " " );
temp.pop();
s.push(t);
}
}
public static void main(String[] args)
{
Stack<Integer> s = new Stack<Integer>();
s.push( 1 );
s.push( 2 );
s.push( 3 );
s.push( 4 );
PrintStack(s);
}
}
|
Python3
import sys
class Stack:
def __init__( self ):
self .s = []
def push( self , data):
self .s.append(data)
def pop( self ):
return self .s.pop()
def peek( self ):
return self .s[ - 1 ]
def count( self ):
return len ( self .s)
def printStack(s):
temp = Stack()
while (s.count() > 0 ):
temp.push(s.peek())
s.pop()
while (temp.count() > 0 ):
t = temp.peek()
print ( "{} " . format (temp.peek()), end = "")
temp.pop()
s.push(t)
if __name__ = = '__main__' :
s = Stack()
s.push( 1 )
s.push( 2 )
s.push( 3 )
s.push( 4 )
printStack(s)
|
C#
using System;
using System.Collections;
class GFG{
static void PrintStack(Stack s)
{
Stack temp = new Stack();
while (s.Count != 0)
{
temp.Push(s.Peek());
s.Pop();
}
while (temp.Count != 0)
{
int t = ( int )temp.Peek();
Console.Write(t + " " );
temp.Pop();
s.Push(t);
}
}
public static void Main( string [] args)
{
Stack s = new Stack();
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
PrintStack(s);
}
}
|
Javascript
<script>
function PrintStack(s)
{
var temp = [];
while (s.length!=0)
{
temp.push(s[s.length-1]);
s.pop();
}
while (temp.length!=0)
{
var t = temp[temp.length-1];
document.write( t + " " );
temp.pop();
s.push(t);
}
}
var s = [];
s.push(1);
s.push(2);
s.push(3);
s.push(4);
PrintStack(s);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
06 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...