views:

724

answers:

3

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?

A: 

Not possible to do it with Pop (without popping other stuff along the way). You should look into providing another way for the user to find where the number 2 is located and another method for the user to extract that, such as using array splice. But by then this is no longer a stack.

http://www.w3schools.com/jsref/jsref_splice.asp

Boon
+6  A: 

Is it Legal doing the above operation in stack?

That's not a traditional stack operation. A data structure that allowed you to do that couldn't really be called a stack; it's more of a plain old list.

If stack will not permits removing the data in between top and start.what are the alter datastructure used in javascript for above logic?

Is there something wrong with a simple Array? It gives you the random-access item writing you want:

var a= [0, 1, 2, 3];
a.splice(1, 1);
// a==[0, 2, 3]

plus stack-style convenience methods for accessing both front-loaded and tail stacks (and, consequently, queues): push/pop/shift/unshift.

There's not much point making your own Stack() wrapper class when Array() already covers it IMO. Whilst a computer scientist might be interested in things like the algorithmic complexity of linked lists for stacks, in practice you aren't going to be able to improve on the optimised Array implementation built into modern JavaScript interpreters from higher-level code.

bobince
A: 

If you really must use stacks, you could pop multiple times, store the popped data in another stack temporarily by pushing, then when you're done, do the reverse to put the end of the stack back in place.

Something like this is commonly done by Undo/Redo operations in GUI applications -- they have a stack of Undo operations, and an opposing stack of Redo operations. Undo moves an action from stack A to B, and Redo moves an action from stack B to A. A new action pushes onto the Undo stack and wipes out the Redo stack entirely.

Another place this is used is in the Back/Forward lists in your browser.

Kevin Conner