public bool Remove(T item) { // possible things that this needs to deal with. 1) there is no node to begin with (it is empty) 2) there is only one node in the list // 3) the list contains many nodes and we need to remove either the first, or an item from middle or last LaurasLinkedListNode <T> previous = null; LaurasLinkedListNode <T> current = Head; while (current != null) { if (current.Value.Equals(item)) { if (previous != null) { // this means there is more than one item in the list // set the next point of previous to the next pointer of current (as current is going away) previous.Next = current.Next; if (current.Next == null) { // this means that now we've reached the tail Tail = previous; } Count--; } else { RemoveFirst(); } return(true); } previous = current; current = current.Next; } return(false); }
IEnumerator IEnumerable.GetEnumerator() { LaurasLinkedListNode <T> current = Head; while (current != null) { yield return(current.Value); current = current.Next; } }
public void CopyTo(T[] array, int arrayIndex) { // copy all elements from the linked list into the array. LaurasLinkedListNode <T> current = Head; while (current != null) { array[arrayIndex++] = current.Value; current = current.Next; } }
public void RemoveFirst() { if (Count != 0) { Head = Head.Next; Count--; if (Count == 0) { Tail = null; } } }
public void AddFirst(LaurasLinkedListNode <T> node) { LaurasLinkedListNode <T> temp = Head; Head = node; Head.Next = temp; Count++; if (Count == 1) { Tail = Head; } }
public void AddLast(LaurasLinkedListNode <T> node) { if (Count == 0) { Head = node; } else { Tail.Next = node; } Tail = node; Count++; }
public bool Contains(T item) { LaurasLinkedListNode <T> current = Head; while (current != null) { if (current.Value.Equals(item)) { return(true); } current = current.Next; } return(false); }
public void RemoveLast() { if (Count != 0) { if (Count == 1) { Head = null; Tail = null; } else { // item1, item2, item3 // want to remove item3. So set Tail = item2 LaurasLinkedListNode <T> current = Head; while (current.Next != Tail) { current = current.Next; } } Count--; } }