Popping an item from the stack, each method call
I have a question considering a program that stimulates the stack (not using built-in stack functions or such).
stack2= 1 2 3 4 5 //single dimension array of 5 elements
When calling the method pop
, the stack should look like this, basically popping an item every time the stack is called 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
Here is my code:
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*/
I already have some code that populates my array with values ββ(by push method). The pop method should take the last value and push it to 0, then calls the next stack and push the next one to 0 (as shown above on stack2).
The current pop method that keeps track of the superscript (0 items = 0 top, 1 item = 1 top, etc.) already includes a low level warning if it 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 another class I already have a loop (the loop for
shown above) that simulates the s2 stack 6 times. (first stack: 1 2 3 4 0, second stack 1 2 3 0 0, etc.) How exactly do I grab an element each time? Either I have the whole display at 0 or 0 in the wrong places / from index errors.
Edit: Pop working method:
public int Pop()
{
if(top <= 0)
{
throw new Exception("Stack underflow...");
}
top--;
tabel[top] = 0;
number = tabel[top];
return number;
}/*Pop*/
source to share
To implement a stack, you need an array and a "pointer" at the top of the stack.
empty _ _ _ _ _
β
Your code table
contains an array, and top
is a pointer (as an array index).
Click on
To push an item onto the stack, push the item at the top of the stack and move the pointer one by one:
push 1 1 _ _ _ _
β
push 2 1 2 _ _ _
β
push 3 1 2 3 _ _
β
What your code already does:
table[top] = g; // insert `g` at `top` into `table`
top++; // advance `top` by one
Pop
To place an element, move the pointer back one and return + erase the element at the top:
pop 3 1 2 _ _ _
β
pop 2 1 _ _ _ _
β
pop 1 _ _ _ _ _
β
Now try translating your Push solution to do the opposite, as shown here!
source to share
You must declare a local variable result
and set it to the current value of the top of the stack, using it top
as an index in your data. Then decrement your variable top
and return result
. Since this is homework, I won't post the code - you should try to implement it yourself to make sure you understand it.
No loop needed for implementation Pop
. There is also no need for null values ββwhen removing them, although you can do that if you like.
It should also be noted that this implementation is not thread safe. This is fine, but make sure it is clearly documented.
source to share
Assuming the top refers to the top of the stack, setting the value at the top of your stack to 0 and decreasing 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*/
source to share
I'm sure I'm going to cry for giving him the answer, but I know what I would like if I were in his shoes on Sunday. Hopefully dtb's answer gets the highest scores because it deserves it for a great explanation of the stack!
Here's a basic example of a stack. Please study this. don't just stick it into 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;
}
}
source to share