Queue and Stack in javascript

Using basic array functions, you can treat an array as a queue (first in first out) or a stack (last in first out):

To act as a queue, use the shift function:

while (arr.length) {
  let val = arr.shift();
  console.log(val);
}

To act as a stack, use the pop function:

while (arr.length) {
  let val = arr.pop();
  console.log(val);
}

Comments

Popular posts from this blog

C# Record Serialization

Versioned content in MVC

Add timestamp to photo using ImageMagick