public void RemoveAt(int index) { if (index == 0) // handle edge-case { RemoveFirst(); return; } LinkedListItem previous = head; LinkedListItem current = head; LinkedListItem next; int indexer = 0; while (true) { if (indexer == index) { next = current?.GetNext(); //next = current != null ? current.GetNext() : current; //if (current != null) { next = current.GetNext(); } else { next = current; }; previous.ModifyNext(next); return; } indexer++; previous = current; current = current.GetNext(); if (current == null) { throw new IndexOutOfRangeException(); } } }
public void ReplaceAt(int requestedIndex, Object o) { LinkedListItem previous = head; LinkedListItem current = head; LinkedListItem next; int indexer = 0; while (true) { if (current == null) { throw new IndexOutOfRangeException(); } if (indexer == requestedIndex) { //next = current != null ? current.GetNext() : current; next = current?.GetNext(); var newItem = new LinkedListItem(o, next); previous.ModifyNext(newItem); if (indexer == 0) { head = newItem; } return; } indexer++; previous = current; current = current.GetNext(); } }
public void AddAt(int index, object o) { if (index == 0) // handle edge-case { AddFirst(o); return; } LinkedListItem previous = head; LinkedListItem current = head; int indexer = 0; while (true) { if (indexer == index) { var newItem = new LinkedListItem(o, current); previous.ModifyNext(newItem); return; } if (current == null) { throw new IndexOutOfRangeException(); } indexer++; previous = current; current = current.GetNext(); } }