I am implementing a stack in JavaScript.
Consider:
Stack{0,1,2,3} Top:0
Now the user pops the value of 2:
Stack{0,1,3} Top:0
Is this an acceptable behavior for a stack?
I am rolling my own stack, but is there any built in code that would do this for me?
My Code:
function Stack() //Creating Stack Object
{
// Create an empty array of cards.
this.cards = new Array(); //cards array inside stack object
this.push = pushdata; //Call pushdata function on push operation
this.pop = popdata; //Call popdata function on pop operation
this.printStack = showStackData; //Call showStackData function on printstack operation
}
function pushdata(data)
{
this.cards.push(data);
}
function popdata(data)
{
return this.cards.pop();
}
function showStackData()
{
return this.cards;
}
var a = new Stack(); //Create stack Object
a.push(12); //Push Data onto Stack
a.push(32);
a.push(42);
var z = a.pop();
document.write("Data Popped: " + z);
document.write("Stack Output: " + a.printStack());
If a stack is not the correct type of data structure for this application, what would be the correct one?