Created
April 1, 2015 05:18
-
-
Save linkkingjay/0d3402f73f181c1e4e0a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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