Skip to content

Instantly share code, notes, and snippets.

@Giagnus64
Last active February 12, 2020 16:43
Show Gist options
  • Select an option

  • Save Giagnus64/bb747da4d9168d66c3866a0683c78611 to your computer and use it in GitHub Desktop.

Select an option

Save Giagnus64/bb747da4d9168d66c3866a0683c78611 to your computer and use it in GitHub Desktop.
Array implementation of a stack using push/pop
//STACK = LAST IN FIRST OUT
const stack = [];
stack.push({"Willow Rosenberg": "A"});
console.log(stack);
//[{ "Willow Rosenberg": "A" }]
stack.push({"Xander Harris": "C"});
console.log(stack);
//[{ "Willow Rosenberg": "A" }, { "Xander Harris": "C" }]
stack.push({"Cordelia Chase": "B+"});
console.log(stack);
//[{ "Willow Rosenberg": "A" },{ "Xander Harris": "C" },{ "Cordelia Chase": "B+" }]
stack.push({"Buffy Summers": "B"});
console.log(stack);
//[{ "Willow Rosenberg": "A" },{ "Xander Harris": "C" },{ "Cordelia Chase": "B+" },{ "Buffy Summers": "B" }]
//REMOVAL
let firstOut = stack.pop();
// { "Buffy Summers": "B" }
console.log(stack);
//[{ "Willow Rosenberg": "A" },{ "Xander Harris": "C" },{ "Cordelia Chase": "B+" }]
firstOut = stack.pop();
//{"Cordelia Chase": "B+"}
console.log(stack);
//[{ "Willow Rosenberg": "A" }, { "Xander Harris": "C" }]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment