title: C# Linked List enqueue and dequeue method syntax
body: <p>I have a project that requires us to use linked list stacks(<code>NodeStack</code>) and queues(<code>NodeQueue</code>). I have this sample code to work with:</p>
  
  <pre><code>class Node
  {
      object value;
      Node next;
  
      public void setValue(object o)
      {
          value = o;
      }
  
      public object getValue()
      {
          return value;
      }
  
      public void setNext(Node o)
      {
          next = o;
      }
  
      public Node getNext()
      {
          return next;
      }
  }
  
  class NodeStack
  {
      Node top;
      int count;
  
      public void Push(object o)
      {
          Node newTop = new Node();
          newTop.setValue(o);
          newTop.setNext(top);
  
          top = newTop;
          count = count + 1;
      }
  
      public object Pop()
      {
          object value = top.getValue();
          top = top.getNext();
          return value;
      }
  
      public object Peek()
      {
          return top.getValue();
      }
  
      public void Clear()
      {
          top = null;
          count = 0;
      }
  
      public int Count()
      {
          return count;
      }
  }
  </code></pre>
  
  <p>From this code I should be able to derive the <code>NodeStack</code> to a <code>NodeQueue</code>but I have trouble understanding the syntax for the <code>Enqueue</code> and <code>Dequeue</code> methods. When I run a simple enqueue and dequeue program I get a Null reference exception on the 1st line of the dequeue method. Really appreciate the help.</p>
  
  <p>Code I have so far:</p>
  
  <pre><code>class NodeQueue
  {
      Node tail;
      Node head;
      int count;
  
      public void Enqueue(object o)
      {
          if (head == null)
          {
              Node newHead = new Node();
              newHead.setValue(o);
              head = tail = newHead;
              newHead.setNext(tail);
          }
          else
          {
              Node newTail = new Node();
              newTail.setValue(o);
              newTail.setNext(tail);
              tail = newTail;
          }
          count++;
      }
  
      public object Dequeue()
      {
          object value = head.getValue();
          head = head.getNext();
          return value;
  
      }
  
      public void Clear()
      {
          head = null;
          tail = null;
          count = 0;
      }
  
      public int Count()
      {
          return count;
      }
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>The problem with the NullException has been handled but now I have a problem with dequeue. I am using this program to test the queue</p>
  
  <pre><code>NodeQueue nq = new NodeQueue();
          nq.Enqueue(1);
          nq.Enqueue(2);
          nq.Enqueue(3);
          nq.Enqueue(4);
          nq.Enqueue(5);
  
          Console.WriteLine(nq.Dequeue());
          Console.WriteLine(nq.Dequeue());
          Console.WriteLine(nq.Dequeue());
          Console.WriteLine(nq.Dequeue());
          Console.WriteLine(nq.Dequeue());
  
          Console.ReadLine();
  </code></pre>
  
  <p>The expected output would be 1..5. The output that happens though is that 1 keeps getting printed.</p>