Program to insert an element at the Bottom of a Stack
Given a stack S and an integer N, the task is to insert N at the bottom of the stack.
Examples:
Input: N = 7
S = 1 <- (Top)
2
3
4
5
Output: 1 2 3 4 5 7
Input: N = 17
S = 1 <- (Top)
12
34
47
15
Output: 1 12 34 47 15 17
Naive Approach: The simplest approach would be to create another stack. Follow the steps below to solve the problem:
- Initialize a stack, say temp.
- Keep popping from the given stack S and pushing the popped elements into temp, until the stack S becomes empty.
- Push N into the stack S.
- Now, keep popping from the stacktemp and push the popped elements into the stack S, until the stack temp becomes empty.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void insertToBottom(stack< int > S, int N)
{
stack< int > temp;
while (!S.empty()) {
temp.push(S.top());
S.pop();
}
S.push(N);
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 N = 7;
insertToBottom(S, N);
return 0;
}
|
Java
import java.util.Stack;
class GFG{
static void insertToBottom(Stack<Integer> S, int N)
{
Stack<Integer> temp = new Stack<>();
while (!S.empty())
{
temp.push(S.peek());
S.pop();
}
S.push(N);
while (!temp.empty())
{
S.push(temp.peek());
temp.pop();
}
while (!S.empty())
{
System.out.print(S.peek() + " " );
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 N = 7 ;
insertToBottom(S, N);
}
}
|
Python3
def insertToBottom(S, N):
temp = []
while ( len (S) ! = 0 ):
temp.append(S[ - 1 ])
S.pop()
S.append(N)
while ( len (temp) ! = 0 ):
S.append(temp[ - 1 ])
temp.pop()
while ( len (S) ! = 0 ):
print (S[ - 1 ], end = " " )
S.pop()
if __name__ = = "__main__" :
S = []
S.append( 5 )
S.append( 4 )
S.append( 3 )
S.append( 2 )
S.append( 1 )
N = 7
insertToBottom(S, N)
|
C#
using System;
using System.Collections;
public class GFG {
static void insertToBottom(Stack S, int N)
{
Stack temp = new Stack();
while (S.Count != 0) {
temp.Push(S.Peek());
S.Pop();
}
S.Push(N);
while (temp.Count != 0) {
S.Push(temp.Peek());
temp.Pop();
}
while (S.Count != 0) {
Console.Write(S.Peek() + " " );
S.Pop();
}
}
static public void Main()
{
Stack S = new Stack();
S.Push(5);
S.Push(4);
S.Push(3);
S.Push(2);
S.Push(1);
int N = 7;
insertToBottom(S, N);
}
}
|
Javascript
<script>
function insertToBottom(S, N)
{
var temp = [];
while (S.length!=0) {
temp.push(S[S.length-1]);
S.pop();
}
S.push(N);
while (temp.length!=0) {
S.push(temp[temp.length-1]);
temp.pop();
}
while (S.length!=0) {
document.write( S[S.length-1] + " " );
S.pop();
}
}
var S = [];
S.push(5);
S.push(4);
S.push(3);
S.push(2);
S.push(1);
var N = 7;
insertToBottom(S, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient Approach: Instead of using a temporary stack, the implicit stack can be used through recursion. Follow the steps below to solve the problem:
- Define a recursion function that accepts the stack S and an integer as parameters and returns a stack.
- Base case to be considered is if the stack is empty. For this scenario, push N into the stack and return it.
- Otherwise, remove the top element of S and store it in a variable, say X.
- Recurse again using the new stack
- Push X into S.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
stack< int > recur(stack< int > S, int N)
{
if (S.empty())
S.push(N);
else {
int X = S.top();
S.pop();
S = recur(S, N);
S.push(X);
}
return S;
}
void insertToBottom(
stack< int > S, int N)
{
S = recur(S, N);
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 N = 7;
insertToBottom(S, N);
return 0;
}
|
Java
import java.util.*;
public class Main
{
static Stack<Integer> recur(Stack<Integer> S, int N)
{
if (S.size() == 0 )
S.push(N);
else {
int X = S.peek();
S.pop();
S = recur(S, N);
S.push(X);
}
return S;
}
static void insertToBottom(Stack<Integer> S, int N)
{
S = recur(S, N);
while (S.size() > 0 ) {
System.out.print(S.peek() + " " );
S.pop();
}
}
public static void main(String[] args) {
Stack<Integer> S = new Stack<Integer>();
S.push( 5 );
S.push( 4 );
S.push( 3 );
S.push( 2 );
S.push( 1 );
int N = 7 ;
insertToBottom(S, N);
}
}
|
Python3
def recur(S, N):
if ( len (S) = = 0 ):
S.append(N)
else :
X = S[ - 1 ]
S.pop()
S = recur(S, N)
S.append(X)
return S
def insertToBottom(S, N):
S = recur(S, N)
while ( len (S) > 0 ):
print (S.pop(), end = " " )
S = []
S.append( 5 )
S.append( 4 )
S.append( 3 )
S.append( 2 )
S.append( 1 )
N = 7
insertToBottom(S, N)
|
C#
using System;
using System.Collections;
class GFG {
static Stack recur(Stack S, int N)
{
if (S.Count == 0)
S.Push(N);
else {
int X = ( int )S.Peek();
S.Pop();
S = recur(S, N);
S.Push(X);
}
return S;
}
static void insertToBottom(Stack S, int N)
{
S = recur(S, N);
while (S.Count > 0) {
Console.Write(( int )S.Peek() + " " );
S.Pop();
}
}
static void Main() {
Stack S = new Stack();
S.Push(5);
S.Push(4);
S.Push(3);
S.Push(2);
S.Push(1);
int N = 7;
insertToBottom(S, N);
}
}
|
Javascript
<script>
function recur(S, N)
{
if (S.length == 0)
S.push(N);
else {
let X = S[S.length - 1];
S.pop();
S = recur(S, N);
S.push(X);
}
return S;
}
function insertToBottom(S, N)
{
S = recur(S, N);
while (S.length > 0) {
document.write(S[S.length - 1] + " " );
S.pop();
}
}
let S = [];
S.push(5);
S.push(4);
S.push(3);
S.push(2);
S.push(1);
let N = 7;
insertToBottom(S, N);
</script>
|
Time Complexity: O(N), where N is the total number of elements in the stack.
Auxiliary Space: O(N)
Last Updated :
23 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...