IzzyTwinkly
asked on
very simple linked list - stackoverflow errror
Hi,
I am trying to build very simple Linked List.
Here is my whole code.
when I ran this code, i got the following error:
System.StackOverflowExcept ion 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~
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();
}
}
}
when I ran this code, i got the following error:
System.StackOverflowExcept
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...
ASKER
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?
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;
}
}
}
ASKER
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!!!
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!!!
ASKER