SOURCE

function Queue(){
    this.dateStore = [];
    this.enqueue = enqueue;
    this.dequeue = dequeue;
    this.front = front;
    this.back = back;
    this.toString = toString;
    this.empty = empty;
}

function enqueue(element){
    this.dateStore.push(element);
}

function dequeue(){
    return this.dateStore.shift();
}

function front(){
    return this.dateStore[0];
}

function back(){
    return this.dateStore[this.dateStore.length -1];
}

function toString(){
    var retStr = '';
    for(var i = 0;i<this.dateStore.length;i++){
        retStr += this.dateStore[i] + '\n'
    }
    return retStr;
}

function empty(){
    if(this.dateStore.length == 0){
        return true;
    }
    else{
        return false;
    }
}

// 一、基数排序
//将数据集扫描两次,第一次按个位上的数字进行排序,第二次按十位上的数字进行排序。
function distribute(nums,queues,n,digit){
    for(var i = 0;i<n;i++){
        if(digit == 1){
            queues[nums[i]%10].enqueue(nums[i])
        }
        else{
            queues[Math.floor(nums[i]/10)].enqueue(nums[i]);
        }
    }
}

function collect(queues,nums){
    var i = 0;
    for(var digit =0;digit < 10;++digit){
        while(!queues[digit].empty()){
            nums[i++] = queues[digit].dequeue();
        }
    }
}



var queues = [];
for(var i = 0;i<10;i++){
    queues[i] = new Queue();
}
var nums = [];
for(var i = 0;i<10;i++){
    nums[i] = Math.floor(Math.floor(Math.random()* 101));
}
console.log('Before radix sort:');
console.log(nums);
distribute(nums,queues,10,1);
collect(queues,nums);
console.log(nums);
distribute(nums,queues,10,10);
collect(queues,nums);
console.log('After radix sort:');
console.log(nums);

console.log('------------------------------------------------------');

// 2.双向队列
function Deque(){
    this.dateStore = [];
    this.addToStar = addToStar;
    this.addToEnd = addToEnd;
    this.deleteToStar = deleteToStar;
    this.deleteToEnd = deleteToEnd;
    this.toString = toString;
    this.empty = empty;
    this.front = front;
    this.back = back;
}
    function addToStar(element){
        this.dateStore.unshift(element);
    }
    function addToEnd(element){
        this.dateStore.push(element);
    }
    function deleteToStar(){
        this.dateStore.shift();
    }
    function deleteToEnd(){
        this.dateStore.pop();
    }

    function toString(){
        var retStr = '';
        for(var i = 0;i<this.dateStore.length;i++){
            retStr += this.dateStore[i] + '\n'
        }
        return retStr;
    }

    function empty(){
        if(this.dateStore.length == 0){
            return true;
        }
        else{
            return false;
        }
    }

    function front(){
    return this.dateStore[0];
    }
    function back(){
    return this.dateStore[this.dateStore.length -1];
}


var str2 = new Deque();
str2.addToStar(15);
str2.addToEnd(20);
str2.addToEnd(25);
str2.addToEnd(30);
console.log(str2.toString());

str2.addToStar(10);
str2.addToEnd(35);
console.log(str2.toString());

str2.deleteToEnd();
str2.deleteToStar();
str2.deleteToEnd();
str2.deleteToStar();
console.log(str2.toString());




console.log('------------------------------------------------------');
// 3.用Deque类判断是否为回文
function isPalindrome(letter){
    var s = new Deque();
    for(var i = 0; i < letter.length;i++){
        s.addToEnd(letter[i])
    }
    while(!s.empty()){
        if(s.front() != s.back()){
            return 'No';
        }
        else{
            s.deleteToEnd();
            s.deleteToStar();
        }
    }
    return 'Yes';
}

var word = 'abcdcba';
console.log(isPalindrome(word));

var letter2 = 'agcdcba';
console.log(isPalindrome(letter2));
console 命令行工具 X clear

                    
>
console