94 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第12天,点击查看活动详情

前言

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

一、栈

我们称数据进入到栈的动作称为压栈,数据从栈中出去的动作为弹栈,如下图:

image-20221211214804900.png

1.1、栈 API 设计

类名Stack<T>
构造方法Stack() :创建 Stack 对象
成员方法1、public boolean isEmpty() :判断栈是否为空
2、public int size() :获取栈中元素的个数
3、public T pop() :弹出栈顶元素
4、public void push(T t) :向栈中压入元素 t
成员变量1、private Node head :记录头节点
2、private int N :当前栈的元素个数
成员内部类private class Node :节点类

1.2、栈代码实现

public class Stack<T>{

    //头节点
    private Node head;
    //记录元素个数
    private int N;

    public Stack() {
        head = new Node(null, null);
        N = 0;
    }

    private class Node{
        public Node next;
        public T item;

        public Node(Node next, T item) {
            this.next = next;
            this.item = item;
        }
    }


    public boolean isEmpty(){
        return N == 0;
    }

    public int size(){
        return N;
    }

    public void clear(){
        head.next = null;
        N = 0;
    }

    public void push(T item){
        Node temp = head.next;
        Node newNode = new Node(null, item);
        head.next = newNode;
        newNode.next = temp;
        N++;
    }

    public T pop(){
        Node temp = head.next;
        if(temp == null)return null;
        head.next = temp.next;
        N--;
        return temp.item;
    }
}

1.3、栈遍历

我们也让 Stack 支持增强 for 循环:

1、让 Stack 实现 Iterable 接口,重写 iterator 接口

2、在 Stack 内部提供一个内部类 SIterator,实现 Iterator 接口,重写 hasNext() 方法和 next() 方法

具体实现:

public class Stack<T> implements Iterable<T>{

    //头节点
    private Node head;
    //记录元素个数
    private int N;

    public Stack() {
        head = new Node(null, null);
        N = 0;
    }

    private class Node{
        public Node next;
        public T item;

        public Node(Node next, T item) {
            this.next = next;
            this.item = item;
        }
    }

    public boolean isEmpty(){
        return N == 0;
    }

    public int size(){
        return N;
    }

    public void clear(){
        head.next = null;
        N = 0;
    }

    public void push(T item){
        Node temp = head.next;
        Node newNode = new Node(null, item);
        head.next = newNode;
        newNode.next = temp;
        N++;
    }

    public T pop(){
        Node temp = head.next;
        if(temp == null)return null;
        head.next = temp.next;
        N--;
        return temp.item;
    }

    //================================== 新增部分代码 start =================================
    @NonNull
    @Override
    public Iterator<T> iterator() {
        return new SIterator();
    }

    private class SIterator implements Iterator<T>{

        private Node temp;

        public SIterator() {
            temp = head;
        }

        @Override
        public boolean hasNext() {
            return temp.next != null;
        }

        @Override
        public T next() {
            temp = temp.next;
            return temp.item;
        }
    }
    //================================== 新增部分代码 end =================================
}

1.4、栈测试

public class StackTest {

    public static void main(String[] args) {
        Stack<String> integerStack = new Stack<>();
        //压栈
        integerStack.push("a");
        integerStack.push("b");
        integerStack.push("c");
        integerStack.push("d");
        //一、增强 for 循环遍历
        for (String str : integerStack) {
            System.out.println(str);
            //d
            //c
            //b
            //a
            
        }

        //二、弹栈
        System.out.println(integerStack.pop());//d
        System.out.println(integerStack.pop());//c

        //三、清空
        integerStack.clear();
        System.out.println(integerStack.size());//0
    }
}

二、总结

本篇文章我们介绍了:

1、栈:一种先进后出的数据结构

2、栈的 API 设计,代码实现,增强 for 循环,测试

好了,本篇文章到这里就结束了,感谢你的阅读🤝