zoukankan      html  css  js  c++  java
  • JavaScript--数据结构之队列

    5.1 队列的操作
    队列是特殊的列表,只能一端入队(队尾)插入操作,一端出队(队头)删除操作。底层用数组,利用javascript数组优于其它语言的数组的方法,shift();删除第一个元素,push();队尾添加元素。分别对应出队,入队操作。
    队列是一种先进先出的数据类型(FIFO);
    一些常见用处:jQuery的动画执行顺序,打印机打印顺序,操作系统进程执行顺序,一些银行等的排队问题。
    5.2 队列的实现(用数组实现的队列);
    队列中的一些方法,
    入队队列队尾添加一个元素enqueue();
    出队队列队头删除一个元素dequeue();
    获取队列队头的元素front();
    获取队列队尾的元素back();
    队列的长度length;
    清空队列元素remove();
    判断队列元素是否为空isEmpty();
    获取队列元素qDatas();

     1 function Queue() {
     2   this.dataStore = [];
     3   this.length = 0;
     4   // this.enqueue = enqueue;
     5   // this.dequeue = dequeue;
     6   // this.remove = remove; //清空队列
     7   // this.isEmpty = isEmpty; //判断队列是否为空
     8   // this.qDatas = qDatas; //获取队列元素
     9   // this.front = front; //获取队列第一个元素
    10   // this.back = back; //获取队列最后一个元素
    11 }
    12 Queue.prototype.enqueue = function(element) {
    13   this.dataStore.push(element);
    14   this.length++;
    15 };
    16 Queue.prototype.dequeue = function() {
    17   this.dataStore.shift();
    18   this.length--;
    19 };
    20 Queue.prototype.remove = function() {
    21   this.dataStore = [];
    22   this.length = 0;
    23 };
    24 Queue.prototype.isEmpty = function() {
    25   return this.length == 0 ? true : false;
    26 };
    27 Queue.prototype.qDatas = function() {
    28   var datas = "";
    29   for(var i = 0;i < this.dataStore.length;i++) {
    30     datas += this.dataStore[i]+"
    ";
    31   }
    32   return datas;
    33 };
    34 Queue.prototype.front = function() {
    35   return this.dataStore[0];
    36 };
    37 Queue.prototype.back = function() {
    38   return this.dataStore[this.dataStore.length-1];
    39 };


    5.3 队列的使用-方块舞伴的分配
    ...
    5.4 使用队列对数据进行排序

  • 相关阅读:
    coco2d-js demo程序之滚动的小球
    【leetcode】Happy Number(easy)
    【leetcode】Remove Linked List Elements(easy)
    【leetcode】LRU Cache(hard)★
    【QT】计时器制作
    【leetcode】Min Stack(easy)
    【leetcode】Compare Version Numbers(middle)
    【leetcode】Excel Sheet Column Title & Excel Sheet Column Number (easy)
    【leetcode】Binary Search Tree Iterator(middle)
    【leetcode】Number of Islands(middle)
  • 原文地址:https://www.cnblogs.com/intelwisd/p/7285327.html
Copyright © 2011-2022 走看看