堆棧是遵循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 無盡。

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Dreamweaver CS6
視覺化網頁開發工具

禪工作室 13.0.1
強大的PHP整合開發環境

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。