0
\$\begingroup\$

I would like to ask if the implementation is ok, and what I can improve. Although I'm not sure if the Singleton is working properly.

class MyLinkedList<T>
{
    class Node<G>
    {
        public Node<G> next;
        public G Data;
        public Node(G data)
        {
            Data = data;
            next = null;
        }
    }

    private Node<T> head;
    private Node<T> tail;

    public T First => head.Data;
    public T Last => tail.Data;

    public MyLinkedList()
    {
        head = tail = null;
    }

    public void Add(T data)
    {
        Node<T> node = new Node<T>(data);

        if (head == null)
        {
            head = node;
            tail = node;
        }
        else
        {
            tail.next = node;
            tail = node;
        }

    }
    public void RemoveByIndex(int index)
    {
        int count = 0;
        Node<T> current = head;
        Node<T> previous = current;

        try
        {
            while (count <= index)
            {
                if (count == index)
                {
                    previous.next = current.next;
                    current = null;
                }

                else
                {
                    previous = current;
                    current = current.next;
                }
                count++;
            }
        }
        catch (NullReferenceException)
        {
            Console.WriteLine("Invalid index to remove");
        }

    }

    public override string ToString()
    {
        StringBuilder list = new StringBuilder();
        Node<T> index = head;
        while (index.next != null)
        {
            list.Append($"{index.Data} -> ");
            index = index.next;
        }
        list.Append(index.Data);

        return list.ToString();
    }
}

public class Singleton<T> where T : class, new()
{
    private Singleton() { }

    private static readonly Lazy<T> instance = new Lazy<T>(() => new T());
    public static T Instance { get { return instance.Value; } }
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$
  • Add does tail = node in both branches. Take it out:

          if (head == null) {
              head = node;
          } else {
              tail.next = node;
          }
          tail = node;
    
  • Depending on index, RemoveByIndex may remove head, or tail (or both). If it happens, head, or tail (or both) members must be adjusted.

  • In RemoveByIndex, a condition count == index is mostly false. It is true exactly once, and when it is true, the loop gets broken. Again, be explicit:

      public void RemoveByIndex(int index) {
          int count = 0;
          Node<T> current = head;
          Node<T> previous = current;
    
          try {
              while (count < index) {
                  previous = current;
                  current = current.next;
                  count++;
              }
              previous.next = current.next;
              current = null;
          } catch(....) {
              ....
          }
      }
    
  • Catching the NullReferenceException in RemoveByIndex is wrong. The caller is not informed that the removal failed. It does not know or care what RemoveByIndex did print. Let the caller handle the problem.

\$\endgroup\$
1
  • \$\begingroup\$ Ok thank you for feedback, I improved things as you said, but I encountered a problem, when i call RemoveByIndex(0) it does nothing. I don't really know why. \$\endgroup\$
    – John
    Jun 6, 2022 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.