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 class OurListNode { public T Data { get; set; } public OurListNode Next { get; set; } public OurListNode(T d = default(T), OurListNode node = null) { Data = d; Next = node; } }

private OurListNode mTop;

public OurStack() { mTop = null; }

public void Clear() { mTop = null; }

public bool IsEmpty() { return mTop == null; }

public void Push(T value) { mTop = new OurListNode(value, mTop); }

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 pTmp = mTop; while (pTmp != null) { count++; pTmp = pTmp.Next; } return count; } }

public override string ToString() { if (IsEmpty() == true) return string.Empty;

StringBuilder returnString = new StringBuilder(); OurListNode pTmp = mTop; while (pTmp != null) { if (returnString.Length > 0) returnString.Append(":"); returnString.Append(pTmp.Data); pTmp = pTmp.Next; } return returnString.ToString(); } }

class OurArrayStack //: IEnumerable, ICollection { private T[] mArray; private int mTop = 0; // points to next empty slot

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!