Javascript Data Structure Stack search

Description

Javascript Data Structure Stack search


function Stack(array) {
    this.array = [];/* w  ww .  j a v a  2 s  .c  o  m*/
    if (array) this.array = array;
}

Stack.prototype.getBuffer = function() {
    return this.array.slice();
}

Stack.prototype.isEmpty = function() {
    return this.array.length == 0;
}

Stack.prototype.peek = function() {
    return this.array[this.array.length - 1];
}


Stack.prototype.push = function(value) {
    this.array.push(value);
}


Stack.prototype.pop = function() {
    return this.array.pop();
};

function stackSearch(stack, element){
    var bufferArray = stack.getBuffer();

    var bufferStack = new Stack(bufferArray);

    while(!bufferStack.isEmpty()){
        if(bufferStack.pop()==element){
            return true;
        }
    }
    return false;
}
var stack3 = new Stack();
stack3.push(1);
stack3.push(2);
stack3.push(3);
let a = stackSearch(stack3,3); // true
console.log(a);



PreviousNext

Related