C# Taking a element off each time (stack)

Posted by Sef on Stack Overflow See other posts from Stack Overflow or by Sef
Published on 2010-04-11T14:35:48Z Indexed on 2010/04/11 14:43 UTC
Read the original article Hit count: 423

Filed under:
|

Greetings,

Have a question considering a program that stimulates a stack.(not using any build in stack features or any such)

stack2= 1 2 3 4 5 //single dimension array of 5 elements

By calling up a method "pop" the stack should look like the following:
Basically taking a element off each time the stack is being "called" up again.

stack2= 1 2 3 4 0  
stack2= 1 2 3 0 0  
stack2= 1 2 0 0 0  
stack2= 1 0 0 0 0  
stack2= 0 0 0 0 0

-

        for (int i = 1; i <= 6; i++)
        {
            number= TryPop(s2);
            //use number

            ShowStack(s2, "s2");
        }

Basically I already have code that fills my array with values (trough a push method). The pop method should basically take the last value and place it on 0. Then calls up the next stack and place the following on 0 (like shown above in stack2).

The current pop method that keeps track of the top index (0 elements = 0 top, 1 element = 1 top etc..). Already includes a underflow warning if this goes on 0 or below (which is correct).

    public int Pop()
    {
        if(top <= 0)
        {
            throw new Exception("Stack underflow...");
        }
        else
        {
            for (int j = tabel.Length - 1; j >= 0; j--)  
            {
               //...Really not sure what to do here.
            }
        }

        return number; 

    }/*Pop*/

Since in the other class I already have a loop (for loop shown above) that simulates 6 times the s2 stack. (first stack: 1 2 3 4 0, second stack 1 2 3 0 0 and so on.) How exactly do I take a element off each time? Either I have the entire display on 0 or the 0 in the wrong places / out of index errors.

Thanks in advance!

© Stack Overflow or respective owner

Related posts about c#

Related posts about homework