Below are instructions and partial code. C# is the programing language. Just wri
ID: 3573678 • Letter: B
Question
Below are instructions and partial code. C# is the programing language. Just write code for the area's that say Fill In Code
1. (30 points) On the next page is a part of the definition of a generic Stack class. It stores its elements in the _elements array, arranged so that the bottom element is at location 0. Its _count field stores the number of elements in the stack. Included within this class definition is a part of the definition of the nested class StackEnumerator, whose instances are to function as enumerators for Stack objects. Specifically, they should step through the elements of the stack from top to bottom. Fill in the StackEnumerator constructor to initialize both private fields using the given parameter (_stack will be the stack being enumerated, and _current will be the index of the current element), and fill in the Current property and MoveNext method to provide the required functionality. You may assume that both classes contain the members required to implement the interfaces listed in their class statements, and you may assume that the Stack class has members that may change the values of its private fields; however, you may not use any of these members. You may also assume that the parameter to the StackEnumerator constructor is non-null. You may not add any code outside of the StackEnumerator constructor, its Current property, or its MoveNext method.
public partial class Stack : System.Collections.Generic.IEnumerable<T>
{
private T[] _elements = new T[10];
private int _count = 0;
public partial class StackEnumerator :
System.Collections.Generic.IEnumerator<T>
{
private Stack<T> _stack;
private int _current;
public StackEnumerator(Stack<T> s)
{ Fill In Code }
public T Current
{
get
{ Fill In Code }
}
public bool MoveNext()
{ Fill in Code }
}
}
Explanation / Answer
public partial class Stack : System.Collections.Generic.IEnumerable<T>
{
private T[] _elements = new T[10];
private int _count = 0;
public partial class StackEnumerator :
System.Collections.Generic.IEnumerator<T>
{
private Stack<T> _stack;
private int _current;
public StackEnumerator(Stack<T> s)
{
_stack = s;
_current = -1;
}
public T Current
{
get
{
return _stack._elements[_current];
}
}
public bool MoveNext()
{
_current++;
return _current >= 0 && _current < _stack._count;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.