【数据结构】队列
发布时间:2020-12-15 05:50:08 所属栏目:安全 来源:网络整理
导读:数据结构队列Queue function Queue() { // 数据存储 this.dataStore = [];}Queue.prototype = { constructor: Queue,add: function(array) { this.dataStore = array; },// 排列 enqueue: function( element ) { this.dataStore.push( element ); },//随即读
数据结构队列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; } }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |