ホームページ >Java >&#&チュートリアル >スタックの下部に要素を挿入するJavaプログラム

スタックの下部に要素を挿入するJavaプログラム

DDD
DDDオリジナル
2025-02-07 11:59:10873ブラウズ

スタックは、LIFO(最後の、最初のアウト)の原則に続くデータ構造です。言い換えれば、スタックに最後に追加する要素は、削除される最初の要素です。要素をスタックに追加(またはプッシュ)すると、それらは上に配置されます。すなわち、とりわけ以前に添付されていた要素。

スタックの下部に要素を追加する必要がある特定のシナリオがある場合があります。スタックの下部に要素を追加する方法は複数あります。それらは -

です
  • 補助スタックを使用
  • 再帰の使用
  • 一時変数を使用して
  • キューを使用して

補助スタックを使用

Javaの補助スタック(操作を実行するセカンダリスタック)を使用して、スタックの下部に要素を挿入できます。ここでは、2つのスタック(メインスタックと補助スタック)を使用して、メインスタックの下部に要素を挿入します。

メインスタックには元の要素がありますが、補助スタックは要素の再配置に役立ちます。この方法は理解しやすいです。

ステップ

以下は、補助スタックを使用してスタックの下部に要素を挿入する手順です。

2つのスタックを初期化します。
    メインスタックを作成して、いくつかの要素をプッシュしてから補助スタックを作成します。
  • すべての要素をポップします:次に、メインスタックからすべての要素を削除し、2番目の補助スタックに押し込みます。これは、要素の順序を逆転させるのに役立ちます。
  • 新しい要素を押します:メインスタックが空になったら、新しい要素をメインスタックに押し込む必要があります。または、必要に応じて補助スタックの上に要素を押すこともできます。
  • 元の順序を復元します:補助スタックからすべての要素をポップし、それらをメインスタックに押し戻します。これにより、元の要素の順序が復元されます。
  • 以下は、補助スタックを使用して下部に要素を追加する方法の例です。
  • 上記のプログラムでは、要素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);
   }
}
再帰は、スタックの下部に要素を挿入するもう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し、それらをスタックに押し戻します。

      output
    • 以下は、上記のコードの出力です -
      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 サイトの他の関連記事を参照してください。

    声明:
    この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。