zoukankan      html  css  js  c++  java
  • 队列

    语雀入口

     https://www.yuque.com/along-n3gko/ezt5z9

    介绍

       队列是遵循FIFO(First In First Out,先进先出,也称为先来先服务)原则的一组有序的项。 队列在尾部添加新元素,并从顶部移除元素。最新添加的元素必须排在队列的末尾。

    在现实中,最常见的队列就是排队,当然不允许插队的存在哦

     分析

      实现一个队列,首先需要一个存储队列的数据结构,我们可以使用数组。

    1 function Queue() {
    2     var items = [];
    3 }

    基本方法

    • 向队列添加方法,新项只能添加在末尾
    • 从队列移除元素,遵循先进先出原则,所以最先添加项也是被最先移除的。
    • 查看队列长度
    • 检查队列是否为空,为空会返回true,否则返回false
    • 查看队列头元素
    • 查看队列尾元素
    • 清空队列
    • 打印队列元素 

    代码实现

     1 function Queue() {
     2   var items = [];
     3   this.enqueue = function(data) {
     4     items.push(data);
     5   };
     6   this.dequeue = function() {
     7     return items.shift();
     8   };
     9   this.size = function() {
    10     return items.length;
    11   };
    12   this.isEmpty = function() {
    13     return items.length === 0;
    14   };
    15 
    16   this.head = function() {
    17     return items[0] || null;
    18   };
    19   this.tail = function() {
    20     let len = items.length;
    21     if (len > 0) {
    22       return items[len - 1];
    23     }
    24     return null;
    25   };
    26   this.clear = function() {
    27     items = [];
    28     return true;
    29   };
    30   this.show = function() {
    31     console.log(items.toString());
    32   };
    33 }
     

     

  • 相关阅读:
    systemmap 使用记录
    reading code record
    吞吐问题
    debug cps 原因
    fopen的a+和rewind
    debug cps && perf debug
    tfo以及quic的阅读笔记
    ss 和netstat
    debug open files
    多核编程 local global
  • 原文地址:https://www.cnblogs.com/alongup/p/12899704.html
Copyright © 2011-2022 走看看