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?
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

0

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~~
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...
1
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0
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!!!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.