[Java] pile de structure de données (graphique)

introduction

La pile est une structure linéaire avec premier entré dernier sorti.

Stockage de l'ordre de pile [implémentation de la baie

La structure de la pile

public class ArrayStack {
    
    
    /**
     * max size of the stack
     */
    private int maxSize;
    /**
     * a stack object
     */
    private Object[] stack;
    /**
     * init the top of the stack
     * from 0 to  maxSize 
     */
    private int top = -1;

    public ArrayStack(int maxSize) {
    
    
        this.maxSize = maxSize;
        stack = new Object[this.maxSize];
    }

	    ------functions------
}

1, dans la pile

Idées:

1. Déterminez d'abord si la pile est pleine et renvoyez si elle est pleine
2. Sinon, ajoutez 1 en haut, puis attribuez une valeur au tableau de pile.

Regardez l'image et parlez:
Insérez la description de l'image ici

Code:

/**
     * push a element
     *
     * @param object
     */
    public void push(Object object) {
    
    
        if (this.isFull()) {
    
    
            System.out.println("the stack is full ! can't push !");
            return;
        }
        stack[++top] = object;
    }

2. Hors de la pile

Idées:

1. Déterminez d'abord si la pile est vide, si elle est vide, demandez et retournez.
2. Sinon, enregistrez d'abord le nœud, puis soustrayez 1 du haut.

Regardez l'image et parlez:
Insérez la description de l'image ici

Code:

/**
     * pop a element
     *
     * @return
     */
    public Object pop() {
    
    
        if (isFull()) {
    
    
            throw new RuntimeException("the stack is empty!");
        }
        return stack[top--];
    }

3. Jugez que la pile est pleine

Code:

  /**
     * Whether the stack is full
     *
     * @return
     */
    public boolean isFull() {
    
    
        return top > this.maxSize - 1;
    }

4. Déterminez que la pile est vide

Code:

/**
     * Whether the stack is empty
     *
     * @return
     */
    public boolean isEmpty() {
    
    
        return top == -1;
    }

5. Obtenez la longueur de la pile

Idées:

Étant donné que le pointeur du haut pointe toujours vers le haut de la pile, la valeur de top est la longueur de la pile, mais comme ce cas commence à 0, la longueur de la pile doit être augmentée de 1, à savoir: top + 1.

Regardez l'image et parlez:
Insérez la description de l'image ici

Code:

 /**
     * get  length of stack
     * @return
     */
    public int getLength(){
    
    
        return top+1;
    }

6. Traversez la pile

Code:

   /**
     * print a stack
     */
    public void list() {
    
    
        if (isEmpty()) {
    
    
            System.out.println("the stack is empty!");
        }
        for (int i = top; i >= 0; i--) {
    
    
            System.out.print("stack[" + i + "]=");
            System.out.println(stack[i]);
        }
    }

Code complet

package com.qingfeng.stack.array;

@SuppressWarnings("all")
public class ArrayStack {
    
    
    /**
     * max size of the stack
     */
    private int maxSize;
    /**
     * a stack object
     */
    private Object[] stack;
    /**
     * init the top of the stack
     * from 0 to  maxSize
     */
    private int top = -1;

    public ArrayStack(int maxSize) {
    
    
        this.maxSize = maxSize;
        stack = new Object[this.maxSize];
    }

    /**
     * Whether the stack is empty
     *
     * @return
     */
    public boolean isEmpty() {
    
    
        return top == -1;
    }

    /**
     * Whether the stack is full
     *
     * @return
     */
    public boolean isFull() {
    
    
        return top > this.maxSize - 1;
    }

    /**
     * push a element
     *
     * @param object
     */
    public void push(Object object) {
    
    
        if (this.isFull()) {
    
    
            System.out.println("the stack is full ! can't push !");
            return;
        }
        stack[++top] = object;
    }

    /**
     * pop a element
     *
     * @return
     */
    public Object pop() {
    
    
        if (isFull()) {
    
    
            throw new RuntimeException("the stack is empty!");
        }
        return stack[top--];
    }

    /**
     * print a stack
     */
    public void list() {
    
    
        if (isEmpty()) {
    
    
            System.out.println("the stack is empty!");
        }
        for (int i = top; i >= 0; i--) {
    
    
            System.out.print("stack[" + i + "]=");
            System.out.println(stack[i]);
        }
    }

    /**
     * get  length of stack
     * @return
     */
    public int getLength(){
    
    
        return top+1;
    }
}

test

Code:

public class ArrayStackTest {
    
    
    public static void main(String[] args) {
    
    
        ArrayStack stack = new ArrayStack(5);
        /*------------------------------------------------------------------*/
        System.out.println("-----testPush-----");
        stack.push(10);
        stack.push(20);
        stack.push(30);
        stack.push(80);
        stack.list();
        /*------------------------------------------------------------------*/
        System.out.println("-----testPop-----");
        stack.pop();
        stack.list();
        /*------------------------------------------------------------------*/
        System.out.println("-----testGetLength-----");
        System.out.println("the length of stack is: "+stack.getLength());


    }
}

résultat de l'opération:

-----testPush-----
stack[3]=80
stack[2]=30
stack[1]=20
stack[0]=10
-----testPop-----
stack[2]=30
stack[1]=20
stack[0]=10
-----testGetLength-----
the length of stack is: 3

Process finished with exit code 0

Je suppose que tu aimes

Origine blog.csdn.net/qq_43073558/article/details/107855023
conseillé
Classement