//вставить элемент перед элементом с указанным индексом //если элемента нет - вставить в конец public void Insert(IListItem item, int index) { if (index < 0) { return; } if (index == 0) { AddFirst(item); return; } IListItem CurrItem = ListHead; for (int i = 0; i < index - 1; i++) { CurrItem = CurrItem.Next(); } if ((CurrItem == null) || (CurrItem.Next() == null)) { AddLast(item); return; } ListItem NewItem = new ListItem(item.Value, CurrItem); NewItem.NextElem = CurrItem.Next(); ListItem PrevItem = CurrItem as ListItem; ListItem NextItem = CurrItem.Next() as ListItem; PrevItem.NextElem = NewItem; NextItem.PrevElem = NewItem; }
//сортировка списка в обратном порядке public void Reverse() { IListItem CurrItem = ListHead; while (CurrItem != null) { ListItem item = CurrItem as ListItem; IListItem Temp = item.NextElem; item.NextElem = item.PrevElem; item.PrevElem = Temp; if (CurrItem.Next() == null) { ListHead = CurrItem as ListItem; } CurrItem = CurrItem.Next(); } }
//добавить элемент в конец public void AddLast(IListItem item) { if (ListHead == null) { ListHead = new ListItem(item.Value); } else { IListItem CurrItem = ListHead; while (CurrItem != null) { if (CurrItem.Next() == null) { ListItem NewItem = new ListItem(item.Value, CurrItem); ListItem Item = CurrItem as ListItem; Item.NextElem = NewItem; } CurrItem = CurrItem.Next(); } } }
//вернуть все элементы списка, кроме первого public IEnumerable <IListItem> GetAll() { List <IListItem> Items = new List <IListItem>(); IListItem CurrItem = ListHead; while (CurrItem != null) { Items.Add(CurrItem); CurrItem = CurrItem.Next(); } return(Items); }