数据结构和算法(六)--队列(Queue)

数据结构和算法(六)–队列(Queue)

什么是队列
  • 队列是一种特殊的线性表,只能在头尾进行操作
  • 队尾(rear):只能从队尾进行添加元素,一般叫做入队(enQueue)
  • 队头(front):智能从队头移除元素,一般叫做出队(deQueue)
  • 先进先出原则,First In First Out,FIFO
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Dkb4opGj-1622453483056)(imgs/队列的数据结构.png)]
队列的接口设计
  • int size();
    获取长度
  • boolean isEmpty();
    判断是否为空
  • void enQueue(E e);
    入队
  • E deQueue();
    出队
  • E front();
    获取队列头部元素
  • void clear();
    清空队列
队列接口代码实现
  • 可以使用动态数组或者链表来实现,优先使用双向链表,因为队列会频繁操作队头和队尾,而双向链表有firstNode和lastNode指针
  • CustomCircleLinkedListPlus请看《数据结构和算法(三)–双向(循环)链表(LinkedList)》
/**
 * @author maolin yuan
 * @version 1.0
 * @date 2021/5/31 16:06
 */
public class CustomQueue<E> {
    private final CustomCircleLinkedListPlus<E> list = new CustomCircleLinkedListPlus<>();

    public int size(){
        return list.size();
    }

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

    public void enQueue(E e){
        list.add(e);
    }

    public E deQueue(){
        return list.remove(0);
    }

    public E front(){
        return list.get(0);
    }

    public void clear(){
        list.clear();
    }

    public static void main(String[] args) {
        CustomQueue<Integer> queue = new CustomQueue<>();
        for (int i = 0; i < 100; i++) {
            queue.enQueue(i);
        }
        while (!queue.isEmpty()){
            System.out.println(queue.deQueue());
        }
    }

}
练习
1.用栈实现队列
  • 链接:https://leetcode-cn.com/problems/implement-queue-using-stacks/
  • 思路:定义两个栈inStack和outStack,入队时进入inStack,出队时需要把inStack全部倒到outStack再出栈
  • 代码实现:
class MyQueue {

    private final Stack<Integer> inStack = new Stack<>();

    private final Stack<Integer> outStack = new Stack<>();

    /** Initialize your data structure here. */
    public MyQueue() {

    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        if (outStack.empty()){
            outStack.push(x);
        }else {
            while (!outStack.empty()){
                inStack.push(outStack.pop());
            }
            inStack.push(x);
            while (!inStack.empty()){
                outStack.push(inStack.pop());
            }
        }
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        return outStack.pop();
    }

    /** Get the front element. */
    public int peek() {
        return outStack.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return outStack.empty();
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值