首页 >Java >java教程 >Java程序将元素插入堆栈的底部

Java程序将元素插入堆栈的底部

DDD
DDD原创
2025-02-07 11:59:10877浏览

堆栈是遵循LIFO(最后,首先)原理的数据结构。换句话说,我们添加到堆栈中的最后一个元素是第一个要删除的元素。当我们将(或推)元素添加到堆栈中时,它们就会放在顶部;即,首先是所有先前添加的元素。

>

可能在某些情况下我们需要在堆栈底部添加一个元素。有多种方法可以在堆栈的底部添加一个元素。它们是 -

  • 使用辅助堆栈
  • 使用递归
  • 使用临时变量
  • 使用队列

使用辅助堆栈

>我们可以在Java中使用辅助堆栈(使用将执行操作的辅助堆栈(使用该辅助堆栈)插入堆栈底部的元素。在这里,我们将使用两个堆栈(一个主堆栈和一个辅助堆栈)在主堆栈底部插入一个元素。

>

>主堆栈将具有原始元素,而辅助堆栈将帮助我们重新排列元素。此方法易于理解。

>步骤

以下是使用辅助堆栈在堆栈底部插入元素的步骤:

  • >>初始化两个堆栈:创建一个主堆栈中推动其中一些元素,然后创建一个辅助堆栈。
  • >
  • 弹出所有元素:然后从主堆栈中删除所有元素,然后将它们推入第二个辅助堆栈。这将有助于我们扭转元素的顺序。
  • >
  • >按下新元素:>一旦主堆栈为空,我们需要将新元素推入主堆栈中,也可以在需要的情况下将元素推到辅助堆栈的顶部。
  • 还原原始顺序:从辅助堆栈中弹出所有元素,然后将它们推回主堆栈。这将恢复元素的原始顺序。>
  • >示例

以下是我们如何使用辅助堆栈在底部添加元素的示例

在上面的程序中,我们首先将元素1、2、3和4推入堆栈。然后,我们将这些元素转移到另一个堆栈中。之后,我们将目标元素插入主堆栈中。最后,我们从辅助堆栈中检索所有元素。

>

import java.util.Stack;
public class InsertAtBottomUsingTwoStacks {    
   public static void insertElementAtBottom(Stack<Integer> mainStack, int x) {
      // Create an extra auxiliary stack
      Stack<Integer> St2 = new Stack<>();
      
      /* Step 1: Pop all elements from the main stack 
      and push them into the auxiliary stack */
      while (!mainStack.isEmpty()) {
         St2.push(mainStack.pop());
      }

      // Step 2: Push the new element into the main stack
      mainStack.push(x);

      /* Step 3: Restore the original order by popping each 
      element from the auxiliary stack and push back to main stack */
      while (!St2.isEmpty()) {
         mainStack.push(St2.pop());
      }
   }
   public static void main(String[] args) {
      Stack<Integer> stack1 = new Stack<>();
      stack1.push(1);
      stack1.push(2);
      stack1.push(3);
      stack1.push(4);

      System.out.println("Original Stack: " + stack1);
      insertElementAtBottom(stack1, 0);
      System.out.println("Stack after inserting 0 at the bottom: " + stack1);
   }
}

使用递归

Java程序将元素插入堆栈的底部递归是将元素插入堆栈底部的另一种方法。在这种方法中,我们将使用递归函数从堆栈中弹出所有元素,直到它变为空,一旦变为空,我们将将新元素插入堆栈中,然后将元素推回堆栈中。 >

>步骤

这是使用递归插入堆栈底部元素的步骤:
  • >>基本情况:检查堆栈是否为空。如果是空的,我们将将新元素推入堆栈。
  • >递归案例:如果堆栈不是空的,我们将弹出顶部元素并递归地调用该函数。
  • >还原元素:完成插入新元素后,我们需要将先前弹出的元素推回堆栈中。>
>示例

在上面的程序中,我们定义了一个递归函数,该功能插入堆栈底部的新元素,然后我们继续从堆栈中弹出元素,直到堆栈变为空,然后我们插入了新元素,此后插入,我们将以前的元素还原到堆栈中。
import java.util.Stack;
public class InsertAtBottomUsingTwoStacks {    
   public static void insertElementAtBottom(Stack<Integer> mainStack, int x) {
      // Create an extra auxiliary stack
      Stack<Integer> St2 = new Stack<>();
      
      /* Step 1: Pop all elements from the main stack 
      and push them into the auxiliary stack */
      while (!mainStack.isEmpty()) {
         St2.push(mainStack.pop());
      }

      // Step 2: Push the new element into the main stack
      mainStack.push(x);

      /* Step 3: Restore the original order by popping each 
      element from the auxiliary stack and push back to main stack */
      while (!St2.isEmpty()) {
         mainStack.push(St2.pop());
      }
   }
   public static void main(String[] args) {
      Stack<Integer> stack1 = new Stack<>();
      stack1.push(1);
      stack1.push(2);
      stack1.push(3);
      stack1.push(4);

      System.out.println("Original Stack: " + stack1);
      insertElementAtBottom(stack1, 0);
      System.out.println("Stack after inserting 0 at the bottom: " + stack1);
   }
}

使用临时变量

我们还可以使用临时变量来实现给定的任务。我们使用此变量在操纵堆栈时存储元素。此方法很容易,我们可以使用一个简单的循环实现。

>

>步骤

以下是使用临时变量&lt;插入堆栈底部的元素的步骤

初始化临时变量:

创建一个变量以暂时保留元素,当您通过堆栈迭代时。
  • >传输元素:然后使用循环从堆栈中弹出元素,然后将这些元素存储在临时变量中。
  • >>插入新元素:>一旦我们的堆栈为空,我们就需要将新元素推入堆栈。
  • 还原元素:插入元素后,将元素从临时变量推回堆栈中。>
  • >示例
  • 在此程序中,我们使用临时数组来操纵堆栈时保持元素。然后,我们将新元素插入堆栈中,然后将原始元素还原到堆栈中。
使用队列

在这种方法中,我们将使用队列在堆栈底部插入新元素时暂时保持元素。此方法是管理元素顺序的更好方法。使用队列我们可以在不篡改现有元素的情况下进入堆栈的新元素。

>步骤
import java.util.Stack;
public class InsertAtBottomUsingRecursion {
   public static void insertAtElementBottom(Stack<Integer> st, int x) {
      // Base case: If the stack is empty, push the new element
      if (st.isEmpty()) {
         st.push(x);
         return;
      }
      // Recursive case: Pop the top element
      int top = st.pop();
      
      // Call the function recursively
      insertAtElementBottom(st, x);
      
      // Restore the top element into the stack
      st.push(top);
   }
   
   public static void main(String[] args) {
      Stack<Integer> st = new Stack<>();
      st.push(1);
      st.push(2);
      st.push(3);
      st.push(4);
   
      System.out.println("Original Stack: " + st);
      insertAtElementBottom(st, 0);
      System.out.println("Stack after inserting 0 at the bottom: " + st);
   }
}
以下是使用队列 -

在堆栈底部插入元素的步骤

>

初始化一个队列:

创建一个队列以保持堆栈中的元素。

>

传输元素:弹出堆栈中的元素,并将它们加入队列。

>插入新元素:
    将新元素推入堆栈。
  • 还原元素:
  • 排列队列中的元素,然后将它们推回堆中。
  • >示例
  • >输出
  • 以下是上述代码的输出 -
  • >
    import java.util.Stack;
    public class InsertAtBottomUsingTwoStacks {    
       public static void insertElementAtBottom(Stack<Integer> mainStack, int x) {
          // Create an extra auxiliary stack
          Stack<Integer> St2 = new Stack<>();
          
          /* Step 1: Pop all elements from the main stack 
          and push them into the auxiliary stack */
          while (!mainStack.isEmpty()) {
             St2.push(mainStack.pop());
          }
    
          // Step 2: Push the new element into the main stack
          mainStack.push(x);
    
          /* Step 3: Restore the original order by popping each 
          element from the auxiliary stack and push back to main stack */
          while (!St2.isEmpty()) {
             mainStack.push(St2.pop());
          }
       }
       public static void main(String[] args) {
          Stack<Integer> stack1 = new Stack<>();
          stack1.push(1);
          stack1.push(2);
          stack1.push(3);
          stack1.push(4);
    
          System.out.println("Original Stack: " + stack1);
          insertElementAtBottom(stack1, 0);
          System.out.println("Stack after inserting 0 at the bottom: " + stack1);
       }
    }
    
    在此实施中,我们使用队列在临时时间内将元素保存。我们首先将现有元素从堆栈转移到队列。然后,我们将新元素推入堆栈,并将原始元素从队列恢复为堆栈

    >

    >注意:>我们可以使用其他数据结构,例如数组,linkedlist,arrayList等。

以上是Java程序将元素插入堆栈的底部的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn