堆栈是遵循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); } } </integer></integer></integer>
使用递归
递归是将元素插入堆栈底部的另一种方法。在这种方法中,我们将使用递归函数从堆栈中弹出所有元素,直到它变为空,一旦变为空,我们将将新元素插入堆栈中,然后将元素推回堆栈中。 >
>步骤这是使用递归插入堆栈底部元素的步骤:
- >>基本情况:检查堆栈是否为空。如果是空的,我们将将新元素推入堆栈。
- >递归案例:如果堆栈不是空的,我们将弹出顶部元素并递归地调用该函数。
- >还原元素:完成插入新元素后,我们需要将先前弹出的元素推回堆栈中。
>
在上面的程序中,我们定义了一个递归函数,该功能插入堆栈底部的新元素,然后我们继续从堆栈中弹出元素,直到堆栈变为空,然后我们插入了新元素,此后插入,我们将以前的元素还原到堆栈中。
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); } } </integer></integer></integer>
使用临时变量
我们还可以使用临时变量来实现给定的任务。我们使用此变量在操纵堆栈时存储元素。此方法很容易,我们可以使用一个简单的循环实现。
>>步骤
以下是使用临时变量&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); } } </integer></integer>以下是使用队列 -
在堆栈底部插入元素的步骤
>
初始化一个队列:创建一个队列以保持堆栈中的元素。
>
传输元素:- 将新元素推入堆栈。
- 还原元素: 排列队列中的元素,然后将它们推回堆中。
- >示例
- >输出 以下是上述代码的输出 -
- >
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); } } </integer></integer></integer>
在此实施中,我们使用队列在临时时间内将元素保存。我们首先将现有元素从堆栈转移到队列。然后,我们将新元素推入堆栈,并将原始元素从队列恢复为堆栈>
>注意:>我们可以使用其他数据结构,例如数组,linkedlist,arrayList等。
以上是Java程序将元素插入堆栈的底部的详细内容。更多信息请关注PHP中文网其他相关文章!

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

Atom编辑器mac版下载
最流行的的开源编辑器

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)