ZL4892
ZL4892

Reputation: 99

How to use a Stack's Pop method in another method

I am attempting to write a program for an assignment that Pops and adds the first 2 items in a Stack. The program has a Pop method, but I would like to know how to call the method within the Add method. This Add is supposed to Pop the top two items in a stack, get their sum, and Push that sum to the stack. In my code below I call the Pop method twice inside the Add method, but when I display the stack, the stack still has all of the original values. Is there something more/else I need to go to get the Pop method to work?

class StackEmptyException : ApplicationException
{
    public StackEmptyException(String message) : base(message)
    {
    }
}

class MathStack
{
    private int[] dataStack;
    private int size;        
    private int top = -1;

    public bool IsEmpty()
    {
        return top == -1;
    }
    public bool IsFull()
    {
        return top == size - 1;
    }
    public void Push(int i)
    {
        dataStack[++top] = i;
    }
    public int Pop()
    {
        if (IsEmpty())
            throw new StackEmptyException
                       ("Stack empty -- cannot pop");
        else
            return dataStack[top--];
    }
    public int Top()
    {
        if (IsEmpty())
            throw new StackEmptyException
                        ("Stack empty -- top undefined");
        else
            return dataStack[top];
    }

    public MathStack()
    {
        dataStack = new int[10];
    }

    public MathStack(int s)
    {
        size = 10;
        dataStack = new int[size];
    }

    public void LoadStack(int v)
    {
        dataStack[++top] = v;
    }

    public void Display()
    {
        int[] display = new int[dataStack.Length];
        for (int i = 0; i < dataStack.Length; i++)
        {
            display[i] = dataStack[i];
            Console.WriteLine("{0}", display[i]);
        }
    }

    public void Add()
    {
        int add1 = dataStack[0];
        int add2 = dataStack[1];
        Pop();
        Pop();
        int sum = add1 + add2;
        Console.WriteLine("Sum: {0}", sum);
    }
}

class Program
{
    static void Main(string[] args)
    {
        MathStack stack1 = new MathStack();
        stack1.Push(9);
        stack1.Push(8);
        stack1.Push(7);
        stack1.Push(6);
        stack1.Push(5);
        stack1.Push(4);
        stack1.Push(3);
        stack1.Push(2);
        stack1.Push(1);
        stack1.Push(0);

        stack1.Display();
        stack1.Add();
        stack1.Display();

        Console.ReadLine();
    }
}

Upvotes: 0

Views: 558

Answers (1)

Kevin Gosse
Kevin Gosse

Reputation: 39007

There are two things wrong with your code.

First, the Display method displays the whole array. Except that since you're not physically removing the items from the array, you need to stop at the index top:

public void Display()
{
    if (IsEmpty())
    {
        Console.WriteLine("Empty");
        return;
    }

    for (int i = 0; i <= top; i++)
    {
        Console.WriteLine(dataStack[i]);
    }
}

The second issue is your Add. From what I understand, you want to pop the last two items, sum them, and push the result. In your implementation, you are actually summing the first two items (not the last two). A better version would be:

public void Add()
{
    int add1 = Pop();
    int add2 = Pop();
    int sum = add1 + add2;
    Console.WriteLine("Sum: {0}", sum);
    Push(sum);
}

Notice how I do not directly access dataStack. If your API is correctly implemented, it should not be needed.

Upvotes: 1

Related Questions