관련지식
javascript, queue, stack

자바스크립트의 Array 객체는 push()pop()shift()unshift() 함수를 제공하고 있는데, 함수만 적절하게 사용하면 Queue 와 Stack 기능을 사용할 수 있습니다.
예를들어 Queue 기능으로 동작하고 싶을땐 push() 와 shift() 함수를 사용하면 되고, Stack 기능으로 동작하고 싶을땐 push()와 pop() 함수를 사용하면 됩니다.

하지만 배열 객체 그대로를 쓰는것보다는, 용도에 맞는 이름의 객체를 사용하는 것이 더 명확할 것입니다. 이번엔 큐와 스택을 만들어보겠습니다.

  1. function Queue() {
  2. this.data = [];
  3. }
  4. Queue.prototype.enqueue = function(v) {
  5. this.data.push(v);
  6. }
  7. Queue.prototype.dequeue = function() {
  8. return this.data.shift() || null;
  9. }
  10. Queue.prototype.size = function() {
  11. return this.data.length;
  12. }
  13. console.log('------ Queue');
  14. var queue1 = new Queue();
  15. queue1.enqueue('a');
  16. queue1.enqueue('b');
  17. console.log(queue1.size());
  18. queue1.enqueue('c');
  19. console.log(queue1.dequeue());
  20. console.log(queue1.dequeue());

스택

  1. function Stack() {
  2. this.data = [];
  3. }
  4. Stack.prototype.push = function(v) {
  5. this.data.push(v);
  6. }
  7. Stack.prototype.pop = function() {
  8. return this.data.pop() || null;
  9. }
  10. Stack.prototype.size = function() {
  11. return this.data.length;
  12. }
  13. console.log('------ Stack');
  14. var stack1 = new Stack();
  15. stack1.push(10);
  16. stack1.push(20);
  17. stack1.push(30);
  18. console.log(stack1.pop());
  19. console.log(stack1.pop());
  20. console.log(stack1.size());

최대 갯수가 고정된 스택

스택의 경우 기존 Array 객체의 사용법과 크게 다를것이 없지만, 이렇게 Wrapping을 할 경우 다른 기능을 붙이기가 좋습니다. 약간의 수정으로 최대 사이즈가 고정된 스택을 만들어보겠습니다. 스택이 꽉찬 상태에서 새로운 값이 푸쉬될 경우, 가장 먼저 입력된 데이터부터 버려지면서 푸쉬 시킬것입니다. 이와 같은 방법으로 부가적인 기능을 가진 큐를 만들수도 있습니다.

  1. function Stack(max) {
  2. this.max = max || 0;
  3. this.data = [];
  4. }
  5. Stack.prototype.push = function(v) {
  6. if(this.max != 0 && this.size() == this.max)
  7. this.data.shift();
  8. this.data.push(v);
  9. }

최종소스

  1. function Queue() {
  2. this.data = [];
  3. }
  4. Queue.prototype.enqueue = function(v) {
  5. this.data.push(v);
  6. }
  7. Queue.prototype.dequeue = function() {
  8. return this.data.shift() || null;
  9. }
  10. Queue.prototype.size = function() {
  11. return this.data.length;
  12. }
  13. console.log('------ Queue');
  14. var queue1 = new Queue();
  15. queue1.enqueue('a');
  16. queue1.enqueue('b');
  17. console.log(queue1.size());
  18. queue1.enqueue('c');
  19. console.log(queue1.dequeue());
  20. console.log(queue1.dequeue());
  21. function Stack(max) {
  22. this.max = max || 0;
  23. this.data = [];
  24. }
  25. Stack.prototype.push = function(v) {
  26. if(this.max != 0 && this.size() == this.max)
  27. this.data.shift();
  28. this.data.push(v);
  29. }
  30. Stack.prototype.pop = function() {
  31. return this.data.pop() || null;
  32. }
  33. Stack.prototype.size = function() {
  34. return this.data.length;
  35. }
  36. console.log('------ Stack');
  37. var stack1 = new Stack(3); //스택 최대 사이즈를 0으로 고정, 0이거나 비어있을땐 최대사이즈 없음
  38. stack1.push(10);
  39. stack1.push(20);
  40. stack1.push(30);
  41. stack1.push(40); //10 버려짐
  42. stack1.push(50); //20 버려짐
  43. console.log(stack1.pop()); //50
  44. console.log(stack1.pop()); //40
  45. console.log(stack1.size()); //1
  46. console.log(stack1.pop()); //30
  47. console.log(stack1.pop()); //스택이 비었음