The operation to retrieve the top entry of a stack without removing it is called a(n)

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

Examples:

Input: S = {2, 3, 4, 5}
Output: 5 4 3 2

Input: S = {3, 3, 2, 2}
Output: 2 2 3 3

Recursive Approach: Follow the steps below to solve the problem:

  1. Create a recursive function having stack as the parameter.
  2. Add the base condition that, if the stack is empty, return from the function.
  3. Otherwise, store the top element in some variable X and remove it.
  4. Print X, call the recursive function and pass the same stack in it.
  5. Push the stored X back to the Stack.

Below is the implementation of the above approach:

void PrintStack(stack<int> s)

public static void PrintStack(Stack<Integer> s) 

    System.out.print(x + " ");

public static void main(String[] args) 

    Stack<Integer> s = new Stack<Integer>(); 

from queue import LifoQueue

if __name__ == '__main__':

using System.Collections.Generic;

public static void PrintStack(Stack<int> s) 

public static void Main(String[] args) 

  Stack<int> s = new Stack<int>(); 

    document.write( x + ' ');

Time Complexity: O(N), where N is the number of elements in the given stack.
Auxiliary Space: O(N)

Singly LinkedList Stack Approach: This approach discusses the solution to solve the problem for Singly LinkedList Stack representation. Below are the steps:

  1. Push the top element from the given stack into a linked list stack.
  2. Print the top element of the singly linked list stack.
  3. Pop the top element from the given primary stack.
  4. Repeat the above steps in order until the given stack is empty.

Below is the implementation of the above approach:

        cout << "\nHeap Overflow"; 

        cout << ("Stack is empty");

        cout << "\nStack Underflow" << endl; 

        cout << "\nStack Underflow"; 

            cout << temp->data << " "; 

import static java.lang.System.exit;

class StackUsingLinkedlist {

            System.out.print("\nHeap Overflow");

            System.out.println("Stack is empty");

            System.out.print("\nStack Underflow");

    DisplayStack(StackUsingLinkedlist s)

            = new StackUsingLinkedlist();

            System.out.print(s1.peek()

    public static void main(String[] args)

            = new StackUsingLinkedlist();

class StackUsingLinkedlist{

public StackUsingLinkedlist()

        Console.Write("\nHeap Overflow");

        Console.WriteLine("Stack is empty");

        Console.Write("\nStack Underflow");

public void DisplayStack(StackUsingLinkedlist s)

    StackUsingLinkedlist s1 = new StackUsingLinkedlist();

    while (s.isEmpty() != true) 

        Console.Write(s1.peek() + " ");

public static void Main(String[] args)

    StackUsingLinkedlist obj = new StackUsingLinkedlist();

class StackUsingLinkedlist

            document.write("<br>Heap Overflow");

            document.write("Stack is empty");

            document.write("<br>Stack Underflow");

        this.top = this.top.link;

     let s1 = new StackUsingLinkedlist();     

let obj = new StackUsingLinkedlist();

Time Complexity: O(N), where N is the number of elements in the given stack.
Auxiliary Space: O(N)

Array Stack Approach: This approach discusses the solution to problems in Array Stack implementation. Below are the steps:

  1. Push the top element from the given stack into an array stack.
  2. Print the top element of the array stack.
  3. Pop-out the top element from the given primary stack.
  4. Repeat the above steps in order until the given stack is empty.

Below is the implementation of the above approach:

            cout << ("Stack Overflow\n");

            cout << ("Stack Underflow\n");

            cout << ("Stack Underflow\n");

    void DisplayStack(Stack s)

            cout << (s1.peek()) << " ";

    static final int MAX = 1000;

    static void DisplayStack(Stack s)

            System.out.print(s1.peek()

    public static void main(String args[])

    Console.WriteLine("Stack Overflow");

    Console.WriteLine("Stack Underflow");

    Console.WriteLine("Stack Underflow");

public void DisplayStack(Stack s)

    Console.Write(s1.peek() + " ");

public static void Main(String []args)

        if (this.top >= (MAX - 1)) {

            let x = this.a[this.top--];

            let x = this.a[this.top];

Time Complexity: O(N), where N is the number of elements in the given stack.
Auxiliary Space: O(N)


Article Tags :