Queue

function Queue(){
    this.dataStore = [];
    this.enqueue = enqueue;
    this.dequeue = dequeue;
    this.front = front;
    this.back = back;
    this.toString = toString;
    this.empty = empty;
}
  • enqueue() : 큐의 끝부분에 요소를 추가한다.
Queue.prototype.enqueue = function(){
    this.dataStore.push(element);
}
  • dequeue() : 배열의 앞부분에서 요소를 삭제한다.
Queue.prototype.dequeue = function(){
    return this.dataStore.shift();
}
  • front() / back() : 큐의 가장 앞부분과 끝부분에 저장된 요소를 확인할 수 있다.
Queue.prototype.front = function(){
    return this.dataStore[0]
}

Queue.prototype.back = function(){
    return this.dataStore[this.dataStore.length-1];
}
  • toString() : 큐의 모든요소를 출력한다.
Queue.prototype.toString = function(){
    var retStr = "",
        i;
    for(i = 0; i<this.dataStore.length; i++){
        retStr += this.dataStore[i] + "\n";
    }
    return retStr;
}
  • empty() : 큐가 비었는지 알려주는 함수
Queue.prototype.empty = function(){
  if(this.dataStore.length==0){
    return true;
  }else{
    return false;
  }
}
  • 예제 : 댄스파티
const fs = require('fs');

function Queue(){
  this.dataStore = [];
};

Queue.prototype.enqueue = function(element) {
  // 젤뒤에 하나 추가한다.
  this.dataStore.push(element);
};

Queue.prototype.dequeue = function(){
  // 젤앞이 있는걸 삭제한다.
  return this.dataStore.shift();
};

Queue.prototype.front = function() {
  return this.dataStore[0];
};

Queue.prototype.back = function() {
  return this.dataStore[this.dataStore.length-1];
};

Queue.prototype.toString = function() {
  var retStr = "";
  for (var i = 0; i < this.dataStore.length; i++){
    retStr += this.dataStore[i] + "\n";
  }
  // dataStore안의 모든 요소를 문자열로 변환(하나로 합쳐) 후 반환
  return retStr;
};

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

Queue.prototype.count = function() {
  return this.dataStore.length;
};

function Dancer(name, sex){
  this.name = name;
  this.sex = sex;
}

function getDancers(males, females){
  var names = fs.readFileSync('dancer.txt', 'utf8').split("\n");

  for (var i = 0; i < names.length; i++){
    names[i] = names[i].trim();
  }
  for (var i = 0; i < names.length; i++){
    var dancer = names[i].split(" ");
    var sex = dancer[0];
    var name = dancer[1];

    if (sex == "F"){
      females.enqueue(new Dancer(name, sex));
    }
    else{
      males.enqueue(new Dancer(name,sex));
    }
  }
}


function dance(males, females){
  console.log("The dance partners are : \n");
  while(!females.empty() && !males.empty()){
    person = females.dequeue();
    console.log("Female dancer is : " + person.name);
    person = males.dequeue();
    console.log("Male dancer is : " + person.name);
    console.log("--------");
  }
}


var maleDancers = new Queue();
var femaleDancers = new Queue();

getDancers(maleDancers, femaleDancers);
dance(maleDancers, femaleDancers);

if(!maleDancers.empty()){
  console.log(maleDancers.front().name + " is waiting for dance. (" + maleDancers.count() + " persons waiting.)")
}
if(!femaleDancers.empty()){
  console.log(femaleDancers.front().name + " is waiting for dance. (" + femaleDancers.count() + " persons waiting.)")
}
  • 예제 : 숫자 정렬
function Queue(){
  this.dataStore = [];
};

Queue.prototype.enqueue = function(element) {
  // 젤뒤에 하나 추가한다.
  this.dataStore.push(element);
};

Queue.prototype.dequeue = function(){
  // 젤앞이 있는걸 삭제한다.
  return this.dataStore.shift();
};

Queue.prototype.front = function() {
  return this.dataStore[0];
};

Queue.prototype.back = function() {
  return this.dataStore[this.dataStore.length-1];
};

Queue.prototype.toString = function() {
  var retStr = "";
  for (var i = 0; i < this.dataStore.length; i++){
    retStr += this.dataStore[i] + "\n";
  }
  // dataStore안의 모든 요소를 문자열로 변환(하나로 합쳐) 후 반환
  return retStr;
};

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

Queue.prototype.count = function() {
  return this.dataStore.length;
};


function distribute(nums, queues, n, digit){
  for(var i = 0; i < n; i++){
    if(digit == 1){
      queues[nums[i]%10].enqueue(nums[i]);
      // 1의 자리로 정렬
      // 예) i = 0, nums[0] dl 33일 경우
      // 33 % 10 => 3
      // queues[3].enqueue(33);
    }
    else{
      queues[Math.floor(nums[i]/10)].enqueue(nums[i]);
      //10의 자리로 정렬
      //예) i = 0 , nums[0]이 33일경우
      // Math.floor(33/10) => 3 정수화.
      // queues[3].enqueue(33);
      // 100일경우 배열이 9까지밖에 없어서 에러 발생.
    }
  }
}

function collect(queues, nums){
  var i = 0;
  for(var digit = 0; digit < 10; digit++){
    while(!queues[digit].empty()){
      //해당 큐가 비어있지 않으면 아래코드를 실행합니다. 비어있다면 다음 큐로 넘어갑니다.
      nums[i++] = queues[digit].dequeue();
    }
  }
}

function dispArray(arr){
  var result = "";
  for(var i = 0; i < arr.length; i++){
    result += arr[i] + " ";
  }
  console.log(result);
}

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() * 100)); //101로 하면 에러가 발생해서 100으루
}

console.log("Befor radix sort: ");
dispArray(nums);
distribute(nums, queues, 10, 1);
collect(queues, nums);
distribute(nums, queues, 10, 10);
collect(queues, nums);
console.log("\n\nAtfer radix sort: ");
dispArray(nums);
  • 우선순위 큐
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.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 ""