¸Þ¼¼Áö À̺¥Æ®¸¦ ó¸®Çϱâ À§Çؼ´Â Å¥°¡ ÇÊ¿äÇÏ´Ù. Å¥ÀÇ °£´ÜÇÑ »ç¿ë¹ý°ú ±¸Çö Äڵ带 ¾Ë¾Æº»´Ù. Queue »ç¿ë¹ý1 var queue = new Queue();
queue.push(5); queue.push(3); queue.pop(); queue.push(8); queue.push(7); queue.pop(); queue.push(9); queue.push(10); queue.pop(); queue.print(); Queue »ç¿ë¹ý2 function AddPeople(q) {
for(var n = 0; n < 5; ++n) { var obj = {}; obj.number = n; obj.name = "Kim" + String(n); q.push(obj); } } var people = new Queue(); AddPeople(people); while(!people.isEmpty()) { var node = people.pop(); console.log(node.number + " = " + node.name); } ¿ÀºêÁ§Æ®¸¦ Å¥¿¡ ³Ö°í ÀÖ´Ù. Queue.js var Queue = function(){
this.data = []; } Queue.prototype.isEmpty = function(){ return this.data.length==0?true:false; } Queue.prototype.length = function(){ return this.data.length; } Queue.prototype.push = function(node){ this.data.push(node); } Queue.prototype.pop = function(){ var node = this.peek(); this.data.shift(); return node; } Queue.prototype.peek = function(){ var node = this.data[0] == undefined ? null: this.data[0]; return node; } Queue.prototype.toArray = function(){ return this.data; } Queue.prototype.print = function(){ console.log(this.data); } Queue.prototype.clearAll = function(){ this.dats = []; } ¹è¿À» ÀÌ¿ëÇØ Å¥¸¦ ±¸ÇöÇÏ°í ÀÖ´Ù. shift¸¦ ÀÌ¿ëÇÏ°í Àֱ⠶§¹®¿¡ ÆÛÆ÷¸Õ½º¿¡ ¹®Á¦°¡ »ý±æ¼ö ÀÖ´Ù. Âü°í) http://mythinkg.blogspot.com/2015/04/data-structure-javascript-queue.html |