UNPKG

emath

Version:
176 lines 3.78 kB
/** * @method * @file dataStruct.js * @desc For create some data structure. * @createDate 2018.7.11. * @author yhzheng */ "use strict"; /** * @class stack * @classdesc Data structure: stack * @desc Init stack */ class stack { constructor(){ this.value = []; } /** * @method * @returns number The top value of stack * @desc To get the top value of stack */ top(){ return this.value[this.value.length - 1]; } /** * @methodk * @desc To remove the last value of stack */ pop(){ this.value.splice(this.value.length - 1,1); } /** * @method * @param {anyType} value The value of you want to pushed the value * @desc To push value */ push(value){ this.value.push(value); } /** * @method * @returns bool The stack is not empty * @desc To get the stack is not empty(true or false) */ empty(){ return this.value.length === 0; } } /** * @class queue * @classdesc Data structure: queue * @desc Init queue */ class queue { constructor(){ this.value = []; } /** * @method * @returns number The front value of queue * @desc To get the top value of queue */ front(){ return this.value[0]; } /** * @method * @returns number The back value of queue * @desc To get the back value of queue */ back(){ return this.value[this.value.length - 1]; } /** * @methodk * @desc To remove the last value of queue */ pop(){ this.value.splice(this.value.length - 1,1); } /** * @method * @param {anyType} value The value of you want to pushed the value * @desc To push value */ push(value){ this.value.push(value); } /** * @method * @returns bool The queue is not empty * @desc To get the queue is not empty(true or false) */ empty(){ return this.value.length === 0; } } /** * @class priority_queue * @classdesc Data structure: priority_queue * @desc Init priority_queue */ class priority_queue { constructor(){ this.value = []; } /** * @method * @returns number The front value of queue * @desc To get the max value of queue */ top(){ return this.value[getMax()]; } /** * @method * @returns number The front value of queue * @desc To get the top value of queue */ front(){ return this.value[0]; } /** * @method * @returns number The back value of queue * @desc To get the back value of queue */ back(){ return this.value[this.value.length - 1]; } /** * @method * @returns number The index of back value of queue * @desc To get the index of back value of queue */ getMax(){ var index = 0,max = this.value[0]; for (var i = 1 ; i < this.value.length ; i++){ if (max < this.value[i]){ max = this.value[i]; index = i; } } return index; } /** * @methodk * @desc To remove the last value of queue */ pop(){ this.value.splice(this.getMax(),1); } /** * @method * @param {anyType} value The value of you want to pushed the value * @desc To push value */ push(value){ this.value.push(value); } /** * @method * @returns bool The queue is not empty * @desc To get the queue is not empty(true or false) */ empty(){ return this.value.length === 0; } } module.exports.stack = stack; module.exports.queue = queue; module.exports.priority_queue = priority_queue;