堆棧是遵循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中文網其他相關文章!