数据结构与算法笔记——队列

1.队列

队列是一种特殊的线性表,只能在头尾两端进行操作
队尾(rear):只能从队尾添加元素,一般叫做 enQueue,入队
队头(front):只能从队头移除元素,一般叫做 deQueue,出队
先进先出的原则,First In First Out,FIFO

2.队列的接口设计

int size(); // 元素的数量

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

boolean isEmpty(); // 是否为空

    public boolean isEmpty() {
        return list.isEmpty();
    }

void clear(); // 清空

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

void enQueue(E element); // 入队

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

E deQueue(); // 出队

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

E front(); // 获取队列的头元素

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

3.双端队列

双端队列是能在头尾两端添加、删除的队列

4.接口设计

int size(); // 元素的数量

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

boolean isEmpty(); // 是否为空

    public boolean isEmpty() {
        return list.isEmpty();
    }

void clear(); // 清空

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

void enQueueRear(E element); // 从队尾入队

    public void enQueueRear(E element) {
        list.add(element);
    }

E deQueueFront(); // 从队头出队

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

void enQueueFront(E element); // 从队头入队

    public void enQueueFront(E element) {
        list.add(0, element);
    }

E deQueueRear(); // 从队尾出队

    public E deQueueRear() {
        return list.remove(list.size() - 1);
    }

E front(); // 获取队列的头元素

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

E rear(); // 获取队列的尾元素

    public E rear() {
        return list.get(list.size() - 1);
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值