Java-Programm zur Implementierung der Stack-Datenstruktur

In diesem Beispiel lernen wir, die Stack-Datenstruktur in Java zu implementieren.

Um dieses Beispiel zu verstehen, sollten Sie die folgenden Java-Programmierthemen kennen:

  • Java-Stapelklasse
  • Java Generics

Beispiel 1: Java-Programm zur Implementierung von Stack

 // Stack implementation in Java class Stack ( // store elements of stack private int arr(); // represent top of stack private int top; // total capacity of the stack private int capacity; // Creating a stack Stack(int size) ( // initialize the array // initialize the stack variables arr = new int(size); capacity = size; top = -1; ) // push elements to the top of stack public void push(int x) ( if (isFull()) ( System.out.println("Stack OverFlow"); // terminates the program System.exit(1); ) // insert element on top of stack System.out.println("Inserting " + x); arr(++top) = x; ) // pop elements from top of stack public int pop() ( // if stack is empty // no element to pop if (isEmpty()) ( System.out.println("STACK EMPTY"); // terminates the program System.exit(1); ) // pop element from top of stack return arr(top--); ) // return size of the stack public int getSize() ( return top + 1; ) // check if the stack is empty public Boolean isEmpty() ( return top == -1; ) // check if the stack is full public Boolean isFull() ( return top == capacity - 1; ) // display elements of stack public void printStack() ( for (int i = 0; i <= top; i++) ( System.out.print(arr(i) + ", "); ) ) public static void main(String() args) ( Stack stack = new Stack(5); stack.push(1); stack.push(2); stack.push(3); System.out.print("Stack: "); stack.printStack(); // remove element from stack stack.pop(); System.out.println("After popping out"); stack.printStack(); ) )

Ausgabe

 Einfügen 1 Einfügen 2 Einfügen 3 Stapel: 1, 2, 3, Nach dem Herausspringen 1, 2, 

Im obigen Beispiel haben wir die Stack-Datenstruktur in Java implementiert.

Weitere Informationen finden Sie unter Stapeldatenstruktur.

Beispiel 2: Implementieren Sie den Stack mit der Stack-Klasse

Java bietet eine erstellte StackKlasse, mit der ein Stapel implementiert werden kann.

 import java.util.Stack; class Main ( public static void main(String() args) ( // create an object of Stack class Stack animals= new Stack(); // push elements to top of stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: " + animals); // pop element from top of stack animals.pop(); System.out.println("Stack after pop: " + animals); ) )

Ausgabe

 Stapel: (Hund, Pferd, Katze) Stapel nach Pop: (Hund, Pferd)

Im obigen Beispiel haben wir die StackKlasse verwendet, um den Stack in Java zu implementieren. Hier,

  • animal.push () - füge Elemente oben in den Stapel ein
  • animal.pop () - entferne das Element von der Oberseite des Stapels

Beachten Sie, dass wir beim Erstellen des Stapels die spitzen Klammern verwendet haben . Es stellt dar, dass der Stapel vom generischen Typ ist.

Interessante Beiträge...