public void RemoveFirst() { if (Count != 0) { _head = _head.Next; // 2nd item becomes new head. Count--; // Count decreases. if (Count == 0) { _tail = null; // If there are no items, tail is now null too. } else { _head.Previous = null; // new head's Previous should point to null } // I wondered why this was in the if/else rather than simply being above Count-- // but I think it's because this operation is not necessary if the item had been // a singleton and spares us one command in that case. } }
public void RemoveLast() { if (Count != 0) { _tail = _tail.Previous; Count--; if (Count == 0) { _head = null; } else { _tail.Next = null; } // I don't know why, but the book broke pattern here and created a // if (Count == 1) case where _head and _tail are set to null. // but this is accounted for with the above code, keeping the same pattern // as our RemoveFirst method. I.e., if there is one item in the list, // _tail.Previous is null so _tail becomes null. When you do Count-- // Count becomes 0, then _head is set to null, resulting in null/null. } }
public void Clear() { _head = null; _tail = null; Count = 0; }