Skip to content

Instantly share code, notes, and snippets.

@linkkingjay
Created April 1, 2015 05:18
Show Gist options
  • Save linkkingjay/0d3402f73f181c1e4e0a to your computer and use it in GitHub Desktop.
Save linkkingjay/0d3402f73f181c1e4e0a to your computer and use it in GitHub Desktop.
function stackseq(input, s, output) {
if (input.length === 0 && s.length === 0) {
console.log(output);
} else {
if (input.length != 0) {
s.push(input.pop());
stackseq(input, s, output);
input.push(s.pop());
}
if (s.length != 0) {
output.push(s.pop());
stackseq(input, s, output);
s.push(output.pop());
}
}
}
function stack(n) {
var input = [],
s = [],
output = [];
for (var i = 1; i <= n; i++) {
input.push(i);
}
stackseq(input, s, output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment