English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriel de base Java

Contrôle de flux Java

Java Tableau

Java Orienté objet (I)

Java Orienté objet (II)

Java Orienté objet (III)

Gestion des exceptions Java

Java Liste (List)

Java Queue (file d'attente)

Java Map ensemble

Java Set ensemble

Java Entrée/Sortie (I/O)

Java Reader/Writer

Autres sujets Java

Programme Java pour implémenter la structure de données pile (Stack)

    Java example summary

Dans cet exemple, nous allons apprendre à implémenter la structure de données pile en Java.

Pour comprendre cet exemple, assurez-vous d'avoir d'abord consulté les tutoriels suivants,

Exemple1:Programme Java pour implémenter la pile

// Implémentation de Stack en Java
class Stack {
  //Stocke les éléments de la pile
  private int arr[];
  //Représente le sommet de la pile
  private int top;
  //La capacité totale de la pile
  private int capacity;
  //Créer une pile
  Stack(int size) {
    // Initialiser le tableau
    // Initialiser les variables de pile
    arr = new int[size];
    capacity = size;
    top = -1;
  }
  // Envoyer un élément au sommet de la pile
  public void push(int x) {
    if (isFull()) {
      System.out.println("Dépassement de capacité de pile");
      // Terminer le programme
      System.exit(1);
    }
    //Insérer un élément au sommet de la pile
    System.out.println("Insérer "); + x);
    arr[++top] = x;
  }
  //Pop an element from the top of the stack
  public int pop() {
    //Si la pile est vide
    //Il n'y a pas d'élément à éjecter
    if (isEmpty()) {
      System.out.println("PILE VIDE");
      //Terminer le programme
      System.exit(1);
    }
    //Pop an element from the top of the stack
    return arr[top--];
  }
  //Renvoyer la taille de la pile
  public int getSize() {
    return top + 1;
  }
  // Vérifier si la pile est vide
  public Boolean isEmpty() {
    return top == -1;
  }
  // Vérifier si la pile est pleine
  public Boolean isFull() {
    return top == capacity - 1;
  }
  // Afficher les éléments de la pile
  public void printStack() {
    for (int i = 0; i <= top;++) {
      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("Pile : ");
    stack.printStack();
    //Supprimer un élément de la pile
    stack.pop();
    System.out.println("\nAprès avoir retiré");
    stack.printStack();
  }
}

Output result

Insérer 1
Insérer 2
Insérer 3
Pile : 1, 2, 3,  
Après avoir retiré
1, 2,

Dans cet exemple, nous avons déjà implémenté la structure de données pile en Java.

Exemple2:使用Stack类实现堆栈

Java fournit une classe intégrée Stack pouvant être utilisée pour implémenter une pile.

import java.util.Stack;
class Main {
  public static void main(String[] args) {
    //Créer un objet de la classe Stack
    Stack<String> animals = new Stack<>();
    //Push an element onto the top of the stack
    animals.push("Dog");
    animals.push("Horse");
    animals.push("Cat");
    System.out.println("Stack: ", + animals);
    //Pop an element from the top of the stack
    animals.pop();
    System.out.println("After popping, Stack: ", + animals);
    }
}

Output result

Stack: [Dog, Horse, Cat]
Stack: [Dog, Horse]

In the above example, we used Java's Stack class to implement the stack. Here,

  • animals.push() - Insert an element at the top of the stack

  • animals.pop() - Remove an element from the top of the stack

Note that we use <> when creating a stack. It indicates that the stack is a generic type. For more information on generics, please visitJava generics.

Java example summary