数据结构 -- 栈

栈作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表。它按照先进后出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读出来)

栈的入栈和出栈过程

图片(注:百度搜索而来的图片)链接: link.
Alt

java代码实现

// An highlighted block
import java.util.Random;

public class Stack {
    private int[] arr;
    private int size;
    private int capacity;

    public Stack() {
        this.arr = new int[10];
        this.size = 0;
        this.capacity = 10;
    }

    public static void main(String[] args) {
        Stack stack = new Stack();
        Random random = new Random();
        for (int i = 0; i < 20; i++) {
            stack.push(random.nextInt(50));
            System.out.println(stack.toString());
            //System.out.printf("stack's size is %d, this capacity is %d\n",stack.getSize(),stack.capacity);
        }
        for (int i = 0; i < 18; i++) {
            try {
                int pop = stack.pop();
                System.out.println(pop);
                System.out.println(stack.toString());
                //System.out.printf("stack's size is %d, this capacity is %d\n",stack.getSize(),stack.capacity);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println(stack.toString());
    }

    /**
     * 进栈
     *
     * @param e
     */
    public void push(int e) {
        _add(e);
    }

    private void _add(int e) {
        if (this.size >= this.capacity) {
            this.capacity = this.capacity * 2;
            int[] newarr = new int[this.capacity];
            for (int i = 0; i < this.size; i++) {
                newarr[i] = this.arr[i];
            }
            this.arr = newarr;
        }
        this.arr[this.size] = e;
        this.size++;
    }

    /**
     * 出栈
     *
     * @return
     * @throws Exception
     */
    public int pop() throws Exception {
        if (this.size < 1) throw new Exception("The stack was empty");
        int popElement = this.arr[this.size - 1];
        this.size--;
        if (this.size <= this.capacity / 4) {
            this.capacity = this.capacity / 2;
            int[] newarr = new int[this.capacity];
            for (int i = 0; i < this.size; i++) {
                newarr[i] = this.arr[i];
            }
            this.arr = newarr;
        }
        return popElement;
    }

    /**
     * 栈顶元素
     *
     * @return
     * @throws Exception
     */
    public int peek() throws Exception {
        if (this.size < 1) throw new Exception("The stack was empty");
        int peekElement = this.arr[this.size - 1];
        return peekElement;
    }

    public int getSize() {
        return this.size;
    }

    public boolean isEmpty() {
        return this.size <= 0;
    }

    public String toString() {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < this.size; i++) {
            stringBuffer.append(arr[i] + " - ");
        }
        return stringBuffer.toString();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值