/// <summary> /// Adds a new node containing the specified value at the end of the linked list /// </summary> /// <param name="value">The value to add at the end of the linked list</param> public void AddLast(T value) { CustomLinkedListNode <T> node = new CustomLinkedListNode <T>(value); if (Count == 0) { First = node; } else { Last.Next = node; node.Previous = Last; } Last = node; Count++; }
/// <summary> /// Retrieves indexed value /// </summary> /// <param name="i">Index</param> /// <returns>Indexed value</returns> public T this[int i] { get { if (i >= Count || i < 0) { throw new ArgumentException("invalid index"); } CustomLinkedListNode <T> current = this.First; int j = 0; while (j < i) { current = current.Next; j++; } return(current.Value); } }
/// <summary> /// Removes the node at the start of the linked list /// </summary> public void RemoveFirst() { if (Count == 0) { throw new InvalidOperationException("list is empty"); } CustomLinkedListNode <T> previous = First; First = First.Next; previous.Next = null; Count--; if (Count == 0) { Last = null; } else { First.Previous = null; } }