Skip to content

Instantly share code, notes, and snippets.

@Drugak
Last active January 10, 2023 12:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drugak/2b164d5969b153a9a267 to your computer and use it in GitHub Desktop.
Save Drugak/2b164d5969b153a9a267 to your computer and use it in GitHub Desktop.
An example of a real-world stack is a Pez dispenser. Imagine that your virtual Pezdispenser is filled with red, yellow, and white colors and you don’t like the yellowones. Write a program that uses a stack (and maybe more than one) to remove theyellow ones without changing the order of the other candies in the dispenser.
function Stack() {
this.dataStore = [];
this.top = 0;
this.clear = clear;
this.push = push;
this.pop = pop;
this.peek = peek;
this.length = length;
}
function push(element) {
this.dataStore[this.top++] = element;
}
function peek() {
return this.dataStore[this.top -1];
}
function pop() {
return this.dataStore[--this.top];
}
function clear() {
this.top = 0;
}
function length() {
return this.top;
}
Stack.prototype.temporaryArray = [];
Stack.prototype.temporaryTop = 0;
Stack.prototype.pushInTemporaryArray = function(element) {
this.temporaryArray[this.temporaryTop++] = element;
};
var s = new Stack();
function fullArray(element) {
element.forEach(function(item, i, arr){
s.push(item);
});
}
function removeAllYellow(oldColor,newColor) {
s.dataStore.forEach(function(item, i, arr) {
if (item == oldColor) {
s.pushInTemporaryArray(newColor);
} else {
s.pushInTemporaryArray(item);
}
});
s.dataStore = s.temporaryArray;
}
fullArray(["white","yellow","green","white","yellow","green","white","yellow","green"]);
removeAllYellow("yellow","red");
console.log(s.dataStore);
@diwakarsingh
Copy link

I am unable to understand when we have traversed array from starting in 'removeAllYellow' method then how it is a Stack.
As per my understanding stack must be read like last in first out fashion.

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