A list of instructions, and the code I have so far.
The issue I'm running into with this so far is that my Enqueue
method or my Dequeue
method in C# is not operating correctly, and it's not passing the UnitTest created for the assignment.
The Unit test should be able to take the Enqueue
method, and the Dequeue
method and put a 5 character array into the stack one at a time.
[]{10,20,30,40,50}
. Then take them out in that same order (FIFO).
The code I have written is not able to pass the unit test, and the error says:
Assert.AreEqual failed. Expected<20>.Actual<10>
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;
}
}
I think this block of code is my issue here, but I could be wrong. I've tried variations of these over and over with no success, and have resigned that my logic may be off or something very silly is missing.
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, 20, 30, 40, 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);
}
}