微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

【数据结构】队列

数据结构队列Queue


function Queue() {
  // 数据存储
  this.dataStore = [];

}
Queue.prototype = {
  constructor: Queue,add: function(array) {
    this.dataStore = array;
  },// 排列
  enqueue: function( element ) {
    this.dataStore.push( element );
  },//随即读取
  randomData: function() {
    var index  = Math.floor(Math.random() * (this.length()-1));
    if ( index < 0 ) {
      //index = 0;
    }
    return this.dataStore.splice(index,1);
  },// 出列
  dequeue: function() {
   return this.dataStore.shift();
  },// 读取队首
  front: function() {
    return this.dataStore[0];
  },// 读取队尾
  back: function() {
    return this.dataStore[this.dataStore.length - 1];
  },// 显示队列内所有元素
  toString: function() {
    var retStr = "";
    for ( var i = 0,length = this.dataStore.length; i< len; i++ ) {
      retStr +=  "src:" + this.dataStore[i].src + " city:" + this.dataStore[i].city + " position: " + this.dataStore[i].position;
    }
    return retStr;
  },// 判断队列是否为空
  empty: function() {
    if ( this.dataStore.length == 0 ) {
      return true;
    } else {
      return false;
    }
  },length: function() {
    return this.dataStore.length;
  }
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐