插入通用链接列表

时间:2014-08-20 05:28:06

标签: c# generics insert linked-list

我在将新对象插入链接列表时遇到问题。我使用一个名为Insert的自定义方法,它接受对象类型和应该放置它的索引。但是,当我插入对象时,列表不会更新。知道为什么吗?

清单:

        GenericLinkedList<string> bet = new GenericLinkedList<string>();
        bet.Add("a");
        bet.Add("b");
        bet.Add("c");
        bet.Add("d");

        //bet.Remove();
        bet.Insert("x", 2);

通用链接列表类:

public class GenericLinkedList<T> where T : IComparable<T> 
{

    public GenericLinkedList()
    {
        count = 0;
        head = null;
    }

    #region Nested Node Class
    private class Node<T>
    {
        private T data;
        public T Data
        {
            get { return data; }
            set { data = value; }
        }

        private Node<T> next;
        public Node<T> Next
        {
            get { return next; }
            set { next = value; }
        }

        public Node<T> Previous { get; set; }


        public Node(T obj_t)
        {
            next = null;
            data = obj_t;
        }
        public Node()
        {
            next = null;
        }


    }
    #endregion

    private Node<T> head;
    private Node<T> tail;


    private int count;
    public int Count
    {
        get
        {
            int num;
            if (count < 0)
                num = 0;
            else {
                num = count;
            }
            return num;
        }
    }

}

插入方法:

    public void Insert(T data, int index)
    {
        Node<T> replacedItem = new Node<T>();
        Node<T> newItem = new Node<T>(data);

        if (head == null)
        {
            head = new Node<T>(data);
            tail = head;
        }
        else
        {
            Node<T> current = head;
            Node<T> tempNode = new Node<T>();


            if (index > 0 && index <= count + 1)
            {
                int c = 0;
                while (current != null)
                {
                    if (c == index)
                    {
                        tempNode = current;
                        current = newItem;
                        current.Next = tempNode;
                    }
                    else
                    {
                        current = current.Next;
                    }
                    c++;
                }
            }
            count++; 
        }
    }

添加方法:

  public void Add(T data)
    {
        count++;

        if (head == null)
        {
            head = new Node<T>(data);
            tail = head;
        }
        else
        {
            tail.Next = new Node<T>(data);
            tail.Next.Previous = tail;
            tail = tail.Next;
        }
    }

2 个答案:

答案 0 :(得分:0)

如果head为null,则需要添加count ++。另外count ++应该进入if(index&gt; 0&amp;&amp; index&lt; = count + 1)

public void Insert(T data, int index)
{
    Node<T> replacedItem = new Node<T>();
    Node<T> newItem = new Node<T>(data);

    if (head == null)
    {
        head = new Node<T>(data);
        tail = head;
        count++;
    }
    else
    {
        Node<T> current = head;
        Node<T> tempNode = new Node<T>();


        if (index > 0 && index <= count + 1)
        {
            int c = 0;
            while (current != null)
            {
                if (c == index)
                {
                    tempNode = current;
                    current = newItem;
                    current.Next = tempNode;
                }
                else
                {
                    current = current.Next;
                }
                c++;
            }
            count++;
        }             
    }
}

看看这是否有帮助。

答案 1 :(得分:0)

您未在Insert方法中正确添加新项目 在这个地方

tempNode = current; // save pointer to current item
current = newItem; // save in current new item
current.Next = tempNode; //set next to counter

但你没有改变上一个元素的链接,所以当你再次从头开始时,你就会错过你添加的项目。

您需要更改此代码,例如

public void Insert(T data, int index)
{
    Node<T> replacedItem = new Node<T>();
    Node<T> newItem = new Node<T>(data);

    if (head == null)
    {
        head = new Node<T>(data);
        tail = head;
        count++;
    }
    else
    {
        Node<T> current = head;
        Node<T> tempNode = new Node<T>();


        if (index > 0 && index <= count + 1)
        {
            int c = 0;
            while (current != null)
            {
                if (c == index)
                {
                    tempNode = current;
                    current = newItem;

                    //update link from previous element
                    if(tempNode.Previous != null) 
                       tempNode.Previous.Next = current;

                    current.Next = tempNode;
                    current.Previous = tempNode.Previous;
                    tempNode.Previous = current;
                    count++;
                    break;
                }
                else
                {
                    current = current.Next;
                }
                c++;
            }
        }             
    }
}