Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack Push and Pop

Tags:

stack

c#

I am trying to get my push and pop methods working but can't seem to be able to. With the Push method I think it is something to do with nextfree but I'm not to sure. Also with the Pop method I am not to sure how to do it, I will put the pseudo code I have for it below my actual code. Here is my code:

class Program
{

   private string[] Stack = new string[5];
   int nextFree = 3;

    public Program() 
    {

        Stack = new string[5];

        Stack[0] = "Greg";
        Stack[1] = "Matt";
        Stack[2] = "Jack";
        Stack[3] = "Fred";


    }

    static void Main(string[] args)
    {
        Program prog = new Program();
        do
        {
            prog.DisplayMenu();
        }
        while (true);
    }




    public void DisplayMenu()
    {
        Int32 userInput = 0;

        Console.WriteLine("Linear Stack");
        Console.WriteLine("1: Add to stack");
        Console.WriteLine("2: Delete from stack");
        userInput = Int32.Parse(Console.ReadLine());


        switch (userInput)
        {
            case 1:
                this.Push();
                break;

            case 2:
                this.Pop();
                break;
        }

    }


    public void Push()
    {


        if (nextFree == Stack.Length)
        {
            Console.WriteLine("Stackoverflow, to many elements for the stack");
            Console.ReadLine();
        }
        else
        {
            Console.WriteLine("Please enter a name to be added");
            string userInput = Console.ReadLine();

            nextFree++;
            Stack[nextFree] = userInput;

        }
        this.list();
    }


        public void Pop()
        {
            if (nextFree == -1)
            {
                Console.WriteLine("Stack is empty");
                Console.ReadLine();
            }
            else
            {

                nextFree--;
            }

            this.list();
        }

        public void list()
        {
            foreach (string s in Stack)
            {
                Console.Write(s + " ");
            }

            Console.WriteLine();
        }



  }
}

Pop pseudo code:

If Stack is empty
Then error
Else
Return Stack[TopOfStackPointer]
Decrement TopOfStackPointer
EndIF

UPDATE: The Push method works now with the nextFree being initiated with the value 3.

like image 997
user2852418 Avatar asked Jan 31 '26 07:01

user2852418


2 Answers

You need to instantiate the value of nextFree to 4 when you first start (since you already have 4 items in your stack).

When checking to see if the value of nextFree is out of bounds of not, you need to remember that array indexes are zero based (i.e. they start at 0). So your condition should be:

if(nextFree >= Stack.Length - 1)
like image 156
System Down Avatar answered Feb 01 '26 23:02

System Down


Your Pop method is almost complete, the last step you need is to actually remove the value from the array before decrementing the index, to "pop" it out. You can do this by setting the previous value to null.

    public void Pop()
    {
        if (nextFree == -1)
        {
            Console.WriteLine("Stack is empty");
            Console.ReadLine();
        }
        else
        {
            Stack[nextFree] = null;
            nextFree--;
        }

        this.list();
    }

You can also get the value just before to show what have been popped by adding

string value = Stack[nextFree];
Console.WriteLine("Just popped value: " + value);

before setting it to null

It is not necessary here to actually return it like in your pseudocode, since you're not using the value outside. If you need it, consider changing the code to

    public string Pop()
    {
        string value = string.Empty;

        if (nextFree == -1)
        {
            Console.WriteLine("Stack is empty");
            Console.ReadLine();
        }
        else
        {
            value = Stack[nextFree];
            Stack[nextFree] = null;
            nextFree--;
        }

        this.list();
        return value;
    }

Notice the return type also changed from void to string.

like image 22
Pierre-Luc Pineault Avatar answered Feb 01 '26 22:02

Pierre-Luc Pineault