【前端】JavaScript 数据结构 -- 队列

JavaScript 数据结构 – 队列

1. 队列定义

1.1 队列定义

普通队列是一种先进先出的线性表,支持尾部插入数据,头部弹出元素,特点有:

  1. 先进先出(First in First Out FIFO)
  2. 尾部插入,增加新数据
  3. 头部弹出,删除元素

1.2 示意图

在这里插入图片描述

1.3 常用方法

  1. push():追加元素
  2. pop():弹出队头元素
  3. size():返回队列大小
  4. peek():返回队列头元素而不弹出
  5. isEmpty():返回队列是否为空

2. 实现

队列的底层实现使用链表较为方便,可具体参考:JavaScript 数据结构 – 链表

2.1 类声明(建议)

class Queue {
    head = null;
    constructor() {
    }
    push(data) {
        let node = {
            data: data,
            next: null
        }
        if (this.head === null) {
            this.head = node
        } else {
            let pre = this.head;
            let next = this.head.next;
            while (next !== null) {
                pre = next;
                next = next.next;
            }
            pre.next = node;
        }
    }
    
    pop() {
        let node = this.head;
        this.head = this.head !== null ? this.head.next : null;
        return node !== null ? node.data : null;
    }

    isEmpty() {
        return this.head === null;
    }

    size() {
        if (this.head === null) {
            return 0;
        } 
        let size = 0;
        let next = this.head.next;
        size++;
        while (next !== null) {
            next = next.next;
            size++;
        }
        return size;
    }

    peek() {
        let node = this.head;
        return node !== null ? node.data : null;
    }
}

2.2 函数式声明

function Queue() {
    this.head = null;
    this.push = function(data) {
        let node = {
            data: data,
            next: null
        }
        if (this.head === null) {
            this.head = node
        } else {
            let pre = this.head;
            let next = this.head.next;
            while (next !== null) {
                pre = next;
                next = next.next;
            }
            pre.next = node;
        }
    }
    
    this.pop = function() {
        let node = this.head;
        this.head = this.head !== null ? this.head.next : null;
        return node !== null ? node.data : null;
    }

    this.isEmpty = function() {
        return this.head === null;
    }

    this.size = function() {
        if (this.head === null) {
            return 0;
        } 
        let size = 0;
        let next = this.head.next;
        size++;
        while (next !== null) {
            next = next.next;
            size++;
        }
        return size;
    }

    this.peek = function() {
        let node = this.head;
        return node !== null ? node.data : null;
    }
}

2.3 字面量声明

let queue = {
    head: null,
    push: function(data) {
        let node = {
            data: data,
            next: null
        }
        if (this.head === null) {
            this.head = node
        } else {
            let pre = this.head;
            let next = this.head.next;
            while (next !== null) {
                pre = next;
                next = next.next;
            }
            pre.next = node;
        }
    },
    
    pop: function() {
        let node = this.head;
        this.head = this.head !== null ? this.head.next : null;
        return node !== null ? node.data : null;
    },

    isEmpty: function() {
        return this.head === null;
    },

    size: function() {
        if (this.head === null) {
            return 0;
        } 
        let size = 0;
        let next = this.head.next;
        size++;
        while (next !== null) {
            next = next.next;
            size++;
        }
        return size;
    },

    peek: function() {
        let node = this.head;
        return node !== null ? node.data : null;
    }
}

3. 数值也支持队列方法

  1. shift():出队队头元素
  2. unshift(element):从队头入队元素
  3. push(element):入队元素
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值