栈(顺序栈、链式栈)

栈是限定仅在表尾进行插入删除操作的线性表。
表尾称为栈顶,表头端称为栈底。
栈是后进先出

顺序栈

我们先来构造一个栈

class SqStack {
    private int top;
    private int[] elem;

    public SqStack() {
        this(10);
    }

    public SqStack(int size) {
        this.top = 0;
        this.elem = new int[size];
    }
}

下面来看栈的各种操作叭
我们先来入栈吧~~
入栈前肯定要先看栈是不是已经放满了对吧
如果放满了也没关系,可以扩容呀,就很nice了

判满

    public boolean isFull(){
        return this.top == this.elem.length;
    }

入栈

    public void push (int val){
        if (isFull()){
            inc();
        }
        //非空栈中的top始终在栈顶元素的下一个位置上
        this.elem[this.top] = val;
        this.top++;
    }
    //扩容
    public void inc(){
        this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
    }

然后就是出栈了
同样,出栈前我们肯定要看栈是不是空的
如果是空的,就没什么可以出啦

判空

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

出栈

    public void pop(){
        if (isEmpty()){
            return;
        }
        --this.top;
    }

得到栈顶元素

    public int getTop(){
        if (isEmpty()){
            return -1;
        }
        return this.elem[this.top -1];
    }

打印

    public void show(){
        for (int i = 0; i < this.top; i++) {
            System.out.printf(this.elem[i] + " ");
        }
        System.out.println();
    }

测试

public class SqStackDemo {

    public static void main(String[] args) {
        SqStack sqStack = new SqStack();
        for (int i = 0; i < 10; i++) {
            sqStack.push(i);
        }
        sqStack.show();
        sqStack.pop();
        sqStack.pop();
        sqStack.pop();
        sqStack.show();
    }
}

在这里插入图片描述

链式栈

链式栈就是采用单链表来保存栈中所有元素
头结点head就相当于top

class LinkStack{
    class Entry{
        int data;
        Entry next;

        public Entry(){
            this.data = -1;
            this.next = null;
        }
        public Entry(int val){
            this.data = val;
            this.next = null;
        }
    }
    private Entry head = new Entry();
}

对于链式栈的出栈和入栈我们都选择头插法,因为这样时间复杂度都为O(1)
下面来看看链式栈的操作吧
入栈

    public void push(int data){
        Entry entry = new Entry(data);
        entry.next = this.head.next;
        this.head.next = entry;
    }

出栈

        public void pop(){
        if (this.head.next == null){
            throw new UnsupportedOperationException("栈为空");
        }else {
            Entry cur = this.head.next;
            this.head.next = cur.next;
        }
    }

得到栈顶元素

    public int getTop(){
        if (this.head.next == null){
            throw new UnsupportedOperationException("栈为空");
        }
        return this.head.next.data;
    }

打印

    public void show(){
        Entry cur = this.head.next;
        while (cur != null){
            System.out.printf(cur.data+" ");
            cur = cur.next;

        }
        System.out.println();
    }

测试

public class LinkStackDemo {
    public static void main(String[] args) {
        LinkStack linkStack = new LinkStack();
        for (int i = 0; i < 10; i++) {
            linkStack.push(i);
        }
        linkStack.show();
        linkStack.pop();
        linkStack.pop();
        linkStack.show();
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值