Programma Java per implementare la struttura dei dati dello stack

In questo esempio, impareremo a implementare la struttura dei dati dello stack in Java.

Per comprendere questo esempio, è necessario conoscere i seguenti argomenti di programmazione Java:

  • Java Stack Class
  • Java Generics

Esempio 1: programma Java per implementare 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(); ) )

Produzione

 Inserimento 1 Inserimento 2 Inserimento 3 Stack: 1, 2, 3, Dopo aver estratto 1, 2, 

Nell'esempio precedente, abbiamo implementato la struttura dei dati dello stack in Java.

Per saperne di più, visita Stack Data Structure.

Esempio 2: implementare lo stack utilizzando la classe Stack

Java fornisce una Stackclasse costruita che può essere utilizzata per implementare uno stack.

 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); ) )

Produzione

 Stack: (cane, cavallo, gatto) Stack dopo pop: (cane, cavallo)

Nell'esempio precedente, abbiamo utilizzato la Stackclasse per implementare lo stack in Java. Qui,

  • animals.push () - inserisce gli elementi in cima alla pila
  • animals.pop () - rimuove l'elemento dalla cima dello stack

Nota, abbiamo usato le parentesi angolari durante la creazione dello stack. Rappresenta che lo stack è di tipo generico.

Articoli interessanti...