Link to home
Start Free TrialLog in
Avatar of IzzyTwinkly
IzzyTwinklyFlag for United States of America

asked on

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~
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IzzyTwinkly

ASKER

Perfect! Thank you~~
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...
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?
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

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