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";
}
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";
}
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]);
}
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();
}
}
}
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));
}
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);