java-栈和队列

本文介绍了栈和队列的基础概念,以及它们与List的区别。通过Java代码展示了如何使用数组和链表实现栈和队列。此外,还讨论了循环队列的实现,包括其有效元素的两种状态和如何处理队列满或空的情况。最后,文章提到了标准库中Stack在空栈时执行pop或peak操作会抛出异常的情况。
摘要由CSDN通过智能技术生成
  1. 栈和队列也是基于List来实现的,限制比List更严格(操作更少)List比栈和队列更灵活。

Stack 栈:只支持三个核心操作:入栈,出栈,取栈顶元素(先进后出)。

Queue 队列:也是有三个核心操作:入队列,出队列,取队首(先进先出)。


public class MyStack {
    //管理一些int元素,不考虑扩容问题
    private int[] array = new int[100];
    private int size = 0;

    //入栈
    public void push(int x) {
        array[size] = x;
        size++;
    }
    //取栈顶元素(最后进的那个)
    public int peak() {
        return array[size-1];
    }
    //出栈
    public int pop() {
        int ret = array[size-1];
        size--;
        return ret;
    }
}

public class MyQueueByLinkedList {
    //Node这个类叫做内部类,定义在某个类或者方法中的类
    //static 效果就是:创建Node的实例不依赖于MyQueueByLinkedList这个类的实例
    static class Node {
           public int val;
           Node next = null;

        public Node(int val) {
            this.val = val;
        }
    }
    //创建一个链表,就得有头节点,此处head节点不是傀儡节点
    //基于链表来实现队列,可以入队列从尾部插入,出队列从头删除;也可以入队列从头部插入,出队列从尾部删除
    //无论是哪种实现方式,最好把头和尾都记录下来
    private Node head = null;
    private Node tail = null;

    //此处按照尾部入队列,头部出队列的方式实现
    //入队列(标准库中的队列,入队列的操作叫offer)
    public void offer(int val) {
        Node newNode = new Node(val);
        if (head == null) {
            head = newNode;
            tail = newNode;
            return;
        }
        //当前如果不是空链表
        tail.next = newNode;
        tail = tail.next;
    }
    //出队列
    public Integer poll() {
        //如果当前队列是空队列
        if (head == null) {
            return null;
        }
        int ret = head.val;
        head = head.next;
        if (head == null) {
           //删除完当前元素后,队列变成了空队列
            tail = null;
        }
        return ret;
    }
    //取队首元素
    public Integer peek() {
        if (head == null) {
            return null;
        }
        return head.val;
    }
}

 

     2.循环队列

队列中的有效元素有两种情况:(1)head在tail之前   (2)tail在head之前

当head和tail重合时,有可能是队列为空,也有可能是满了。可以引入size标记空还是满。


public class MyQueueByArray {
    private int[] array = new int[100];
    //[head,tail)有效元素的范围,注意tail可能在head之前
    private int head = 0;
    private int tail = 0;
    private int size = 0;//元素个数
//入队列
    public void offer(int val) {
        if (size == array.length) {
            return;//队列满了,无法继续插入
        }
        //保证下标不能越界
        array[tail] = val;
        tail++;
        //tail++之后如果超出了数组的有效范围,就从头开始
        if (tail >= array.length) {
            tail = 0;
        }
        size++;
    }
//出队列
    public Integer poll() {
        if (size == 0) {
            return null;
        }
        Integer ret = array[head];
        head++;
        if (head >= array.length) {
            head = 0;
        }
        size--;
        return ret;
    }
//取队首元素
    public Integer peak() {
        if (size == 0) {
            return null;
        }
        return array[head];
    }
}

    3.标准库中的stack为空时,再pop,peak都会抛出异常。


public class TestStack {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        int ret = stack.pop();
        System.out.println(ret);
        ret = stack.pop();
        System.out.println(ret);
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值