1.3.20

1.3.20 #

解答 #

和上一题类似,只不过这次让 Cur 移动 k – 1 次即可。

代码 #

/// <summary>
/// 删除指定位置的元素,返回该元素。
/// </summary>
/// <param name="index">需要删除元素的位置。</param>
/// <returns></returns>
public Item Delete(int index)
{
    if (index >= this.count)
    {
        throw new IndexOutOfRangeException();
    }

    Node<Item> front = this.first;
    Item temp = this.first.item;
    if (index == 0)
    {
        this.first = this.first.next;
        return temp;
    }

    for (int i = 1; i < index; ++i)
    {
        front = front.next;
    }

    temp = front.next.item;
    front.next = front.next.next;
    this.count--;
    return temp;
}