Skip to content

Instantly share code, notes, and snippets.

@ceme
Created December 1, 2018 21:09
Show Gist options
  • Save ceme/8a987956813bec21f741593f3963cba5 to your computer and use it in GitHub Desktop.
Save ceme/8a987956813bec21f741593f3963cba5 to your computer and use it in GitHub Desktop.
Stack and Queue Operations and Write to DOM in Vanilla JavaScript
https://codepen.io/anon/pen/dQaJBm?editors=1111
<div id="out"></div>
var out = document.querySelector('#out');
var addBreak = function() {
var brDiv = document.createElement('br');
out.appendChild(brDiv);
};
var addText = function(text) {
var textNode = document.createTextNode(text.toString());
out.appendChild(textNode);
};
//Queue
var queue = [];
queue.unshift('boh');
queue.unshift('bog');
queue.unshift('bogo');
queue.unshift('begone');
var qres = queue.shift();
addText(queue + ' : ' + qres);
//End Queue
addBreak();
//Stack
var stack = [];
stack.push('one');
stack.push('two');
stack.push('three');
stack.push('four');
var sres = stack.pop();
addText(stack + ' : ' + sres);
//End Stack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment