Ein Stapel ist eine Datenstruktur, die dem LIFO -Prinzip (zuletzt, zuerst) folgt. Mit anderen Worten, das letzte Element, das wir einem Stapel hinzufügen, ist das erste, das entfernt wird. Wenn wir einem Stapel Elemente hinzufügen (oder drücken), werden sie oben platziert. d.h. vor allem die zuvor angezeigten Elemente.
Es kann bestimmte Szenarien geben, in denen wir am unteren Rand des Stapels ein Element hinzufügen müssen. Es gibt mehrere Möglichkeiten, ein Element zum Boden des Stapels hinzuzufügen. Sie sind -
Wir können ein Element am unteren Rand eines Stapels mit einem Hilfsstapel (einen Sekundärstapel verwenden, der mit dem von uns Vorgänge ausführen werden) in Java einfügen. Hier werden wir zwei Stapel (einen Hauptstapel und einen Hilfsstapel) verwenden, um ein Element am unteren Rand des Hauptstapels einzulegen.
Der Hauptstapel hat die ursprünglichen Elemente, während der Hilfsstapel uns hilft, die Elemente neu zu ordnen. Diese Methode ist leicht zu verstehen.
Befolgen Sie die Schritte zum Einfügen eines Elements am unteren Rand eines Stapels mit einem Hilfsstapel:
Folgendes ist ein Beispiel dafür, wie wir einen Hilfsstapel verwenden können, um ein Element unten hinzuzufügen -
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); } }
im obigen Programm starten wir zunächst die Elemente 1, 2, 3 und 4 in den Stapel. Dann übertragen wir diese Elemente in einen anderen Stapel. Danach setzen wir das Zielelement in den Hauptstapel ein. Schließlich holen wir alle Elemente zurück aus dem Hilfsstapel.
Rekursion ist eine andere Möglichkeit, ein Element am Boden eines Stapels einzulegen. Bei diesem Ansatz werden wir eine rekursive Funktion verwenden, um alle Elemente aus unserem Stapel zu streichen, bis sie leer wird. Sobald es leer wird, werden wir das neue Element in den Stapel einfügen und dann die Elemente zurück in den Stapel schieben.
Hier sind die Schritte zum Einfügen eines Elements am unteren Rand eines Stapels mit Rekursion:
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); } }
Im obigen Programm haben wir eine rekursive Funktion definiert, die ein neues Element am unteren Rand des Stapels einfügt. Wir haben die Elemente weiterhin aus dem Stapel gepoppt Wir haben die vorherigen Elemente in den Stapel wiederhergestellt.
Wir können die angegebene Aufgabe auch mit einer temporären Variablen erreichen. Wir verwenden diese Variable, um die Elemente zu speichern, während wir den Stapel manipulieren. Diese Methode ist einfach und wir können mit einer einfachen Schleife implementieren.
Befolgen Sie die Schritte zum Einfügen eines Elements am unteren Rand eines Stapels unter Verwendung einer temporären Variablen & 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); } }
Verwenden einer Warteschlange
Schritte
import java.util.Stack; public class InsertAtBottomUsingTempVar { public static void insertAtElementBottom(Stack<Integer> st, int x) { // Temporary variable to hold elements int[] temp = new int[st.size()]; int index = 0; // Transfer elements to temporary variable while (!st.isEmpty()) { temp[index++] = st.pop(); } // Push the new element into the stack st.push(x); // Restore elements from temporary variable for (int i = 0; i < index; i++) { st.push(temp[i]); } } 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); } }
In dieser Implementierung haben wir eine Warteschlange verwendet, um die Elemente für eine vorübergehende Zeit zu halten. Wir übertragen zuerst die vorhandenen Elemente aus dem Stapel in die Warteschlange. Dann schieben wir das neue Element in den Stapel und stellen die Originalelemente aus der Warteschlange zurück zum Stapel
wieder herHinweis: Wir können andere Datenstrukturen wie Array, LinkedList, ArrayList usw. anstelle einer Warteschlange verwenden.
Das obige ist der detaillierte Inhalt vonJava -Programm zum Einfügen eines Elements am unteren Rand eines Stapels. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!