Question: in c#: Add a PopSecond method to the OurStack class that pops the second item on a stack and returns it. Leave the first item
in c#:
Add a PopSecond method to the OurStack class that pops the second item on a stack and returns it. Leave the first item on the stack.
OurStack Class:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
class OurStack
private OurListNode
public OurStack() { mTop = null; }
public void Clear() { mTop = null; }
public bool IsEmpty() { return mTop == null; }
public void Push(T value) { mTop = new OurListNode
public T Pop() { if (IsEmpty() == true) throw new ApplicationException("Error: can't pop an empty stack"); T removedData = mTop.Data; mTop = mTop.Next; return removedData; }
public T Peek() { if (IsEmpty() == true) throw new ApplicationException("Error: can't peek at an empty stack"); return mTop.Data; } public int Count { get { int count = 0; OurListNode
public override string ToString() { if (IsEmpty() == true) return string.Empty;
StringBuilder returnString = new StringBuilder(); OurListNode
class OurArrayStack
public OurArrayStack(int size = 10) { if (size <= 0) throw new ApplicationException("Stack size must be > 0"); else mArray = new T[size]; }
public void Clear() { mTop = 0; }
public bool IsEmpty() { return mTop == 0; }
public void Push(T value) { if (mTop < mArray.Length) mArray[mTop++] = value; //mTop++; }
public T Pop() { if (IsEmpty() == true) throw new ApplicationException("Error: can't pop an empty stack"); return mArray[--mTop]; }
public T Peek() { if (IsEmpty() == true) throw new ApplicationException("Error: can't peek at an empty stack"); return mArray[mTop-1]; }
public int Count { get { return mTop; } }
public override string ToString() { if (IsEmpty() == true) return string.Empty;
StringBuilder returnString = new StringBuilder(); int pTmp = mTop; while (pTmp > 0) { if (pTmp < mTop) returnString.Append(":"); returnString.Append(mArray[--pTmp]); } return returnString.ToString(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
