tags:

views:

121

answers:

6

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");
        }

 public void Push(int g)
    {
        if (top == Max)
        {
            throw new Exception("Stack overflow...");
        }
        else
        {
            tabel[top] = g;
            top++;
        }
    }/*Push*/

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!

Edit: Working push method:

        public int Pop()
    {
        if(top <= 0)
        {
            throw new Exception("Stack underflow...");
        }

        top--;
        tabel[top] = 0;
        number = tabel[top];

        return number;
    }/*Pop*/
+1  A: 

You should declare a local variable result and set it to the current value of the top of the stack, using top as an index into your data. Then decrement your top variable and return result. Since it is homework I won't post the code - you should try to implement it yourself to make sure that you understand it.

There is no need to loop to implement Pop. There is also no need to zero values as you remove them, although you can do this if you wish.

You should also note that this implementation is not thread-safe. That is OK, but make sure that this is clearly documented.

Mark Byers
A: 

You should not be using a for loop in Pop(). You are only removing one element, and you know the location of that element (it's at index top), so you need only deal with that element of the stack.

Thomas
A: 

Assuming that top references the top of the stack, setting the value at the top of your stack to 0 and decrementing the top should be sufficient.

public int Pop()
{
    if(top < 0)
    {
        throw new Exception("Stack underflow...");
    }
    else
    {
        table[top] = 0;
        top--;
    }

    // not sure where number comes from
    return number; 

}/*Pop*/
Mr Roys
Hello, Yes the top shows how many items there are currently in the stack (top 5 for a full stack of 5 elements).Although every time i try anything (also with the code above) i get out of index errors. Cheers.
Sef
@Sef, you probably have to post your complete code then.Your top might be pointing out of the array. Assuming that your stack is an array of 5 integers, top is valid if it's 0 (first element) to 4 (last element). Anything otherwise will throw out of bounds exceptions.
Mr Roys
+8  A: 

To implement a stack, you need an array and a "pointer" to the top of the stack.

empty       _ _ _ _ _
            ↑

In your code, table is the array and top is the pointer (as array index).

Push

To push an item to a stack, put the item at top of the stack and advance the pointer by one:

push 1      1 _ _ _ _
              ↑

push 2      1 2 _ _ _
                ↑

push 3      1 2 3 _ _
                  ↑

That's what your code already does:

table[top] = g;    // insert `g` at `top` into `table`
top++;             // advance `top` by one

Pop

To pop an item, move the pointer back by one and return+erase the item at the top:

pop 3       1 2 _ _ _
                ↑

pop 2       1 _ _ _ _
              ↑

pop 1       _ _ _ _ _
            ↑

Now try to translate your solution for Push to do the reverse as shown here!

dtb
+1 for excellent usage of the `↑` character.
Mark Byers
Cheers for the well drawed illustration.The thing is i fail to adress because my Push is a void method. Where my pop has to be a int with a single return type. The push value uses a specifiek number to push on it, but how exactly do i call upon this value(index)? table[top] = // everything results in out of index.
Sef
@Sef: Please focus on this: "Move the pointer back by one and return the item at the top". Can you try to implement this and post your solution?
dtb
+1 excellent example!
used2could
Cheers for all the input!. Placed the code in the op at the bottom.My problem was i was placing the top-- in the wrong order, resulting in out of index errors all the time.Perhaps another small question. I had to use a stringbuider to be able to show me the desired results (else i get the class name and thats it). Is there any other way exept for the stringbuilder to acheive this? Have not really seen this, and perhaps i should not have used this already. Anyway thanks a bunch, very much apperciated!
Sef
@Sef: I'm not going to answer your second question here because it is not directly related to this question, and also comments are not appropriate or convenient for answering questions for a number of reasons (voting, formatting, character limits, etc, etc). You are welcome to create a new question though.
Mark Byers
Ok my bad. cheers
Sef
A: 

I'm sure i'm going to get flamed for giving him the answer but i know what i would want if i were in his shoes on a Sunday. I hope dtb's answer gets the most upvotes because it deserves it for an excellent explanation of a stack!

Here is a basic example of a stack. Please learn it. don't just paste it in your homework.

public class Stack<T>
{
    public int Count { get; private set; }
    private int _CurrentPosition;
    private T[] _Values;

    public Stack(int capacity)
    {
        _CurrentPosition = -1;
        _Values = new T[capacity];
        Count = capacity;
    }

    public T Peek()
    {
        if (_CurrentPosition < 0)
            return default(T);

        return _Values[_CurrentPosition];
    }

    public void Push(T item)
    {
        if (_CurrentPosition == Count)
            throw new Exception("Stack overflow...");

        _CurrentPosition++;
        _Values[_CurrentPosition] = item;
    }

    public T Pop()
    {
        if(_CurrentPosition < 0)
            throw new Exception("Stack underflow...");
        T item = _Values[_CurrentPosition];
        _Values[_CurrentPosition] = default(T);
        _CurrentPosition--;
        return item;
    }
}
used2could
<generic flame for posting full solution to homework question>. Please remove it.
dtb
We have to be sensitive to homework etiquette? God forbid he goes to another site that has code examples. LOL I Love the "<generic flame...>"
used2could
A: 
    public int Pop()
    {
        if(top <= 0)
        {
            throw new Exception("Stack underflow...");
        }

        top--;
        tabel[top] = 0;
        number = tabel[top];

        return number;
    }/*Pop*/
Sef