数据结构从入门到放弃-栈

1.栈定义

栈也是一种线性结构,但是是一种受限的线性表,它只能在栈顶进行操作,栈底不能进行操作。

通常栈的操作有 构建空栈,入栈,出栈,判断是否空栈,获取栈顶元素等等

2.栈的分类

栈是线性表,按照栈的物理存储方式可以分为顺序栈和链式的栈。顺序栈基本是用数组来实现,链式栈通常通过链表来实现

3.顺序栈

public class MyStack {

    private int[] table;

    private int top;


    public MyStack(int initSize){
        this.table = new int[initSize];
        this.top = -1;
    }

    public boolean push(int element){
        if(top + 1 >= table.length){
            throw new RuntimeException("stack over flow");
        }

        table[top + 1] = element;
        top = top + 1;
        return true;
    }

    public int pop(){
        if(top == -1){
            throw new RuntimeException("stack is empty");
        }
        int element = table[top];
        top = top -1;
        return element;
    }

    public boolean clearStack(){
        top = -1;
        return true;
    }

    public boolean isEmpty(){
        return top == -1;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值