public void Add(T t) { _count++; if (_tail == null) { _tail = new SimpleLinkedListNode <T>(t, null, null); _head = _tail; return; } _tail = _tail.Add(t); }
private SimpleLinkedListNode <T> PerformActionOnIndexElement(int index, SimpleLinkedListNode <T> current, Func <SimpleLinkedListNode <T>, SimpleLinkedListNode <T> > func) { if (index == 0) { return(func(current)); } if (current == null || current.Next == null) { throw new ArgumentException("List doesn't have so many elements."); } return(PerformActionOnIndexElement(--index, current.Next, func)); }
public void Remove(int index) { _count--; var modifiedNode = PerformActionOnIndexElement(index, _head, _ => _.Remove()); if (modifiedNode != null) { _head = modifiedNode; } if (_count == 0) { _head = null; } }
public void Add(T t, int index) { _count++; if (index == 0) { var newHead = new SimpleLinkedListNode <T>(t, _head, null); _head = newHead; return; } var modifiedNode = PerformActionOnIndexElement(--index, _head, _ => _.Add(t)); if (index == _count - 1) { _tail = modifiedNode; } }