0

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);
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Explain to us what `node.Next = _tail; _tail = node.Next;` is trying to do. I ask because what I think you _should_ be doing is making the `Next` of _a different node_ (specifically the last one) point to your new node. **But you aren't doing that.** – mjwills Jun 21 '21 at 04:44
  • I was a little confused on that one myself, in the instructions it states that "you’ll need to connect the tail to the new node before making the new node be the tail". the tail is supposed to represent that last node in the stack. – SJ_224224 Jun 21 '21 at 04:59
  • Which isn't what you are doing. Get the tail - set its Next to your node. Then set the Next of your node to null. Then set the tail to your node. That is what I suspect you should do. – mjwills Jun 21 '21 at 05:02

0 Answers0