Java程序將元素插入堆棧的底部
堆棧是遵循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); } }
使用遞歸
遞歸是將元素插入堆棧底部的另一種方法。在這種方法中,我們將使用遞歸函數從堆棧中彈出所有元素,直到它變為空,一旦變為空,我們將將新元素插入堆棧中,然後將元素推回堆棧中。 >
>步驟這是使用遞歸插入堆棧底部元素的步驟:
- >>基本情況:檢查堆棧是否為空。如果是空的,我們將將新元素推入堆棧。
- >遞歸案例:如果堆棧不是空的,我們將彈出頂部元素並遞歸地調用該函數。
- >還原元素:完成插入新元素後,我們需要將先前彈出的元素推回堆棧中。
>
在上面的程序中,我們定義了一個遞歸函數,該功能插入堆棧底部的新元素,然後我們繼續從堆棧中彈出元素,直到堆棧變為空,然後我們插入了新元素,此後插入,我們將以前的元素還原到堆棧中。
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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

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

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

膠囊是一種三維幾何圖形,由一個圓柱體和兩端各一個半球體組成。膠囊的體積可以通過將圓柱體的體積和兩端半球體的體積相加來計算。本教程將討論如何使用不同的方法在Java中計算給定膠囊的體積。 膠囊體積公式 膠囊體積的公式如下: 膠囊體積 = 圓柱體體積 兩個半球體體積 其中, r: 半球體的半徑。 h: 圓柱體的高度(不包括半球體)。 例子 1 輸入 半徑 = 5 單位 高度 = 10 單位 輸出 體積 = 1570.8 立方單位 解釋 使用公式計算體積: 體積 = π × r2 × h (4

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP成為許多網站首選技術棧的原因包括其易用性、強大社區支持和廣泛應用。 1)易於學習和使用,適合初學者。 2)擁有龐大的開發者社區,資源豐富。 3)廣泛應用於WordPress、Drupal等平台。 4)與Web服務器緊密集成,簡化開發部署。
