#include <bits/stdc++.h> // Standard headers (iostream, stack, etc.)
using namespace std; // Standard namespace
// Function to take user input for stack elements
void inputStack(stack<int> &st) { // Pass stack by reference to modify it
cout << "Enter the size : ";
cin >> size; // Get desired size
cout << "Enter stack elements : ";
for(int i=0; i<size; i++) { // Loop to get elements
cin >> temp; // Read element
st.push(temp); // Push onto stack
// Function to print stack elements (consumes the stack as it pops)
void printStack(stack<int> st) { // Pass by value to print a copy, preserving original
while(!st.empty()) { // Loop until stack is empty
cout << st.top() << " "; // Print top element
st.pop(); // Remove top element
// Recursive function to insert data at the bottom of the stack
void insertBottom(stack<int> &st, int data) { // Pass stack by reference
// Base Case: If stack is empty, insert data
st.push(data); // Data is now at the very bottom
return; // End recursion for this branch
// Step 1: Hold the top element and pop it
int top = st.top(); // Store top element
st.pop(); // Remove top element
// Step 2: Recursive call to reach the bottom
insertBottom(st, data); // Recurse to insert data deeper
// Step 3: Push back the held element (backtracking)
st.push(top); // Push the stored top element back
stack<int> st; // Create a stack
inputStack(st); // Populate the stack from user input
printStack(st); // Print the original stack
cout << "Inserting 10 at bottom..." << endl;
insertBottom(st, 10); // Call function to insert 10 at bottom
printStack(st); // Print the stack after insertion