very simple linked list - stackoverflow errror

Hi,

I am trying to build very simple Linked List.

Here is my whole code.
namespace LinkedList
{
    public class Node {
       public Node next;
       public int data;
        public Node(int i)
        {
            data = i;
            next = null;
        }        
    }
    public class MyList {
        public Node head;
        public MyList()
        {
            head = null;
        }
        public void AddToEnd(int data)
        {
            Node toAdd = new Node(data);
            Node current = head;
            if (current == null)
                head = toAdd;
            else
            {
                current = current.next;
                AddToEnd(data);
            }
        }
        public void AddToBeginning(int data)
        {
            if (head == null)
                head = new Node(data);
            else {
                Node toAdd = new Node(data);
                toAdd.next = head;
                head = toAdd;
            }
              
        }
        public void Print()
        {
            Node current = head;
            while (current != null)
            {
                Console.Write(current.data + "-->");
                current = current.next;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyList list = new MyList();
            list.AddToEnd(9);
            list.AddToEnd(5);
            list.AddToEnd(7);
            list.AddToEnd(11);
            list.Print();
            Console.Read();
        }
    }
}

Open in new window


when I ran this code, i got the following error:
System.StackOverflowException was unhandled
And the exception was pointing to the Node constructor inside Node class.

Why am I getting this error, and how can i fix it?

Thanks~
IzzyTwinklyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
You're calling AddToEnd recursively, but your exit condition isn't very well defined. And honestly, I think a recursive approach isn't warranted here--though you could probably make it work. I would suggest using a loop instead:

public void AddToEnd(int data)
{
    Node toAdd = new Node(data);
    Node current = head;

    if (current == null)
        head = toAdd;
    else
    {
        do
        {
            current = current.next;
        }
        while (current.next != null);

        current.next = toAdd;
    }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IzzyTwinklyAuthor Commented:
Perfect! Thank you~~
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You're keeping a reference to the "head", why not keep a reference to the "tail" as well (it doesn't have to publicly exposed for use outside the class)?  Then you wouldn't have to traverse the whole list to add something to the end...
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

IzzyTwinklyAuthor Commented:
Hi Mike,

I am a beginner, but I understand what you meant.
So I should have something like,
public class MyList {
        public Node head;
        public Node tail;
        public MyList()
        {
            head = null;
            tail = null;
        }
        public void AddToEnd(int data)
        {

right? Then how would you implement AddToEnd? can you please show me?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
It could look like:
        public class MyList
        {

            public Node head = null;
            private Node tail = null;

            public void AddToEnd(int data)
            {
                Node toAdd = new Node(data);

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

            public void AddToBeginning(int data)
            {
                Node toAdd = new Node(data);

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

            public void Print()
            {
                Node current = head;
                if (current != null)
                {
                    Console.Write(current.data);
                    current = current.next;
                }
                while (current != null)
                {
                    Console.Write("-->" + current.data);
                    current = current.next;
                }
            }

        }

Open in new window

IzzyTwinklyAuthor Commented:
Thanks Mike!

You way is much more efficient. Now I am able to write all other methods(RemoverFirst(), RemoverLast(), Reverse(), etc.) based on your method.

Thank you so much!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.