-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInsertion_at_the_end_of_stack.cpp
More file actions
74 lines (59 loc) · 1011 Bytes
/
Insertion_at_the_end_of_stack.cpp
File metadata and controls
74 lines (59 loc) · 1011 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Problem Statement:
-------------------
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
*/
// Link --> https://www.geeksforgeeks.org/program-to-insert-an-element-at-the-bottom-of-a-stack/
// Code:
#include <bits/stdc++.h>
using namespace std;
stack<int> insertUtil(stack<int> s, int N)
{
if (s.empty())
s.push(N);
else
{
int X = s.top();
s.pop();
s = insertUtil(s, N);
s.push(X);
}
return s;
}
void insertToBottom(stack<int> s, int N)
{
s = insertUtil(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;
}