📜  在栈底插入元素的程序

📅  最后修改于: 2021-09-02 05:56:38             🧑  作者: Mango

给定一个堆栈S和一个整数N ,任务是在堆栈底部插入N。

例子:

天真的方法:最简单的方法是创建另一个堆栈。请按照以下步骤解决问题:

  1. 初始化一个堆栈,比如temp
  2. 继续从给定的堆栈S中弹出并将弹出的元素推入temp ,直到堆栈S变空。
  3. N压入堆栈S
  4. 现在,继续从堆栈temp弹出并将弹出的元素推入堆栈S ,直到堆栈temp变空。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to insert an element
// at the bottom of a given stack
void insertToBottom(stack S, int N)
{
    // Temporary stack
    stack temp;
 
    // Iterate until S becomes empty
    while (!S.empty()) {
 
        // Push the top element of S
        // into the stack temp
        temp.push(S.top());
 
        // Pop the top element of S
        S.pop();
    }
 
    // Push N into the stack S
    S.push(N);
 
    // Iterate until temp becomes empty
    while (!temp.empty()) {
 
        // Push the top element of
        // temp into the stack S
        S.push(temp.top());
 
        // Pop the top element of temp
        temp.pop();
    }
 
    // Print the elements of S
    while (!S.empty()) {
        cout << S.top() << " ";
        S.pop();
    }
}
 
// Driver Code
int main()
{
    // Input
    stack 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
// Java program for the above approach
import java.util.Stack;
 
class GFG{
 
// Function to insert an element
// at the bottom of a given stack
static void insertToBottom(Stack S, int N)
{
     
    // Temporary stack
    Stack temp = new Stack<>();
 
    // Iterate until S becomes empty
    while (!S.empty())
    {
 
        // Push the top element of S
        // into the stack temp
        temp.push(S.peek());
 
        // Pop the top element of S
        S.pop();
    }
 
    // Push N into the stack S
    S.push(N);
 
    // Iterate until temp becomes empty
    while (!temp.empty())
    {
 
        // Push the top element of
        // temp into the stack S
        S.push(temp.peek());
 
        // Pop the top element of temp
        temp.pop();
    }
 
    // Print the elements of S
    while (!S.empty())
    {
        System.out.print(S.peek() + " ");
        S.pop();
    }
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given Binary Tree
    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);
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to insert an element
# at the bottom of a given stack
def insertToBottom(S, N):
     
    # Temporary stack
    temp = []
 
    # Iterate until S becomes empty
    while (len(S) != 0):
 
        # Push the top element of S
        # into the stack temp
        temp.append(S[-1])
 
        # Pop the top element of S
        S.pop()
 
    # Push N into the stack S
    S.append(N)
 
    # Iterate until temp becomes empty
    while (len(temp) != 0):
 
        # Push the top element of
        # temp into the stack S
        S.append(temp[-1])
 
        # Pop the top element of temp
        temp.pop()
 
    # Print the elements of S
    while (len(S) != 0):
        print(S[-1], end = " ")
        S.pop()
 
# Driver Code
if __name__ == "__main__":
 
    # Input
    S = []
    S.append(5)
    S.append(4)
    S.append(3)
    S.append(2)
    S.append(1)
 
    N = 7
 
    insertToBottom(S, N)
 
# This code is contributed by ukasp


C#
// C# program for the above approach
 
using System;
using System.Collections;
 
public class GFG {
 
    // Function to insert an element
    // at the bottom of a given stack
    static void insertToBottom(Stack S, int N)
    {
 
        // Temporary stack
        Stack temp = new Stack();
 
        // Iterate until S becomes empty
        while (S.Count != 0) {
 
            // Push the top element of S
            // into the stack temp
            temp.Push(S.Peek());
 
            // Pop the top element of S
            S.Pop();
        }
 
        // Push N into the stack S
        S.Push(N);
 
        // Iterate until temp becomes empty
        while (temp.Count != 0) {
 
            // Push the top element of
            // temp into the stack S
            S.Push(temp.Peek());
 
            // Pop the top element of temp
            temp.Pop();
        }
 
        // Print the elements of S
        while (S.Count != 0) {
            Console.Write(S.Peek() + " ");
            S.Pop();
        }
    }
 
    // Driver code
    static public void Main()
    {
 
        // Given Binary Tree
        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);
    }
}
 
// This code is contributed by Dharanendra L V.


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Recursive function to use implicit stack
// to insert an element at the bottom of stack
stack recur(stack S, int N)
{
    // If stack is empty
    if (S.empty())
        S.push(N);
 
    else {
 
        // Stores the top element
        int X = S.top();
 
        // Pop the top element
        S.pop();
 
        // Recurse with remaining elements
        S = recur(S, N);
 
        // Push the previous
        // top element again
        S.push(X);
    }
    return S;
}
 
// Function to insert an element
// at the bottom of stack
void insertToBottom(
    stack S, int N)
{
 
    // Recursively insert
    // N at the bottom of S
    S = recur(S, N);
 
    // Print the stack S
    while (!S.empty()) {
        cout << S.top() << " ";
        S.pop();
    }
}
 
// Driver Code
int main()
{
    // Input
    stack S;
    S.push(5);
    S.push(4);
    S.push(3);
    S.push(2);
    S.push(1);
 
    int N = 7;
 
    insertToBottom(S, N);
 
    return 0;
}


输出:
1 2 3 4 5 7

时间复杂度: O(N)
辅助空间: O(N)

高效的方法:可以通过递归使用隐式堆栈,而不是使用临时堆栈。请按照以下步骤解决问题:

  1. 定义一个递归函数,它接受堆栈S和一个整数作为参数并返回一个堆栈。
  2. 要考虑的基本情况是堆栈是否为空。对于这种情况,将N压入堆栈并返回。
  3. 否则,删除S的顶部元素并将其存储在一个变量中,比如X
  4. 使用新堆栈再次递归
  5. X推入S

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Recursive function to use implicit stack
// to insert an element at the bottom of stack
stack recur(stack S, int N)
{
    // If stack is empty
    if (S.empty())
        S.push(N);
 
    else {
 
        // Stores the top element
        int X = S.top();
 
        // Pop the top element
        S.pop();
 
        // Recurse with remaining elements
        S = recur(S, N);
 
        // Push the previous
        // top element again
        S.push(X);
    }
    return S;
}
 
// Function to insert an element
// at the bottom of stack
void insertToBottom(
    stack S, int N)
{
 
    // Recursively insert
    // N at the bottom of S
    S = recur(S, N);
 
    // Print the stack S
    while (!S.empty()) {
        cout << S.top() << " ";
        S.pop();
    }
}
 
// Driver Code
int main()
{
    // Input
    stack S;
    S.push(5);
    S.push(4);
    S.push(3);
    S.push(2);
    S.push(1);
 
    int N = 7;
 
    insertToBottom(S, N);
 
    return 0;
}
输出:
1 2 3 4 5 7

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live