Skip to content

Instantly share code, notes, and snippets.

@joe-oli
Last active September 19, 2019 07:01
Show Gist options
  • Save joe-oli/65ef5e54bd3e25dc7f9fabc996dcabf8 to your computer and use it in GitHub Desktop.
Save joe-oli/65ef5e54bd3e25dc7f9fabc996dcabf8 to your computer and use it in GitHub Desktop.
javascript stack and queue
//-- stack -----------
let stackArr = [];
stackArr.push(2); // [2]
stackArr.push(5); // [2, 5]
let item = stackArr.pop(); // stackArr is now [2] //
console.log(item); // displays 5
//-- queue -----------
let queueArr = [];
queueArr.push(2); // [2]
queueArr.push(5); // [2, 5]
let itemB = queueArr.shift(); // queueArr is now [5]
console.log(itemB); // displays 2
/*
push: add one (or more) element to end of array
pop: remove from end of array
shift: remove first element from array
unshift: adds one (or more) element to the beginning of an array.
BUT NOTE:
using Array.shift() / .unshift() on a large array is not efficient, as other elements have to re-adjust position.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment