Skip to content

Instantly share code, notes, and snippets.

@vikingmute
Created September 17, 2014 09:54
Show Gist options
  • Save vikingmute/ecdef7e26200bab3de6d to your computer and use it in GitHub Desktop.
Save vikingmute/ecdef7e26200bab3de6d to your computer and use it in GitHub Desktop.
<!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>
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());
@vikingmute
Copy link
Author

stack in javascript,
and solve the problem in book 52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment