[Java]データ構造-スタック(グラフィック)

前書き

スタックは、first-in-last-outの線形構造です。

1つは、[配列の実装]スタック順序ストレージ

スタックの構造

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、スタックに

アイデア:

1.最初にスタックがいっぱいかどうかを判断し、いっぱいになったら戻ります
。2。それ以外の場合は、先頭に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;
    }

2.スタックから

アイデア:

1.最初にスタックが空かどうかを判断し、空の場合はプロンプトを表示して戻ります。
2.それ以外の場合は、最初にノードを保存してから、上から1を引きます。

写真を見て話してください:
ここに画像の説明を挿入

コード:

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

3.スタックがいっぱいであると判断します

コード:

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

4.スタックが空であることを確認します

コード:

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

5.スタックの長さを取得します

アイデア:

トップポインタは常にスタックのトップを指すため、topの値はスタックの長さですが、この場合は0から始まるため、スタックの長さを1増やす必要があります。つまり、top +1です。

写真を見て話してください:
ここに画像の説明を挿入

コード:

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

6.スタックをトラバースします

コード:

   /**
     * 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]);
        }
    }

完全なコード

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

テスト

コード:

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


    }
}

演算結果:

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

おすすめ

転載: blog.csdn.net/qq_43073558/article/details/107855023