Добавить узел по индексу в настраиваемый LinkedList со сдвигом вправо - PullRequest
2 голосов
/ 13 июля 2020

Я столкнулся с очень странной проблемой. Я не могу вставить узел по индексу в настраиваемый связанный список. Итак, вот мой метод:

public void Insert(int index, T item)
    {
        if(item == null)
            throw new ArgumentNullException(nameof(item), "Item is equal null.");

        if (index > Count || index < 0)
            throw new ArgumentOutOfRangeException(nameof(index),"Index out of range.");

        Item<T> newNode = new Item<T>(item);
        Item<T> currentItem = Head;

        if (index == 0)
        {
            newNode.Next = Head;
            return;
        }

        var x = FindItemByData(this[index]);

        while (currentItem.Next != null)
        {
            if (currentItem.Next == x)
            {
                currentItem.Next = newNode;
                newNode.Next = FindItemByData(this[index]);
            }

            currentItem = currentItem.Next;
        }
    }

И есть класс Item:

public class Item<T>
   {
       public T Data { get; set; }
       public Item<T> Next { get; set; }
       public Item(T data)
       {
           Data = data;
           Next = null;
       }
   }

Индексатор моего связанного списка возвращает данные из указанного узла в списке, поэтому мне нужно вставить новый узел в список с данными: item. Например: у меня есть список предметов ⛑ ? ? ?. И мне нужно вставить в индекс 1 - элемент ?, список результатов может быть ⛑ ? ? ? ?.

Я попытался показать, что эта проблема не проста, потому что я не могу найти решение для вставки с помощью index всегда есть только «Вставить перед».

Метод, который я реализовал, не работает. Надеюсь, кто-нибудь сможет мне с этим помочь.

Также есть тест на этот метод:

        [TestCase(0)]
        [TestCase(5)]
        [TestCase(2)]
        public void Insert_AtPositionValue_ReturnCount(int position)
        {
            //arrange
            CustomList<int> list = new CustomList<int>(1, 2, 7, 8, 10);

            //act
            int elementToInsert = 100;
            list.Insert(position, elementToInsert);
            int count = list.Count;

            int actualValue = list[position];


            //arrange
            Assert.Multiple(() =>
            {
                Assert.AreEqual(elementToInsert, actualValue, message: "Insert or Get Count  work incorrectly ");
                Assert.AreEqual(6, count, message: "Insert or Get Count  work incorrectly ");
            });
        }

Ответы [ 2 ]

0 голосов
/ 13 июля 2020

Собственно предыдущий автор был близок. Но ошибка заключалась в передаче ссылок на следующий элемент.

public void Insert(int index, T item)
        {
            if (item == null)
                throw new ArgumentNullException(nameof(item), "Item is equal null.");

            if (index > Count || index < 0)
                throw new ArgumentOutOfRangeException(nameof(index), "Index out of range.");

            Item<T> newNode = new Item<T>(item);
            Item<T> currentItem = Head;

            if (index == 0)
            {
                newNode.Next = Head;
                Head = newNode;
                Count++;
                return;
            }

            for (int i = 0; i < index - 1; i++) 
            {
                currentItem = currentItem.Next;
            }

            newNode.Next = currentItem; 
            currentItem.Next = newNode;
            Count++;
        }
0 голосов
/ 13 июля 2020
• 1000
public void Insert(int index, T item)
{
    if(item == null)
        throw new ArgumentNullException(nameof(item), "Item is equal null.");

    if (index > Count || index < 0)
        throw new ArgumentOutOfRangeException(nameof(index),"Index out of range.");

    Item<T> newNode = new Item<T>(item);
    Item<T> currentItem = Head;

    if (index == 0)
    {
        
        newNode.Next = Head;
        Head = newNode; //you forgot this. You have to reset the head of the list.
        return;
    }

    //delete this <var x = FindItemByData(this[index]);>

    
    for (int i = 0; i<index-1; i++) //if index == 1 I want to add right after head
    {
        currentItem = currentItem.Next;
    }

    newNode.Next = currentItem.Next; //append the rest of the list after newNode
    currentNode.Next = newNode; //insert new node after the current node
}
...