ホームページ >Java >&#&チュートリアル >スタックの下部に要素を挿入するJavaプログラム
スタックは、LIFO(最後の、最初のアウト)の原則に続くデータ構造です。言い換えれば、スタックに最後に追加する要素は、削除される最初の要素です。要素をスタックに追加(またはプッシュ)すると、それらは上に配置されます。すなわち、とりわけ以前に添付されていた要素。
スタックの下部に要素を追加する必要がある特定のシナリオがある場合があります。スタックの下部に要素を追加する方法は複数あります。それらは -
ですJavaの補助スタック(操作を実行するセカンダリスタック)を使用して、スタックの下部に要素を挿入できます。ここでは、2つのスタック(メインスタックと補助スタック)を使用して、メインスタックの下部に要素を挿入します。
メインスタックには元の要素がありますが、補助スタックは要素の再配置に役立ちます。この方法は理解しやすいです。
ステップ
再帰の使用
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); } }再帰は、スタックの下部に要素を挿入するもう1つの方法です。このアプローチでは、再帰関数を使用して、空になるまでスタックからすべての要素をポップし、空になるとスタックに新しい要素を挿入し、要素をスタックに戻します。 >
ステップ
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 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); } }スタックから要素をポップして、それらをキューに入れてください。 新しい要素を挿入します:
要素の復元:
キューから要素をdequeueし、それらをスタックに押し戻します。
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 中国語 Web サイトの他の関連記事を参照してください。