• 분명히 좋은 방법이 있을텐데, 스택이랑 똑같이 함..
function Queue(){
  this.dataStore = [];
};

Queue.prototype.enqueue = function(element) {
  this.dataStore.push(element);
};

Queue.prototype.dequeue = function(){
  var result = this.dataStore.shift();
  return result;
};

Queue.prototype.last = function(){
  var result = this.dataStore.pop();
  return result;
}

Queue.prototype.front = function() {
  var result = this.dataStore[0] ? this.dataStore[0] : -1
  console.log(result);
  return result;
};

Queue.prototype.back = function() {
  var last = this.dataStore[this.dataStore.length-1];
  console.log(last ? last : -1);
  return last;
};

Queue.prototype.size = function() {
  var result = this.dataStore.length
  console.log(result);
  return result;
};

Queue.prototype.empty = function() {
  if(this.dataStore.length == 0){
    console.log(1);
    return true;
  }else{
    console.log(0);
    return false;
  }
};

function isPalindrome(word){
  var q = new Queue();
  for(var i = 0; i < word.length; i++){
    q.enqueue(word[i]);
  }

  var rword = "";
  while(!q.empty()){
    rword += q.last();
  }
  var result = word == rword ? true : false;
  return result;
}

var word = "1001";
isPalindrome(word) ? console.log("It is Palindrome") : console.log("It is not Palindrome");
  • 우선순위 반대로 (높은수가 우선 지진처럼)
function Queue(){
  this.dataStore = [];
};

Queue.prototype.enqueue = function(element) {
  this.dataStore.push(element);
};

Queue.prototype.dequeue = function(){
  var entry = 0;
  for(var i = 0; i < this.dataStore.length; i++){
    if(this.dataStore[i].code > this.dataStore[entry].code){
      entry = i;
    }
  }
  return this.dataStore.splice(entry, 1);
};

Queue.prototype.last = function(){
  var result = this.dataStore.pop();
  return result;
}

Queue.prototype.front = function() {
  var result = this.dataStore[0] ? this.dataStore[0] : -1
  console.log(result);
  return result;
};

Queue.prototype.back = function() {
  var last = this.dataStore[this.dataStore.length-1];
  console.log(last ? last : -1);
  return last;
};

Queue.prototype.size = function() {
  var result = this.dataStore.length
  console.log(result);
  return result;
};

Queue.prototype.empty = function() {
  if(this.dataStore.length == 0){
    console.log(1);
    return true;
  }else{
    console.log(0);
    return false;
  }
};

function Patient(name, code){
  this.name = name;
  this.code = code;
}


var p = new Patient("Smith", 5),
    ed = new Queue();

ed.enqueue(p);
p = new Patient("David", 4);
ed.enqueue(p);
p = new Patient("Tim", 3);
ed.enqueue(p);
p = new Patient("Christine", 2);
ed.enqueue(p);

console.log(ed.dataStore);
var seen = ed.dequeue();
console.log(seen[0].name  + " : " + seen[0].code);
seen = ed.dequeue();
console.log(seen[0].name  + " : " + seen[0].code);
seen = ed.dequeue();
console.log(seen[0].name  + " : " + seen[0].code);

results matching ""

    No results matching ""