簡體   English   中英

插入通用鏈接列表

[英]Inserting Into a Generic 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;
        }
    }

如果head為null,則需要添加count ++。 另外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++;
        }             
    }
}

看看是否有幫助。

您沒有正確地在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++;
            }
        }             
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM