It should remove all occurrences of a value from the Stack. This is my code but it somehow has a problem with my while statement.
At some test cases, it highlights the while statement and throw me an unhandled exception "System.NullReferenceException"
public bool RemoveAll(T value)
{
if (Count == 0)
return false;
if (Count == 1)
{
if (mTop.Data.Equals(value))
{
mTop = mTop.Next;
return true;
}
}
else
{
OurListNode<T> pTmp = mTop;
while (pTmp.Next != null)
{
if (pTmp.Next.Data.Equals(value))
{
pTmp.Next = pTmp.Next.Next;
}
pTmp = pTmp.Next;
}
return true;
}
return false;
}