【Java】数据结构——栈(图文)

文章目录

    • 简介
    • 一、【数组实现】栈的顺序存储
      • 栈的结构
      • 1、入栈
      • 2、出栈
      • 3、判断栈满
      • 4、判断栈空
      • 5、获取栈的长度
      • 6、遍历栈
      • 完整代码
      • 测试

简介

栈是一种先进后出的线性结构。

一、【数组实现】栈的顺序存储

栈的结构

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、否则先将top加1,然后给栈数组赋值。

看图说话:
【Java】数据结构——栈(图文)_第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.否则,先保存结点,后top减1。

看图说话:
【Java】数据结构——栈(图文)_第2张图片

代码:

/**
     * 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指针一直指向栈的顶部,所以top的值就是栈的长度,但是由于本案例是从0开始的,所以栈的长度需要加1,即:top+1。

看图说话:
【Java】数据结构——栈(图文)_第3张图片

代码:

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

你可能感兴趣的:(数据结构与算法,数据结构)