Open In App

Program to insert an element at the Bottom of a Stack

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a stack s containing n integers and an integer x, the task is to insert the element x at the bottom of the stack.

Examples:

Input: x = 7
s = 1 <- (Top)
      2
     3
     4
     5
Output: 1 2 3 4 5 7

Input: x = 17
s = 1 <- (Top)
      12
    34
     47
    15
Output: 1 12 34 47 15 17

Using Temporary Stack – O(n) time and O(n) space

First, we pop all the elements from the original stack and push them onto a temporary stack. This essentially reverses the order of elements from the original stack.

Then, we push the new element at the bottom of the original stack by adding it to the now-empty stack.

After inserting the new element, we push all elements from the temporary stack back onto the original stack to restore the original order.

C++
#include <iostream>
#include <stack>
using namespace std;

void insertAtBottom(stack<int> s, int x)
{
    stack<int> temp;

    while (!s.empty()) {
        temp.push(s.top());
        s.pop();
    }

    s.push(x);

    while (!temp.empty()) {
        s.push(temp.top());
        temp.pop();
    }

    while (!s.empty()) {
        cout << s.top() << " ";
        s.pop();
    }
}

int main()
{
    stack<int> s;
    s.push(5);
    s.push(4);
    s.push(3);
    s.push(2);
    s.push(1);

    int x = 7;

    insertAtBottom(s, x);

    return 0;
}
Java
import java.util.Stack;

public class GfG {
    public static void insertAtBottom(Stack<Integer> s, int x) {
        Stack<Integer> temp = new Stack<>();

        while (!s.isEmpty()) {
            temp.push(s.pop());
        }

        s.push(x);

        while (!temp.isEmpty()) {
            s.push(temp.pop());
        }

        while (!s.isEmpty()) {
            System.out.print(s.pop() + " ");
        }
    }

    public static void main(String[] args) {
        Stack<Integer> s = new Stack<>();
        s.push(5);
        s.push(4);
        s.push(3);
        s.push(2);
        s.push(1);

        int x = 7;

        insertAtBottom(s, x);
    }
}
Python
def insertAtBottom(s, x):
    temp = []

    while s:
        temp.append(s.pop())

    s.append(x)

    while temp:
        s.append(temp.pop())

    while s:
        print(s.pop(), end=' ')

if __name__ == '__main__':
    s = []
    s.append(5)
    s.append(4)
    s.append(3)
    s.append(2)
    s.append(1)

    x = 7

    insertAtBottom(s, x)
C#
using System;
using System.Collections.Generic;

class GfG {
    static void InsertAtBottom(Stack<int> s, int x) {
        Stack<int> temp = new Stack<int>();

        while (s.Count > 0) {
            temp.Push(s.Pop());
        }

        s.Push(x);

        while (temp.Count > 0) {
            s.Push(temp.Pop());
        }

        while (s.Count > 0) {
            Console.Write(s.Pop() + " ");
        }
    }

    static void Main() {
        Stack<int> s = new Stack<int>();
        s.Push(5);
        s.Push(4);
        s.Push(3);
        s.Push(2);
        s.Push(1);

        int x = 7;

        InsertAtBottom(s, x);
    }
}
JavaScript
function insertAtBottom(s, x) {
    let temp = [];

    while (s.length > 0) {
        temp.push(s.pop());
    }

    s.push(x);

    while (temp.length > 0) {
        s.push(temp.pop());
    }

    while (s.length > 0) {
        console.log(s.pop() + ' ');
    }
}

let s = [];
s.push(5);
s.push(4);
s.push(3);
s.push(2);
s.push(1);

let x = 7;

insertAtBottom(s, x);

Output
1 2 3 4 5 7 

Using Recursion – O(n) time and O(n) space

We one by one remove stack elements and hold these elements in the recursion call stack. Once the stack becomes empty, we push the given element x into the stack and then one by one push the elements held in the recursion call stack.

C++
#include <iostream>
#include <stack>
using namespace std;

stack<int> recur(stack<int> &s, int x)
{
    if (s.size() == 0) {
        s.push(x);
    } else {
        int y = s.top();
        s.pop();
        s = recur(s, x);
        s.push(y);
    }
    return s;
}

stack<int> insertAtBottom(stack<int> s, int x)
{
    s = recur(s, x);
    return s;
}

int main()
{
    stack<int> s;
    s.push(5);
    s.push(4);
    s.push(3);
    s.push(2);
    s.push(1);

    int x = 7;

    s = insertAtBottom(s, x);

    while (!s.empty()) {
        cout << s.top() << " ";
        s.pop();
    }

    return 0;
}
Java
import java.util.Stack;

public class GfG {
    public static Stack<Integer> recur(Stack<Integer> s, int x) {
        if (s.isEmpty()) {
            s.push(x);
        } else {
            int y = s.pop();
            s = recur(s, x);
            s.push(y);
        }
        return s;
    }

    public static Stack<Integer> insertAtBottom(Stack<Integer> s, int x) {
        s = recur(s, x);
        return s;
    }

    public static void main(String[] args) {
        Stack<Integer> s = new Stack<>();
        s.push(5);
        s.push(4);
        s.push(3);
        s.push(2);
        s.push(1);

        int x = 7;

        s = insertAtBottom(s, x);

        while (!s.isEmpty()) {
            System.out.print(s.pop() + " ");
        }
    }
}
Python
def recur(s, x):
    if not s:
        s.append(x)
    else:
        y = s.pop()
        recur(s, x)
        s.append(y)
    return s


def insertAtBottom(s, x):
    return recur(s, x)

if __name__ == '__main__':
    s = []
    s.append(5)
    s.append(4)
    s.append(3)
    s.append(2)
    s.append(1)

    x = 7

    s = insertAtBottom(s, x)

    while s:
        print(s.pop(), end=' ')
C#
using System;
using System.Collections.Generic;

class GfG {
    public static Stack<int> Recur(Stack<int> s, int x) {
        if (s.Count == 0) {
            s.Push(x);
        } else {
            int y = s.Pop();
            s = Recur(s, x);
            s.Push(y);
        }
        return s;
    }

    public static Stack<int> InsertAtBottom(Stack<int> s, int x) {
        s = Recur(s, x);
        return s;
    }

    static void Main() {
        Stack<int> s = new Stack<int>();
        s.Push(5);
        s.Push(4);
        s.Push(3);
        s.Push(2);
        s.Push(1);

        int x = 7;

        s = InsertAtBottom(s, x);

        while (s.Count > 0) {
            Console.Write(s.Pop() + " ");
        }
    }
}
JavaScript
function recur(s, x) {
    if (s.length === 0) {
        s.push(x);
    } else {
        let y = s.pop();
        recur(s, x);
        s.push(y);
    }
    return s;
}

function insertAtBottom(s, x) {
    return recur(s, x);
}

let s = [];

s.push(5);
    s.push(4);
    s.push(3);
    s.push(2);
    s.push(1);

let x = 7;

s = insertAtBottom(s, x);

while (s.length > 0) {
    console.log(s.pop());
}

Output
1 2 3 4 5 7


Next Article

Similar Reads