开发者

doubly-circular linked list in c#

开发者 https://www.devze.com 2023-03-12 12:53 出处:网络
I am using a doubly-circular linked list. After adding values I am retrieving values from the list. I am using this code to add values:

I am using a doubly-circular linked list. After adding values I am retrieving values from the list. I am using this code to add values:

public void Add(T data)
{
    if (root == null)
    {
        this.Add(data);
        root = new LinkedListNode<T>(data);
        last = root;
    }
    else
    {
        last.Next = new LinkedListNode<T>(data);
        last.Next.Previous = last;
        last = last.Next;
    }
}

I am giving the data as:

char[] dir = { 'n', 'w', 开发者_运维知识库's', 'e' };
TestProject.classes.LinkedList<char> list = new TestProject.classes.LinkedList<char>();

foreach (char c in dir)
{
    list.Add(c);
}

I am retrieving the list data as:

public LinkedListNode<T> GetAt(int index)
{
    var current = root;
    for (int i = 0; i < index; i++)
    {
        if (current == null)
            return null;
        current = current.Next;
    }
    return current;
}

The problem is it is giving the value of current as null while retrieving the node value.

The list is an example from stackoverflow itself.

Please help me..

My whole linked list class is as:

public class LinkedList<T>
{
    protected LinkedListNode<T> root = null;
    protected LinkedListNode<T> last = null;
    public LinkedList()
    {
    }

    public string ToString()
    {
        StringBuilder sb = new StringBuilder();
        var node = root;
        while (node != null)
        {
            sb.Append("{ " + node.Data.ToString() + " } ");
            node = node.Next;
        }
        return sb.ToString();
    }

    public T this[int index]
    {
        get
        {
            var node = GetAt(index);
            if (node == null)
                throw new ArgumentOutOfRangeException();
            return node.Data;
        }
        set
        {
            var node = GetAt(index);
            if (node == null)
                throw new ArgumentOutOfRangeException();
            node.Data = value;
        }
    }

    public LinkedListNode<T> GetAt(int index)
    {
        var current = root;
        for (int i = 0; i < index; i++)
        {
            if (current == null)
                return null;
            current = current.Next;
        }
        return current;
    }

    public void Add(T data)
    {
        if (root == null)
        {
            this.Add(data);
            root = new LinkedListNode<T>(data);
            last = root;
        }
        else
        {
            last.Next = new LinkedListNode<T>(data);
            last.Next.Previous = last;
            last = last.Next;
        }
    }
}

public class LinkedListNode<T>
{
    public T Data { get; set; }

    public LinkedListNode(T data)
    {
        Data = data;
    }

    public LinkedListNode<T> Next { get; set; }
    public LinkedListNode<T> Previous { get; set; }
}


If root == null when this is called, there's no reason for this function to work, it should be causing a stack overflow when it call itself over and over again...

public void Add(T data)
{
    if (root == null)
    {
        this.Add(data);  // recurse?!?

So, if you're successfully calling it there seems to be three options:

  • You've managed to configure your compiler optimizations so that it's removing the call (this seems unlikely, but I guess could be possible).
  • You're never be calling it with root==null, which means something else is modifying root, possible since it's protected, however if the usage code you've provided is correct, this shouldn't be the case, since you don't mention a derived class.
  • Your usage code is paraphrased and really you're calling add from within a try/catch block and ignoring the exception that's getting thrown.

As I've said in my comment, removing the extra call to this.add(data), should fix your problem (with populating the list), however, I'd suggest you step through the function calls in the debugger, so that you can see what's going on. I'd be interest to know if it really calls the 'Add' function.

As far as retrieving information from the list, the get function looks like it should work, assuming information has been put into the list correctly and you're calling get on the same list that the data was inserted into. Again, if it's not working, the debugger is your friend when trying to figure out which bit isn't populated the way you'd expect it to be.


As pointed out by @forsvarir: You should not call Add again when no root node exists. Just remove that call and everything will be fine.

If I were you I would not add Indexer or a GetAt method to a LinkedList implementation. It's quite inefficient to use a linked list of you need to have index access.

Make your list implement IEnumerable instead and let the users use LINQ.

0

精彩评论

暂无评论...
验证码 换一张
取 消