问题描述
A list of instructions,and the code I have so far.
到目前为止,我遇到的问题是我在 C# 中的 Enqueue
方法或 Dequeue
方法运行不正确,并且它没有通过为分配创建的 UnitTest。
单元测试应该能够采用 Enqueue
方法和 Dequeue
方法并将一个 5 个字符的数组一次一个放入堆栈。
[]{10,20,30,40,50}
。然后以相同的顺序(FIFO)取出它们。
我写的代码无法通过单元测试,报错说:
Assert.AreEqual 失败。预期.实际
public int Count { get; private set; }
private Node<T> _head = null;
private Node<T> _tail = null;
public void Enqueue(T data)
{
Node<T> node = new Node<T>();
node.Data = data;
if (Count == 0)
{
_tail = node;
_head = node;
_tail.Next = node;
}
else
{
node.Next = _tail;
_tail = node.Next;
}
}
我认为这段代码是我的问题,但我可能是错的。我一遍又一遍地尝试了这些变体,但都没有成功,并且因为我的逻辑可能不正确或缺少一些非常愚蠢的东西而辞职。
public T Dequeue()
{
Node<T> position = _head;
if (Count == 0)
{
throw new InvalidOperationException("You've taken it all...");
}
else
{
T temp = _head.Data;
_head = position.Next;
--Count;
if (_head == null)
{
_tail = null;
}
return temp;
}
}
public void Reverse()
{
Node<T> prevIoUs,current,next;
prevIoUs = null;
current = _head;
next = _head.Next;
while (current != null)
{
next = current.Next;
current.Next = prevIoUs;
prevIoUs = current;
current = next;
}
_head = prevIoUs;
_tail = next;
}
public void EnqueueDequeuetest()
{
int[] testValues = new int[5] { 10,50 };
PG2Queue<int> testQueue = new PG2Queue<int>();
foreach (var testValue in testValues)
{
testQueue.Enqueue(testValue);
}
for (int i = 0; i < testValues.Length; i++)
{
int itemPopped = testQueue.Dequeue();
Assert.AreEqual(testValues[i],itemPopped);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)