简体   繁体   English

插入通用链接列表

[英]Inserting Into a Generic Linked List

I'm having an issue inserting a new object into a linked list. 我在将新对象插入到链表中时遇到问题。 I use a custom method called Insert that takes the object type and the index where it should be placed. 我使用一个名为Insert的自定义方法,该方法采用对象类型和应将其放置在其中的索引。 However, when I insert the object, the list doesn't update. 但是,当我插入对象时,列表不会更新。 Any idea why? 知道为什么吗?

The list: 名单:

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

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

The Generic Linked List Class: 通用链表类:

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;
        }
    }

} }

The Insert Method: 插入方法:

    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++; 
        }
    }

The Add Method: 添加方法:

  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;
        }
    }

Need to add count++ in case head is null. 如果head为null,则需要添加count ++。 Also count++ should go inside if(index>0&&index<=count+1) 另外count ++应该放在if(index> 0 && index <= 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++;
        }             
    }
}

See if this helps. 看看是否有帮助。

You not correctly add new item in your Insert method 您没有正确地在Insert方法中添加新项目
in this place 在这个地方

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

but you don't change link from previous element, so when you again go from head you just miss your added item. 但是您不会更改上一个元素的链接,因此当您再次从开头移开时,只会错过您添加的项目。

You need change this code something like 您需要像这样更改此代码

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++;
            }
        }             
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM