Question
Add a RemoveAll method to OurStack class. It removes all occurrences of a value from the Stack. Use the generic data types Equals method. in
Add a RemoveAll method to OurStack class. It removes all occurrences of a value from the Stack. Use the generic data types Equals method. in C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace ConsoleApplication1 {
class OurStack
public Node Next { get; set; } public Node(T d = default(T), Node node = null) { Data = d; Next = node; } }
private Node top;
public OurStack() { top = null; }
public void Clear() { top = null; }
public bool IsEmpty() { return top == null; }
public void Push(T value) { top = new Node(value, top); }
public T Pop() { if (IsEmpty() == true) throw new ApplicationException("Error: can't pop an empty stack"); T removedData = top.Data; top = top.Next; return removedData; }
public T Peek() { if (IsEmpty() == true) throw new ApplicationException("Error: can't peek at an empty stack"); return top.Data; }
public int Count { get { int count = 0; Node pTmp = top; while (pTmp != null) { count++; pTmp = pTmp.Next; } return count; } }
public override string ToString() { if (IsEmpty() == true) return string.Empty;
StringBuilder returnString = new StringBuilder(); Node pTmp = top; while (pTmp != null) { if (returnString.Length > 0) returnString.Append(":"); returnString.Append(pTmp.Data); pTmp = pTmp.Next; } return returnString.ToString(); } }
class OurArrayStack
private T[] mArray; private int top = 0;
public OurArrayStack(int size = 10) { if (size <= 0) throw new ApplicationException("Stack size must be > 0"); else mArray = new T[size]; }
public void Clear() { top = 0; }
public bool IsEmpty() { return top == 0; }
public void Push(T value) { if (top < mArray.Length) mArray[top++] = value; }
public T Pop() { if (IsEmpty() == true) throw new ApplicationException("Error: can't pop an empty stack"); return mArray[--top]; }
public T Peek() { if (IsEmpty() == true) throw new ApplicationException("Error: can't peek at an empty stack"); return mArray[top - 1]; }
public int Count { get { return top; } }
public override string ToString() { if (IsEmpty() == true) return string.Empty;
StringBuilder returnString = new StringBuilder(); int pTmp = top; while (pTmp > 0) { if (pTmp < top) returnString.Append(":"); returnString.Append(mArray[--pTmp]); } return returnString.ToString(); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started