Print stack elements from Bottom to Top

Here given two ways to print stack elements from bottom to top (reverse ) reverse order. First is using of recursion. See this example.

/*
   Java Program
   Print stack elements from Bottom to Top 
*/
import java.util.Stack;
public class Display
{
    // Display stack elements in reverse order
    public void bottomToTop(Stack < Integer > s)
    {
        if (s.isEmpty() == true)
        {
            return;
        }
        // Get top element
        int element = s.peek();
        // Remove top
        s.pop();
        // Recursively calling process
        bottomToTop(s);
        // Display element
        System.out.print(" " + element);
        //  Push back to stack
        s.push(element);
    }
    public static void main(String[] arg)
    {
        Display task = new Display();
        Stack < Integer > s = new Stack < Integer > ();
        // Add element
        s.push(1);
        s.push(3);
        s.push(6);
        s.push(2);
        s.push(8);
        s.push(9);
        /*
         Constructed Stack
         ------------------
            9  <- top
            8
            2
            6
            3
            1
         ------------------
        */
        task.bottomToTop(s);
    }
}

In java programming language stack class internally use vector and vector are work with array to store data elements. Look at this example. Which is show internal representation of above program.

Stack data representation in java

Output

 1 3 6 2 8 9
// Include header file
#include <iostream>
#include <stack>

using namespace std;
/*
   C++ Program
   Print stack elements from Bottom to Top 
*/
class Display
{
    public:
        // Display stack elements in reverse order
        void bottomToTop(stack < int > s)
        {
            if (s.empty() == true)
            {
                return;
            }
            // Get top element
            int element = s.top();
            // Remove top
            s.pop();
            // Recursively calling process
            this->bottomToTop(s);
            // Display element
            cout << " " << element;
            //  Push back to stack
            s.push(element);
        }
};
int main()
{
    Display task = Display();
    stack < int > s ;
    // Add element
    s.push(1);
    s.push(3);
    s.push(6);
    s.push(2);
    s.push(8);
    s.push(9);
    /*
     Constructed Stack
     ------------------
        9  <- top
        8
        2
        6
        3
        1
     ------------------
    */
    task.bottomToTop(s);
    return 0;
}

Output

 1 3 6 2 8 9
// Include namespace system
using System;
using System.Collections.Generic;
/*
   C# Program
   Print stack elements from Bottom to Top 
*/
public class Display
{
    // Display stack elements in reverse order
    public void bottomToTop(Stack < int> s)
    {
        if (s.Count == 0 )
        {
            return;
        }
        // Get top element
        int element = s.Peek();
        // Remove top
        s.Pop();
        // Recursively calling process
        bottomToTop(s);
        // Display element
        Console.Write(" " + element);
        //  Push back to stack
        s.Push(element);
    }
    public static void Main(String[] arg)
    {
        Display task = new Display();
        Stack < int > s = new Stack < int > ();
        // Add element
        s.Push(1);
        s.Push(3);
        s.Push(6);
        s.Push(2);
        s.Push(8);
        s.Push(9);
        /*
         Constructed Stack
         ------------------
            9  <- top
            8
            2
            6
            3
            1
         ------------------
        */
        task.bottomToTop(s);
    }
}

Output

 1 3 6 2 8 9
from queue import LifoQueue
#    Python 3 Program
#    Print stack elements from Bottom to Top 

class Display :
    #  Display stack elements in reverse order
    def bottomToTop(self, s) :
        if (s.empty() == True) :
            return
        
        #  Get and remove top element
        element = s.get()
        
        #  Recursively calling process
        self.bottomToTop(s)
        #  Display element
        print(" ", element, end = "")
        #   Push back to stack
        s.put(element)
    

def main() :
    task = Display() 
    s = LifoQueue()
    #  Add element
    s.put(1)
    s.put(3)
    s.put(6)
    s.put(2)
    s.put(8)
    s.put(9)
    # 
    #  Constructed Stack
    #  ------------------
    #     9  <- top
    #     8
    #     2
    #     6
    #     3
    #     1
    #  ------------------
    
    task.bottomToTop(s)

if __name__ == "__main__": main()

Output

  1  3  6  2  8  9
import scala.collection.mutable._;
/*
   Scala Program
   Print stack elements from Bottom to Top 
*/
class Display
{
    // Display stack elements in reverse order
    def bottomToTop( s: Stack[Int]): Unit = {
        if (s.isEmpty == true)
        {
            return;
        }
        // Get top element
        var element: Int = s.top;
        // Remove top
        s.pop;
        // Recursively calling process
        this.bottomToTop(s);
        // Display element
        print(" " + element);
        //  Push back to stack
        s.push(element);
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: Display = new Display(); 
        var s = Stack[Int]();
        // Add element
        s.push(1);
        s.push(3);
        s.push(6);
        s.push(2);
        s.push(8);
        s.push(9);
        /*
         Constructed Stack
         ------------------
            9  <- top
            8
            2
            6
            3
            1
         ------------------
        */
        task.bottomToTop(s);
    }
}

Output

 1 3 6 2 8 9
import Foundation
/*
   Swift 4 Program
   Print stack elements from Bottom to Top 
*/
// implement stack
struct Stack
{
    private
    var items: [Int] = []
    func peek()->Int
    {
        if (self.isEmpty()==false)
        {
            return items.first!
        }
        else
        {
            fatalError("This stack is empty.")
        }
    }
    func isEmpty()->Bool
    {
        return items.count == 0
    }
    mutating func pop()->Int
    {
        return items.removeFirst()
    }
    mutating func push(_ data: Int)
    {
        items.insert(data, at: 0)
    }
}
class Display
{
    // Display stack elements in reverse order
    func bottomToTop(_ s: inout Stack)
    {
        if (s.isEmpty() == true)
        {
            return;
        }
        // Get and remove top element
        let element: Int = s.pop();
      
        // Recursively calling process
        self.bottomToTop(&s);
        // Display element
        print(" ", element, terminator: "");
        //  Push back to stack
        s.push(element);
    }
}
func main()
{
    let task: Display = Display(); 
    var s = Stack();
    // Add element
    s.push(1);
    s.push(3);
    s.push(6);
    s.push(2);
    s.push(8);
    s.push(9);
    /*
     Constructed Stack
     ------------------
        9  <- top
        8
        2
        6
        3
        1
     ------------------
    */
    task.bottomToTop(&s);
}
main();

Output

  1  3  6  2  8  9
#  Ruby Program
#  Print stack elements from Bottom to Top 

class Display 
    #  Display stack elements in reverse order
    def bottomToTop( s) 
        if (s.length == 0 ) 
            return
        end

        #  Get top element
        element = s.last
        s.pop()
        #  Recursively calling process
        self.bottomToTop(s)
        #  Display element
        print(" ", element)
        s.push(element)
    end

end

def main() 
    task = Display.new() 
    # Implement stack using array
    s = []
    s.push(1)
    s.push(3)
    s.push(6)
    s.push(2)
    s.push(8)
    s.push(9)
    # 
    #  Constructed Stack
    #  ------------------
    #     9  <- top
    #     8
    #     2
    #     6
    #     3
    #     1
    #  ------------------
    
    task.bottomToTop(s)
end

main()

Output

 1 3 6 2 8 9
import java.util.Stack;
/*
   Kotlin Program
   Print stack elements from Bottom to Top 
*/
class Display
{
    // Display stack elements in reverse order
    fun bottomToTop(s: Stack<Int> ): Unit
    {
        if (s.isEmpty() == true)
        {
            return;
        }
        // Get top element
        var element: Int = s.peek();
        // Remove top
        s.pop();
        // Recursively calling process
        this.bottomToTop(s);
        // Display element
        print(" " + element);
        //  Push back to stack
        s.push(element);
    }
}
fun main(args: Array < String > ): Unit
{
    var task: Display = Display(); 
    var s = Stack<Int>();
    // Add element
    s.push(1);
    s.push(3);
    s.push(6);
    s.push(2);
    s.push(8);
    s.push(9);
    /*
     Constructed Stack
     ------------------
        9  <- top
        8
        2
        6
        3
        1
     ------------------
    */
    task.bottomToTop(s);
}

Output

1 3 6 2 8 9

Time complexity of above solution is O(n). Second solution are by using of auxiliary stack.

/*
   Java Program
   Print stack elements from Bottom to Top 
   iterative solution
*/
import java.util.Stack;
public class Display
{
    // Display stack elements in reverse order
    public void bottomToTop(Stack < Integer > s)
    {
        // Define auxiliary stack
        Stack < Integer > auxiliary = new Stack < Integer > ();
        int temp = 0;
        // Get the element into auxiliary stack
        while (!s.isEmpty())
        {
            temp = s.peek();
            auxiliary.push(temp);
            s.pop();
        }
        // Put and  display auxiliary stack elements into actual stack
        while (!auxiliary.isEmpty())
        {
            temp = auxiliary.peek();
            // Display the auxiliary stack element
            System.out.print(" " + temp);
            // Add element into actual stack
            s.push(temp);
            // Remove auxiliary stack top element
            auxiliary.pop();
        }
    }
    public static void main(String[] arg)
    {
        Display task = new Display();
        Stack < Integer > s = new Stack < Integer > ();
        // Add element
        s.push(1);
        s.push(3);
        s.push(6);
        s.push(2);
        s.push(8);
        s.push(9);
        /*
         Constructed Stack
         ------------------
            9  <- top
            8
            2
            6
            3
            1
         ------------------
        */
        task.bottomToTop(s);
    }
}

Output

 1 3 6 2 8 9
// Include header file
#include <iostream>
#include <stack>

using namespace std;
/*
   C++ Program
   Print stack elements from Bottom to Top 
   iterative solution
*/
class Display
{
    public:
        // Display stack elements in reverse order
        void bottomToTop(stack < int > s)
        {
            // Define auxiliary stack
            stack < int > auxiliary ;
            int temp = 0;
            // Get the element into auxiliary stack
            while (!s.empty())
            {
                temp = s.top();
                auxiliary.push(temp);
                s.pop();
            }
            // Put and  display auxiliary stack elements into actual stack
            while (!auxiliary.empty())
            {
                temp = auxiliary.top();
                // Display the auxiliary stack element
                cout << " " << temp;
                // Add element into actual stack
                s.push(temp);
                // Remove auxiliary stack top element
                auxiliary.pop();
            }
        }
};
int main()
{
    Display task = Display();
    stack < int > s ;
    // Add element
    s.push(1);
    s.push(3);
    s.push(6);
    s.push(2);
    s.push(8);
    s.push(9);
    /*
     Constructed Stack
     ------------------
        9  <- top
        8
        2
        6
        3
        1
     ------------------
    */
    task.bottomToTop(s);
    return 0;
}

Output

 1 3 6 2 8 9
// Include namespace system
using System;
using System.Collections.Generic;
/*
   C# Program
   Print stack elements from Bottom to Top 
   iterative solution
*/
public class Display
{
    // Display stack elements in reverse order
    public void bottomToTop(Stack < int > s)
    {
        // Define auxiliary stack
        Stack < int > auxiliary = new Stack < int > ();
        int temp = 0;
        // Get the element into auxiliary stack
        while (s.Count != 0)
        {
            temp = s.Peek();
            auxiliary.Push(temp);
            s.Pop();
        }
        // Put and  display auxiliary stack elements into actual stack
        while (auxiliary.Count != 0)
        {
            temp = auxiliary.Peek();
            // Display the auxiliary stack element
            Console.Write(" " + temp);
            // Add element into actual stack
            s.Push(temp);
            // Remove auxiliary stack top element
            auxiliary.Pop();
        }
    }
    public static void Main(String[] arg)
    {
        Display task = new Display();
        Stack < int > s = new Stack < int > ();
        // Add element
        s.Push(1);
        s.Push(3);
        s.Push(6);
        s.Push(2);
        s.Push(8);
        s.Push(9);
        /*
         Constructed Stack
         ------------------
            9  <- top
            8
            2
            6
            3
            1
         ------------------
        */
        task.bottomToTop(s);
    }
}

Output

 1 3 6 2 8 9
from queue import LifoQueue

#    Python 3 Program
#    Print stack elements from Bottom to Top 
#    iterative solution

class Display :
    #  Display stack elements in reverse order
    def bottomToTop(self, s) :
        #  Define auxiliary stack
        auxiliary = LifoQueue()
        temp = 0
        #  Get the element into auxiliary stack
        while (not s.empty()) :
            temp = s.get()
            auxiliary.put(temp)
            
        
        #  Put and  display auxiliary stack elements into actual stack
        while (not auxiliary.empty()) :
            temp = auxiliary.get()
            #  Display the auxiliary stack element
            print(" ", temp, end = "")
            #  Add element into actual stack
            s.put(temp)
            
        
    

def main() :
    task = Display() 
    s = LifoQueue()
    #  Add element
    s.put(1)
    s.put(3)
    s.put(6)
    s.put(2)
    s.put(8)
    s.put(9)
    # 
    #  Constructed Stack
    #  ------------------
    #     9  <- top
    #     8
    #     2
    #     6
    #     3
    #     1
    #  ------------------
    
    task.bottomToTop(s)

if __name__ == "__main__": main()

Output

  1  3  6  2  8  9
#    Ruby Program
#    Print stack elements from Bottom to Top 
#    iterative solution

class Display 
    #  Display stack elements in reverse order
    def bottomToTop(s) 
        #  Define auxiliary array
        auxiliary = []
        temp = 0
        #  Get the element into auxiliary stack
        while (s.length != 0) 
            temp = s.last
            auxiliary.push(temp)
            s.pop()
        end

        #  Put and  display auxiliary stack elements into actual stack
        while (auxiliary.length != 0) 
            temp = auxiliary.last
            #  Display the auxiliary stack element
            print(" ", temp)
            s.push(temp)
            auxiliary.pop()
        end

    end

end

def main() 
    task = Display.new() 
    s = []
    s.push(1)
    s.push(3)
    s.push(6)
    s.push(2)
    s.push(8)
    s.push(9)
    # 
    #  Constructed Stack
    #  ------------------
    #     9  <- top
    #     8
    #     2
    #     6
    #     3
    #     1
    #  ------------------
    
    task.bottomToTop(s)
end

main()

Output

 1 3 6 2 8 9
import scala.collection.mutable._;
/*
   Scala Program
   Print stack elements from Bottom to Top 
   iterative solution
*/
class Display
{
    // Display stack elements in reverse order
    def bottomToTop( s:  Stack[Int] ): Unit = {
        // Define auxiliary stack
        var auxiliary = Stack[Int]();
        var temp: Int = 0;
        // Get the element into auxiliary stack
        while (!s.isEmpty)
        {
            temp = s.top;
            auxiliary.push(temp);
            s.pop;
        }
        // Put and  display auxiliary stack elements into actual stack
        while (!auxiliary.isEmpty)
        {
            temp = auxiliary.top;
            // Display the auxiliary stack element
            print(" " + temp);
            // Add element into actual stack
            s.push(temp);
            // Remove auxiliary stack top element
            auxiliary.pop;
        }
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: Display = new Display(); 
        var s = Stack[Int]();
        // Add element
        s.push(1);
        s.push(3);
        s.push(6);
        s.push(2);
        s.push(8);
        s.push(9);
        /*
         Constructed Stack
         ------------------
            9  <- top
            8
            2
            6
            3
            1
         ------------------
        */
        task.bottomToTop(s);
    }
}

Output

 1 3 6 2 8 9
import Foundation
/*
   Swift 4 Program
   Print stack elements from Bottom to Top 
   iterative solution
*/
// implement stack
struct Stack
{
    private
    var items: [Int] = []
    func peek()->Int
    {
        if (self.isEmpty()==false)
        {
            return items.first!
        }
        else
        {
            fatalError("This stack is empty.")
        }
    }
    func isEmpty()->Bool
    {
        return items.count == 0
    }
    mutating func pop()->Int
    {
        return items.removeFirst()
    }
    mutating func push(_ data: Int)
    {
        items.insert(data, at: 0)
    }
}
class Display
{
    // Display stack elements in reverse order
    func bottomToTop(_ s: inout Stack)
    {
        // Define auxiliary stack
    
        var auxiliary: Stack = Stack();
        var temp: Int = 0;
        // Get the element into auxiliary stack
        while (!s.isEmpty())
        {
            temp = s.pop();
            auxiliary.push(temp);
            
        }
        // Put and  display auxiliary stack elements into actual stack
        while (!auxiliary.isEmpty())
        {
            temp = auxiliary.pop();
            // Display the auxiliary stack element
            print(" ", temp, terminator: "");
            // Add element into actual stack
            s.push(temp);
            
        }
    }
}
func main()
{
    let task: Display = Display(); 
    var s: Stack = Stack();
    // Add element
    s.push(1);
    s.push(3);
    s.push(6);
    s.push(2);
    s.push(8);
    s.push(9);
    /*
     Constructed Stack
     ------------------
        9  <- top
        8
        2
        6
        3
        1
     ------------------
    */
    task.bottomToTop(&s);
}
main();

Output

  1  3  6  2  8  9
import java.util.Stack;
/*
   Kotlin Program
   Print stack elements from Bottom to Top 
   iterative solution
*/
class Display
{
    // Display stack elements in reverse order
    fun bottomToTop(  s: Stack<Int>  ): Unit
    {
        // Define auxiliary stack

        var auxiliary = Stack<Int>();
        var temp: Int;
        // Get the element into auxiliary stack
        while (!s.isEmpty())
        {
            temp = s.peek();
            auxiliary.push(temp);
            s.pop();
        }
        // Put and  display auxiliary stack elements into actual stack
        while (!auxiliary.isEmpty())
        {
            temp = auxiliary.peek();
            // Display the auxiliary stack element
            print(" " + temp);
            // Add element into actual stack
            s.push(temp);
            // Remove auxiliary stack top element
            auxiliary.pop();
        }
    }
}
fun main(args: Array <String> ): Unit
{
    var task: Display = Display(); 
    var s = Stack<Int>();
    // Add element
    s.push(1);
    s.push(3);
    s.push(6);
    s.push(2);
    s.push(8);
    s.push(9);
    /*
     Constructed Stack
     ------------------
        9  <- top
        8
        2
        6
        3
        1
     ------------------
    */
    task.bottomToTop(s);
}

Output

 1 3 6 2 8 9


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved