Created
September 17, 2014 09:54
-
-
Save vikingmute/ecdef7e26200bab3de6d to your computer and use it in GitHub Desktop.
// source http://jsbin.com/tehab/1
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
var Stack = function() { | |
this.dataSource = []; | |
this.length = 0; | |
} | |
Stack.prototype.push = function(item) { | |
this.dataSource.unshift(item); | |
this.length = this.dataSource.length; | |
} | |
Stack.prototype.pop = function() { | |
var i = this.dataSource.shift(); | |
this.length = this.dataSource.length; | |
return i; | |
} | |
Stack.prototype.peek = function() { | |
if (this.dataSource.length > 0) { | |
return this.dataSource[0]; | |
} else { | |
return null; | |
} | |
} | |
Stack.prototype.toString = function() { | |
var str = "top ... "; | |
for (var i = 0; i < this.dataSource.length; i++) { | |
str += ' ' + this.dataSource[i] + ' '; | |
} | |
return str; | |
} | |
/*var stack = new Stack(); | |
stack.push('a'); | |
stack.push('b'); | |
stack.push('c'); | |
console.log(stack.toString()); | |
console.log(stack.peek()); | |
console.log(stack.length); | |
stack.pop(); | |
stack.pop(); | |
console.log(stack.toString()); | |
console.log(stack.peek()); | |
console.log(stack.length);*/ | |
//Pez Memorabilia problem | |
var oldbox = new Stack(); | |
var newbox = new Stack(); | |
var candies = ['yellow', 'red', 'blue', 'yellow', 'red', 'red', 'green']; | |
for (var i = 0; i< candies.length; i++) { | |
oldbox.push(candies[i]); | |
} | |
console.log(oldbox.toString()); | |
while(oldbox.length > 0) { | |
var candy = oldbox.pop(); | |
if (candy != 'yellow') { | |
newbox.push(candy); | |
} | |
} | |
console.log(newbox.toString()); | |
while(newbox.length > 0) { | |
var candy = newbox.pop(); | |
oldbox.push(candy); | |
} | |
console.log(oldbox.toString()); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var Stack = function() { | |
this.dataSource = []; | |
this.length = 0; | |
} | |
Stack.prototype.push = function(item) { | |
this.dataSource.unshift(item); | |
this.length = this.dataSource.length; | |
} | |
Stack.prototype.pop = function() { | |
var i = this.dataSource.shift(); | |
this.length = this.dataSource.length; | |
return i; | |
} | |
Stack.prototype.peek = function() { | |
if (this.dataSource.length > 0) { | |
return this.dataSource[0]; | |
} else { | |
return null; | |
} | |
} | |
Stack.prototype.toString = function() { | |
var str = "top ... "; | |
for (var i = 0; i < this.dataSource.length; i++) { | |
str += ' ' + this.dataSource[i] + ' '; | |
} | |
return str; | |
} | |
/*var stack = new Stack(); | |
stack.push('a'); | |
stack.push('b'); | |
stack.push('c'); | |
console.log(stack.toString()); | |
console.log(stack.peek()); | |
console.log(stack.length); | |
stack.pop(); | |
stack.pop(); | |
console.log(stack.toString()); | |
console.log(stack.peek()); | |
console.log(stack.length);*/ | |
//Pez Memorabilia problem | |
var oldbox = new Stack(); | |
var newbox = new Stack(); | |
var candies = ['yellow', 'red', 'blue', 'yellow', 'red', 'red', 'green']; | |
for (var i = 0; i< candies.length; i++) { | |
oldbox.push(candies[i]); | |
} | |
console.log(oldbox.toString()); | |
while(oldbox.length > 0) { | |
var candy = oldbox.pop(); | |
if (candy != 'yellow') { | |
newbox.push(candy); | |
} | |
} | |
console.log(newbox.toString()); | |
while(newbox.length > 0) { | |
var candy = newbox.pop(); | |
oldbox.push(candy); | |
} | |
console.log(oldbox.toString());</script></body> | |
</html> |
This file contains 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
var Stack = function() { | |
this.dataSource = []; | |
this.length = 0; | |
} | |
Stack.prototype.push = function(item) { | |
this.dataSource.unshift(item); | |
this.length = this.dataSource.length; | |
} | |
Stack.prototype.pop = function() { | |
var i = this.dataSource.shift(); | |
this.length = this.dataSource.length; | |
return i; | |
} | |
Stack.prototype.peek = function() { | |
if (this.dataSource.length > 0) { | |
return this.dataSource[0]; | |
} else { | |
return null; | |
} | |
} | |
Stack.prototype.toString = function() { | |
var str = "top ... "; | |
for (var i = 0; i < this.dataSource.length; i++) { | |
str += ' ' + this.dataSource[i] + ' '; | |
} | |
return str; | |
} | |
/*var stack = new Stack(); | |
stack.push('a'); | |
stack.push('b'); | |
stack.push('c'); | |
console.log(stack.toString()); | |
console.log(stack.peek()); | |
console.log(stack.length); | |
stack.pop(); | |
stack.pop(); | |
console.log(stack.toString()); | |
console.log(stack.peek()); | |
console.log(stack.length);*/ | |
//Pez Memorabilia problem | |
var oldbox = new Stack(); | |
var newbox = new Stack(); | |
var candies = ['yellow', 'red', 'blue', 'yellow', 'red', 'red', 'green']; | |
for (var i = 0; i< candies.length; i++) { | |
oldbox.push(candies[i]); | |
} | |
console.log(oldbox.toString()); | |
while(oldbox.length > 0) { | |
var candy = oldbox.pop(); | |
if (candy != 'yellow') { | |
newbox.push(candy); | |
} | |
} | |
console.log(newbox.toString()); | |
while(newbox.length > 0) { | |
var candy = newbox.pop(); | |
oldbox.push(candy); | |
} | |
console.log(oldbox.toString()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
stack in javascript,
and solve the problem in book 52