Queue ์˜ ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ

๐Ÿƒ๐Ÿปโ€โ™‚๏ธ์‚ฌ์ „ ์ค€๋น„

์ผ๋‹จ ์ด ๊ธ€์„ ์ฒ˜์Œ ๋ณธ๋‹ค๋ฉด,

https://dev-seolleung2.netlify.app/development/Stack-Queue/

https://dev-seolleung2.netlify.app/development/Stack-Queue-2/

์ด๋ ‡๊ฒŒ ๋‘๊ฐœ๋ฅผ ์„ ํ–‰ ํ•™์Šต ํ•ด์ค€๋‹ค. ์•„๋‹˜ ๋ง๊ณ ..

queue ํ์šฐ์—์—์šฐ์— ๋Š” ๋ฌด์Šจ ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ๋”๋ผ?

์•„! ์„ ์ž… ์„ ์ถœํ•˜๋Š” ์—ญํ• ์„ ํ–ˆ์—ˆ๊ตฌ๋‚˜.

queue
๋‹ค์Œ๊ณผ ๊ฐ™์€ method๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š” :
enqueue(element) - ์š”์†Œ๋ฅผ ํ์˜ ๋’ค์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
dequeue() - ์š”์†Œ๋ฅผ ํ์˜ ์•ž์—์„œ ์ œ๊ฑฐํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
size() - ํ์˜ ํ˜„์žฌ ์š”์†Œ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Queue ์˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๋‚ด๋ถ€ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ณผ์ •์ด๋‹ค.

๊ณ ๊ณ ์‹ฑ!

๐Ÿคฎํ์šฐ์—์šฐ์—‘ Queue

part-1/src/queue.js ํŒŒ์ผ์„ ํ™•์ธํ•˜๋ฉด ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™๋‹ค.

class Queue {
  constructor() {
    this.storage = {}
    this.front = 0
    this.rear = 0
  }

  size() {}

  enqueue(element) {}

  dequeue() {}
}

module.exports = Queue

stack ์„ ์ง„ํ–‰ํ–ˆ์„ ๋•Œ ์ฒ˜๋Ÿผ storage ๋ผ๋Š” ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์„ ๊ฐ์ฒด๊ฐ€ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ front, rear?

๐Ÿค”๊ณผ์ • 1. constructor ๋‚ด์˜ this.front, this.rear ์˜ ์—ญํ•  ์ดํ•ด

stack ์„ ๋งŒ๋“ค ๋•Œ๋Š” top ์ด๋ผ๋Š” ๋ณ€์ˆ˜ ํ•˜๋‚˜๋ฉด ๋˜์—ˆ๋Š”๋ฐ, ์™œ ์ด๋ฒˆ์—” 2๊ฐœ์ง€?

์ผ๋‹จ this.front ๋Š” 0 ์ด๊ณ , ์ดˆ๊ธฐํ™” ๊ฐ’์ด์ž ์•ž์œผ๋กœ๋„ ์•ˆ๋ฐ”๋€Œ๋Š” ๊ณ ์ •๋œ ๊ฐ’! ์ด๋ผ๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋จผ์ € ๋„ฃ์€ ์ž๋ฃŒ๊ฐ€ ๋‚˜์ค‘์— ๋นผ๋ ค๊ณ  ํ•  ๋•Œ ์ œ์ผ ๋จผ์ €, ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ๋งจ ์•ž์—์„œ ๋น ์ง€๊ฒŒ ๋  ๊ฑฐ๋ผ๋Š” ๊ฑฐ๋‹ค.

์–˜๋Š” 0์ด๋‹ค 0.

// ๋งจ ์•ž์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•  ๋•Œ ์“ธ ์ธ๋ฑ์Šค (์˜ˆ: ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ unshift) --> ๊ณ ์ •๋œ ๊ฐ’
this.front = 0

๊ทธ๋ฆฌ๊ณ  this.rear ๋Š” storage ์— ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ์š”์†Œ๋“ค์˜ ๊ฐœ์ˆ˜ ์ด๋ฉด์„œ,

๋“ค์–ด๊ฐ€๋Š” ๊ฐ ๋ฐ์ดํ„ฐ ์š”์†Œ์˜ key ๊ฐ€ ๋œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด storage ์— ๋ฐ์ดํ„ฐ ํ•œ ๊ฐœ๊ฐ€ ์ตœ์ดˆ ๋“ค์–ด๊ฐ”์„ ๋•Œ ๋“ค์–ด๊ฐ„ ๊ฐฏ์ˆ˜๋Š” 1์ด ๋˜๋ฉด์„œ, ํ•ด๋‹น ๋“ค์–ด๊ฐ„ ๋ฐ์ดํ„ฐ์˜ key ์˜ ์—ญํ•  (index ์ฒ˜๋Ÿผ!) ์„ ๊ฒธ์—…ํ•˜๋Š” ์•„์ด์ธ ๊ฒƒ์ด๋‹ค!

์ฆ‰ ๋ฐ์ดํ„ฐ์˜ ๊ฐฏ์ˆ˜์™€ key ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ์—ญํ• ์„ ๋‘˜๋‹ค ์ปจํŠธ๋กคํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋ผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค!

(ํˆฌํƒ€๊ฒธ์—…์˜ ๋Œ€๋ช…์‚ฌ ์˜ค์˜คํƒ€๋‹ˆโ€ฆโšพ๏ธ)

๊ทธ ์—ญํ• ์„ ๋ฐ”๋กœ this.rear ๊ฐ€ ํ•˜๋Š” ๊ฒƒ์ด๊ณ  ์ดˆ๊ธฐ๊ฐ’์€ 0 ์œผ๋กœ ๋‘์—ˆ๋‹ค.

constructor() {
  this.storage = {}; // ๊ฒฐ๊ณผ๊ฐ’
  this.front = 0; // ๋งจ ์•ž์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•  ๋•Œ ์“ธ ์ธ๋ฑ์Šค (์˜ˆ: ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ unshift) --> ๊ณ ์ •๋œ ๊ฐ’
  this.rear = 0; // ๊ฐœ์ˆ˜, ์ธ๋ฑ์Šค(๊ฐœ์ˆ˜-1) --> ์ปจํŠธ๋กค๋Ÿฌ
}

๐Ÿค”๊ณผ์ • 2. class ๋‚ด ๋ฉ”์†Œ๋“œ size() ๋Š” ๋ฌด์—‡์ผ๊นŒ?

์ธ์Šคํ„ด์Šค.size()

์œ„์˜ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ๋‚ด๊ฐ€ storage ์— enqueue ์ด๋‚˜ dequeue ์„ ํ†ตํ•ด ๋„ฃ์€ (์•„๋ž˜์— ๋‹ค๋ฃธ) ์†์„ฑ๋“ค์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

์™œ? this.rear ๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ฐฏ์ˆ˜๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์œ„์— ์„ค๋ช… ๐Ÿ˜ƒ!

return this.rear; ๋ฅผ ํ†ตํ•ด ์–ด๋– ํ•œ ์ž๋ฃŒ๋ฅผ ๋„ฃ๊ณ  ๋บ„ ๋•Œ, ๋ณ€์ˆ˜ rear ๊ฐ€ ํ•˜๋‚˜ ์ปค์ง€๊ฑฐ๋‚˜ ์ž‘์•„์ง€๋Š” ๊ฒƒ์„ ์ปจํŠธ๋กค ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ–ˆ๋‹ค.

constructor() {
  this.storage = {}; // ๊ฒฐ๊ณผ๊ฐ’
  this.front = 0; // ๋งจ ์•ž์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•  ๋•Œ ์“ธ ์ธ๋ฑ์Šค (์˜ˆ: ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ unshift) --> ๊ณ ์ •๋œ ๊ฐ’
  this.rear = 0; // ๊ฐœ์ˆ˜, ์ธ๋ฑ์Šค(๊ฐœ์ˆ˜-1) --> ์ปจํŠธ๋กค๋Ÿฌ
  }
  size() {
    return this.rear;
  }

๐Ÿค”๊ณผ์ • 3. class ๋‚ด ๋ฉ”์†Œ๋“œ enqueue() ๋Š” ๋ฌด์—‡์ผ๊นŒ?

stack ์˜ push ์ฒ˜๋Ÿผ ๊ฐ’์„ ๋ฐ€์–ด ๋„ฃ์–ด์ฃผ๋Š” ์—ญํ• ์ด๋‹ค. ์•„๊นŒ stack ์— ๊ฐ’์„ ๋„ฃ๋Š” ๊ฒƒ๊ณผ ๋˜‘๊ฐ™์€ ๋ฐฉ์‹์ด๋‹ค.

enqueue(element) {
  this.storage[this.rear] = element; // this.storage = { 0: element1, 1: element2, 2: element3 }
  this.rear++;
  return this.storage;
}

๐Ÿค”๊ณผ์ • 4. class ๋‚ด ๋ฉ”์†Œ๋“œ dequeue() ๋Š” ๋ฌด์—‡์ผ๊นŒ?

์„ ์ž… ์„ ์ถœ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋“ฏ์ด dequeue() ์˜ ์—ญํ• ์€ storage ์˜ ๋งจ ์•ž ์š”์†Œ๋ฅผ ๋นผ๊ณ  ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ์—ญํ• ์ผ ๊ฒƒ์ด๋‹ค.

๐Ÿคง๊ณผ์ • 4-1. ๋นผ๊ณ  ์‹ถ์ง€๋งŒ ๋บ„ ๊ฒŒ ์—†๋Š” ์˜ˆ์™ธ์ƒํ™ฉ

// this.rear ๊ฐ€ (storage ๋‚ด ๊ฐฏ์ˆ˜๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋‹ค๋ฉด)
if (this.rear === 0) {
  return this.storage
}

if ๋กœ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค€ ๋’ค ์•„๋ž˜ ์ฝ”๋“œ ๋ถ€ํ„ฐ ์ด์ œ ์ผ๋ฐ˜์ ์ธ ์š”์†Œ ๋นผ๊ธฐ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

๐ŸŒณ๊ณผ์ • 4-2. storage ๋‚ด์˜ ๋งจ ์•ž์˜ ๋ฐ์ดํ„ฐ ์š”์†Œ ์„ ํƒํ•ด์„œ ๋ณ€์ˆ˜์— ๋‹ด๊ธฐ

๋งจ ์•ž์˜ ๋ฐ์ดํ„ฐ ์š”์†Œ๋ฅผ ๋นผ๋ ค๋ฉด ์ผ๋‹จ ํ•ด๋‹น ์š”์†Œ๋ฅผ ์„ ํƒํ•  ์ค„ ์•Œ์•„์•ผ ํ•œ๋‹ค.

์ด ๋•Œ, ๋ฐ”๋กœ this.front ๊ฐ€ ์“ฐ์ด๊ฒŒ ๋œ๋‹ค.

this.storage[this.front]

์œ„์˜ ์ฝ”๋“œ๋Š” ์‚ฌ์‹ค์ƒ..

this.storage[0]

storage ๊ฐ์ฒด์˜ 0๋ฒˆ ๋ฐ์ดํ„ฐ ๋ผ๋Š” ๋ง๊ณผ ๋™์˜์–ด ์ด๋‹ค.

์ด์ œ ์ด ๊ฒƒ์„ ๋ณ€์ˆ˜์— ๋”ฐ๋กœ ๋‹ด๋Š”๋‹ค.

let omitValue = this.storage[this.front]

๐ŸŒณ๊ณผ์ • 4-3. ๋งจ ์•ž ์š”์†Œ ์ง€์›Œ๋ฒ„๋ฆฌ๊ธฐ

delete this.storage[this.front]

์˜ค ์ง€์› ๋‹ค. ๊ทผ๋ฐ ์ด์ œ๋ถ€ํ„ฐ๊ฐ€ ์ค‘์š”ํ•ด ์ง„๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณธ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด ์š”์†Œ๋ฅผ ์ธ์Šคํ„ด์Šค.enqueue() ๋ฅผ ํ†ตํ•ด ๋„ฃ์—ˆ๋‹ค.

enqueue

์ด์ œ ์•ž์˜ ์š”์†Œ๋ฅผ ์ง€์›Œ ๋ฒ„๋ฆฌ๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ?

let result = new Queue();

result.enqueue('๐ŸŒ');
{0: "๐ŸŒ"}
result.enqueue('๐Ÿ‘');
{0: "๐ŸŒ", 1: "๐Ÿ‘"}
result.enqueue('๐Ÿ’');
{0: "๐ŸŒ", 1: "๐Ÿ‘", 2: "๐Ÿ’"}
result.enqueue('๐Ÿ‹');
{0: "๐ŸŒ", 1: "๐Ÿ‘", 2: "๐Ÿ’", 3: "๐Ÿ‹"}
result.enqueue('๐Ÿฅ');
{0: "๐ŸŒ", 1: "๐Ÿ‘", 2: "๐Ÿ’", 3: "๐Ÿ‹", 4: "๐Ÿฅ"}
result.size()
5
result.dequeue();
"๐ŸŒ"

๋ฐ”๋‚˜๋‚˜๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉฐ ํ•˜๋‚˜๊ฐ€ ์—†์–ด์ง€๋Š” ๊ฒƒ ๊นŒ์ง„ ์ข‹์•˜๋Š”๋ฐ ๋‚ด๊ฐ€ ์›ํ•œ ๋ชจ์Šต์ด ์•„๋‹ˆ๋‹ค.

result.storage
{1: "๐Ÿ‘", 2: "๐Ÿ’", 3: "๐Ÿ‹", 4: "๐Ÿฅ"}
result.size();
4

{0: โ€๐Ÿ‘โ€, 1: โ€๐Ÿ’โ€, 2: โ€๐Ÿ‹โ€, 3: โ€๐Ÿฅโ€} ์ด ๋˜๋Š” ๋ชจ์Šต์„ ์›ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์œ„์˜ ์ƒํƒœ์—์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ dequeue() ๋ฅผ ํ•˜๋ฉด ์ด์ œ ๋”์ด์ƒ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค. ์™œ๋ƒ๋ฉด delete this.storage[this.front]; ๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด key ๊ฐ€ 0 ์ธ ๊ฒƒ์„ ์ง€์šฐ๋„๋ก ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์•„๋ž˜๋Š” ๋‹ค์‹œ dequeue() ๋ฅผ ํ–ˆ์„ ๋•Œ, ๋” ์ด์ƒ ์•ˆ๋˜๋Š” ๋ชจ์Šต์ด๋‹ค.

result.dequeue();
undefined // ๋บ€ ๊ฒŒ ์—†์œผ๋‹ˆ undefined ๋ฅผ ๋‚ด๋ฑ‰๋Š”๋‹ค.
result.storage
{1: "๐Ÿ‘", 2: "๐Ÿ’", 3: "๐Ÿ‹", 4: "๐Ÿฅ"}

๊ทธ๋Œ€๋กœ์ด๋‹ค ใ… ใ…  ์–ด๋–ป๊ฒŒ ํ•˜์ง€..?

์ด์ œ dequeue() ๋ฅผ ๋‘๋ฒˆ์งธ๋กœ ํ–ˆ์„ ๋•Œ, 0๋ฒˆ์งธ์˜ value๊ฐ’์ด undefined ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ์ƒํ™ฉ์— ๋Œ€ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

๐ŸŽ†๊ณผ์ • 4-4. 0๋ฒˆ์งธ์˜ value๊ฐ’์ด undefined ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ์ƒํ™ฉ

์œ„์˜ ์„ค๋ช…์ฒ˜๋Ÿผ dequeue() ๋ฅผ ๋‘๋ฒˆ์งธ๋กœ ํ•œ ์ƒํ™ฉ์ด ๋˜์—ˆ์„ ๋•Œ๋ฅผ ์ •์˜ํ•ด ์ฃผ์—ˆ๋‹ค.

์ตœ์ดˆ {0: โ€๐ŸŒโ€, 1: โ€๐Ÿ‘โ€, 2: โ€๐Ÿ’โ€, 3: โ€๐Ÿ‹โ€, 4: โ€๐Ÿฅโ€} ์—์„œ ๋งจ ์•ž ๋ฐ”๋‚˜๋‚˜๊ฐ€ ํ•˜๋‚˜ ๋น ์กŒ์„ ๋•Œ,

storage ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋œปํ•˜๋Š” this.rear ๋Š” 4๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค.

if (this.storage[this.front] === undefined) {
  //
  this.storage[this.front] = this.storage[this.rear - 1]
}

enqueue2

์˜ค ์ด๋ ‡๊ฒŒ ํ•˜๊ณ  rear ๋ฅผ ํ•˜๋‚˜ ๋บ€ ๋’ค์— ์ œ๊ฑฐํ•œ ์š”์†Œ๋ฅผ ๋ฆฌํ„ดํ•˜๊ฒŒ ํ•˜๊ณ  ํ…Œ์ŠคํŠธ๋ฅผ ๋Œ๋ ค๋ณด๋ฉด, ํ†ต๊ณผ๊ฐ€ ๋œ๋‹ค!

๊ทผ๋ฐ ์ •๋ง ๋์ผ๊นŒ? ์•„๋‹ˆ๋‹ค.

๐ŸŽ†๊ณผ์ • 4-5. ๊ฐฏ์ˆ˜๋„ ํ•˜๋‚˜ ์ค„๊ณ  ์‚ฌ์ด์ฆˆ๋„ ํ•˜๋‚˜ ์ค„์—ˆ๋Š”๋ฐ ๋Œ€์ฒด ์ €๋์— ์ €๊ฑด ๋ญ์ง€?

๋งจ์•ž์— ์žˆ์—ˆ๋˜ ๋ฐ”๋‚˜๋‚˜๋ฅผ ์—†์•ด๋‹ค. ์˜ค ์›ํ•˜๋˜ ๊ทธ๋ฆผ์ด ๋‚˜์™”๋„ค? ํ†ต๊ณผ๋„ ๋˜๊ณ ? ํ•˜๋‚˜ ๋บ์œผ๋‹ˆ๊นŒ rear ๋„ 4 ๊ณ  result.size() ๋„ 4์ธ๋ฐ ์•„๋‹ˆ ๊ทผ๋ฐ ์ € storage ๋งจ ๋์— ํ‚ค์œ„๋Š” ๋ญ”๋ฐ..?

์•„๋งˆ ์—ฌ๊ธฐ์—์„œ ๋‚ด๊ฐ€ ํ—ฌํ”„ ๋ฐ์Šคํฌ์— ์งˆ๋ฌธ์„ ์˜ฌ๋ ค์•ผ ํ•  ๊ฒƒ ๊ฐ™์€๋ฐ,

๋งˆ์ง€๋ง‰์œผ๋กœ ๋„ฃ์€ ํ‚ค์œ„๊ฐ€ ๋งจ ์•ž์œผ๋กœ ์˜จ๊ฑฐ๋Š” ๋˜ ๋ญ๊ณ  ๋งจ ๋งˆ์ง€๋ง‰ ์—๋„ ํ‚ค์œ„๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ๋‹ค๋Š”๊ฒŒ ์ด์ƒํ•˜๋‹ค.

๋”์šฑ ์—ฝ๊ธฐ์ ์ธ ์‚ฌ์‹ค์€ ์ด๊ฒŒ npm run test ๊ฐ€ ํ†ต๊ณผ๊ฐ€ ๋œ๋‹ค๋Š” ์‚ฌ์‹ค์ด๋‹ค.

ํ—ฌํ”„ ๋ฐ์Šคํฌ ๊ฐ์ด๋‹ค.

result.storage
{0: "๐Ÿฅ", 1: "๐Ÿ‘", 2: "๐Ÿ’", 3: "๐Ÿ‹", 4: "๐Ÿฅ"}
result.rear
4
result.size();
4

๊ทธ๋ž˜์„œ ํŽ˜์–ด๋‹˜๊ณผ ๋‚˜๋Š” ์ € ๋งˆ์ง€๋ง‰ 4:โ€๐Ÿฅโ€ ๋ฅผ undefined ๋กœ ๋งŒ๋“ค ์ƒ๊ฐ์„ ํ–ˆ๋‹ค.

์œ„์— ๋งŒ๋“  if ๋ฌธ ์•ˆ์— ํ•˜๋‚˜๋ฅผ ๋” ์ถ”๊ฐ€ํ–ˆ๋‹ค.

if (this.storage[this.front] === undefined) {
  this.storage[this.front] = this.storage[this.rear - 1] // this.storage = { 0: element2 , ...}
  // rear = 1์ธ ์ƒํƒœ
  this.storage[this.rear - 1] = undefined
  // { 0: element2, 1: undefined }
}

this.storage[this.rear - 1] = undefined;

์ด ์ฝ”๋“œ์˜ ์˜๋ฏธ๋Š”,

{0: โ€๐ŸŒโ€, 1: โ€๐Ÿ‘โ€, 2: โ€๐Ÿ’โ€, 3: โ€๐Ÿ‹โ€, 4: โ€๐Ÿฅโ€} ์ด ์ƒํƒœ์—์„œ..

ํ˜„์žฌ rear ๋Š” 5์ด๋‹ค. ๊ทธ๋ž˜์„œ ๋ฐ”๋‚˜๋‚˜๋ฅผ ๋บŒ๊ณผ ๋™์‹œ์—

this.storage[5 - 1] = undefined;

this.storage[4] = undefined;

์ฆ‰ storage ์˜ key ๊ฐ€ 4์ธ ํ‚ค์œ„๋ฅผ undefined ๋กœ ๋งŒ๋“ ๋‹ค๋Š” ์ ์ธ๋ฐ..

dequeue() ์„ ํ†ตํ•ด์„œ.. ๋งจ์•ž ๋ฐ”๋‚˜๋‚˜๊ฐ€ ์ง€์›Œ์ง€๋Š” ๊ฒƒ์€ ์ข‹์•˜๋Š”๋ฐ..

result.storage
{0: "๐Ÿฅ", 1: "๐Ÿ‘", 2: "๐Ÿ’", 3: "๐Ÿ‹", 4: undefined}

์™œ ๋‚˜์ค‘์— ๋„ฃ์€ ํ‚ค์œ„๊ฐ€ ๋งจ์•ž์— ์˜ค๊ฒŒ ๋œ ๊ฒƒ์ผ๊นŒโ€ฆ

์ด๋Ÿฌ๋ฉด ์„ ์ž… ์„ ์ถœ์ด ์•„๋‹ˆ์ž๋‚˜โ€ฆ

ํ•œ๋ฒˆ ๋” dequeue() ๋ฅผ ํ•˜๋‹ˆ..

result.dequeue()
"๐Ÿฅ"
result.storage
{0: "๐Ÿ‹", 1: "๐Ÿ‘", 2: "๐Ÿ’", 3: undefined, 4: undefined}

์•„ ๋ญ์ง€.. ์„ ์ž… ์„ ์ถœ์ด ์•„๋‹Œ๋ฐ.. ๋ญ์ง€ ใ… ใ… 

ํ†ต๊ณผ๊ฐ€ ๋˜์—ˆ๋‹ค๋Š” ์ ์ด ์•„์ด๋Ÿฌ๋‹ˆ ์ด๋‹ค.. ์ผ๋‹จ ์ตœ์ข… ์ฝ”๋“œ๋ฅผ ๋‚จ๊ฒจ ๋ณธ๋‹ค.

๐ŸŽ†๊ณผ์ • 5. ์ตœ์ข… ์ฝ”๋“œ

class Queue {
  constructor() {
    this.storage = {} // ๊ฒฐ๊ณผ๊ฐ’
    this.front = 0 // ๋งจ ์•ž์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•  ๋•Œ ์“ธ ์ธ๋ฑ์Šค (์˜ˆ: ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ unshift) --> ๊ณ ์ •๋œ ๊ฐ’
    this.rear = 0 // ๊ฐœ์ˆ˜, ์ธ๋ฑ์Šค(๊ฐœ์ˆ˜-1) --> ์ปจํŠธ๋กค๋Ÿฌ
  }
  size() {
    return this.rear
  }
  enqueue(element) {
    this.storage[this.rear] = element // this.storage = { 0: element1, 1: element2, 2: element3... }
    this.rear++
    return this.storage
  }
  dequeue() {
    if (this.rear === 0) {
      return this.storage
    }
    let minusValue = this.storage[this.front] // element1
    delete this.storage[this.front]
    // ๊ฒฐ๊ณผ๊ฐ’: { 0: undefined, 1: element2, 2: element3 }
    // { 0: element2, 1: element2 }
    // ๊ธฐ๋Œ€ํ•  ๊ฐ’: { 0: element2, 1: element3 }
    // 0๋ฒˆ์งธ์˜ value๊ฐ’์ด undefined๋ผ๋ฉด ์•„๋ž˜์˜ ์ž‘์—…์„ ํ•ด์ค€๋‹ค.
    if (this.storage[this.front] === undefined) {
      this.storage[this.front] = this.storage[this.rear - 1] // this.storage = { 0: element2 , ...}
      // rear = 1์ธ ์ƒํƒœ
      this.storage[this.rear - 1] = undefined
      // { 0: element2, 1: undefined }
    }
    this.rear--
    return minusValue
  }
}
module.exports = Queue

์œผ์•„์•„์•…!!! ใ… ใ… ใ… ใ… 


Written by@[DotoriMook]
ํ”„๋ก ํŠธ์—”๋“œ ์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž ๋„ํ† ๋ฆฌ๋ฌต์˜ ๊ธฐ์ˆ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค.

GitHubMediumTwitterFacebook