美文网首页
队列和双端队列

队列和双端队列

作者: 媛猿YY | 来源:发表于2021-08-13 11:30 被阅读0次

    队列

    队列是先进先出,比如排队,先排的先得到服务

    const { Queue } = PacktDataStructuresAlgorithms;
    
    const queue = new Queue();
    console.log("队列为空",queue.isEmpty()); // outputs true
    queue.enqueue('John'); //添加John
    queue.enqueue('Jack');//添加Jack
    console.log(queue.toString()); //打印 John,Jack
    queue.enqueue('Camila');
    console.log(queue.toString()); // John,Jack,Camila
    console.log(queue.size()); // outputs 3
    console.log(queue.isEmpty()); // outputs false
    queue.dequeue(); // remove John 从队首开始删
    queue.dequeue(); // remove Jack 从队首开始删
    console.log(queue.toString()); // Camila
    
    

    双端队列

    双端队列同时遵守了先进先出和后进先出的原则,可以说它是一种结合队列和栈的数据结构

    const { Deque } = PacktDataStructuresAlgorithms;
    
    const deque = new Deque();
    console.log(deque.isEmpty()); // outputs true
    deque.addBack('John');
    deque.addBack('Jack');
    console.log(deque.toString()); // John,Jack
    deque.addBack('Camila');
    console.log(deque.toString()); // John,Jack,Camila
    console.log(deque.size()); // outputs 3
    console.log(deque.isEmpty()); // outputs false
    deque.removeFront(); // remove John
    console.log(deque.toString()); // Jack,Camila
    deque.removeBack(); // Camila decides to leave
    console.log(deque.toString()); // Jack
    deque.addFront('John'); // John comes back for information
    console.log(deque.toString()); // John,Jack
    
    

    相关文章

      网友评论

          本文标题:队列和双端队列

          本文链接:https://www.haomeiwen.com/subject/rgedbltx.html