private void SetPrevNode(MyLinkedListItem <T> item, MyLinkedListItem <T> prev) { try { item.Prev = prev; } catch (NullReferenceException) { } }
private void SetNextNodeOrRoot(MyLinkedListItem <T> item, MyLinkedListItem <T> next) { try { item.Next = next; } catch (NullReferenceException) { this._root = next; } }
public void Add(T item) { try { var last = this.GetLast(); var newItem = new MyLinkedListItem <T>(item); this.SetNextNodeOrRoot(last, newItem); this.SetPrevNode(newItem, last); } catch (NullReferenceException) { this.SetRoot(new MyLinkedListItem <T>(item)); } }
public void AddAfter(T existing, T newElement) { try { var existingItem = this.Find(existing); var nextItem = existingItem.Next; var newItem = new MyLinkedListItem <T>(newElement); this.SetNextNodeOrRoot(newItem, nextItem); this.SetPrevNode(newItem, existingItem); this.SetNextNodeOrRoot(existingItem, newItem); this.SetPrevNode(nextItem, newItem); } catch (NullReferenceException) { throw new InvalidOperationException(); } }
private void SetRoot(MyLinkedListItem <T> item) { this._root = item; }
public MyLinkedList() { this._root = null; }