堆栈是遵循LIFO(最后,首先)原理的数据结构。换句话说,我们添加到堆栈中的最后一个元素是第一个要删除的元素。当我们将(或推)元素添加到堆栈中时,它们就会放在顶部;即,首先是所有先前添加的元素。
>可能在某些情况下我们需要在堆栈底部添加一个元素。有多种方法可以在堆栈的底部添加一个元素。它们是 -
>我们可以在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); } }
使用递归
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中文网其他相关文章!