Placing & deleting element(s) from a object (stack)

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-04-04T13:32:37Z Indexed on 2010/04/04 13:33 UTC
Read the original article Hit count: 322

Filed under:

Hello,

Initialising 2 stack objects:

Stack s1 = new Stack(), s2 = new Stack();

s1 = 0 0 0 0 0 0 0 0 0 0 (array of 10 elements wich is empty to start with) top:0

        const int Rows = 10;
    int[] Table = new int[Rows];

    public void TableStack(int[] Table)
    {
        for (int i=0; i < Table.Length; i++)
        {
        }
    }

My question is how exactly do i place a element on a stack (push) or take a element from the stack (pop) as the following:
Push:

s1.Push(5);   // s1 = 5 0 0 0 0 0 0 0 0 0   (top:1)
s1.Push(9);   // s1 = 5 9 0 0 0 0 0 0 0 0   (top:2)

Pop:

int number = s1.Pop();  // s1 = 5 0 0 0 0 0 0 0 0 0 0 (top:1) 9 got removed

Do i have to use get & set, and if so how exactly do i implent this with a array? Not really sure what to exactly use for this. Any hints highly appreciated.

The program uses the following driver to test the Stack class (wich cannot be changed or modified):

        public void ExecuteProgram()
    {
        Console.Title = "StackDemo";
        Stack s1 = new Stack(), s2 = new Stack();
        ShowStack(s1, "s1");
        ShowStack(s2, "s2");
        Console.WriteLine();
        int getal = TryPop(s1);
        ShowStack(s1, "s1");
        TryPush(s2, 17);
        ShowStack(s2, "s2");
        TryPush(s2, -8);
        ShowStack(s2, "s2");
        TryPush(s2, 59);
        ShowStack(s2, "s2");
        Console.WriteLine();
        for (int i = 1; i <= 3; i++)
        {
            TryPush(s1, 2 * i);
            ShowStack(s1, "s1");
        }
        Console.WriteLine();
        for (int i = 1; i <= 3; i++)
        {
            TryPush(s2, i * i);
            ShowStack(s2, "s2");
        }
        Console.WriteLine();
        for (int i = 1; i <= 6; i++)
        {
            getal = TryPop(s2);
            //use number

            ShowStack(s2, "s2");
        }
    }/*ExecuteProgram*/

Regards.

© Stack Overflow or respective owner

Related posts about c#