public void RemoveFirst() { if (Length == 1) { _root = null; _tail = null; Length = 0; } else if (Length > 0) { DoubleLinkedListNode <T> current = _root.Next; _root = current; _root.Previous = null; --Length; } }
public override string ToString() { StringBuilder result = new StringBuilder(string.Empty); if (Length != 0) { DoubleLinkedListNode <T> currentNode = _root; result.Append($"{ currentNode.Value} "); while (!(currentNode.Next is null)) { currentNode = currentNode.Next; result.Append($"{ currentNode.Value} "); } } return(result.ToString().Trim()); }
public void Reverse() { if (Length > 0) { DoubleLinkedListNode <T> current = _root; while (!(current is null !)) { var tempNode = current.Next; current.Next = current.Previous; current.Previous = tempNode; current = current.Previous; } current = _root; _root = _tail; _tail = current; } }
public void RemoveRangeByIndex(int index, int count) { if (index >= 0 && index < Length) { if (index == 0) { RemoveRangeFirst(count); } else if (count == Length - 1) { RemoveRange(count); } else if (count > 0) { if (count + index >= Length) { DoubleLinkedListNode <T> current = GetNodeByIndex(index - 1); current.Next = null; _tail = current; Length = index; } else { DoubleLinkedListNode <T> startRemoveRangeNode = GetNodeByIndex(index - 1); DoubleLinkedListNode <T> finishRemoveRangeNode = GetNodeByIndex(index + count - 1); startRemoveRangeNode.Next = finishRemoveRangeNode.Next; finishRemoveRangeNode.Next.Previous = startRemoveRangeNode; Length -= count; } } else { throw new ArgumentException("Invalid count"); } } else if (index < 0 || index >= Length) { throw new IndexOutOfRangeException(); } }
public T[] ToArray() { if (Length > 0) { T[] result = new T[Length]; DoubleLinkedListNode <T> currentR = _root; DoubleLinkedListNode <T> currentT = _tail; for (int i = 0; i <= Length / 2; i++) { result[i] = currentR.Value; result[Length - i - 1] = currentT.Value; currentR = currentR.Next; currentT = currentT.Previous; } return(result); } return(new T[] { }); }
private DoubleLinkedList(T[] values) { Length = values.Length; if (values.Length != 0) { _root = new DoubleLinkedListNode <T>(values[0]); _tail = _root; for (int i = 1; i < values.Length; i++) { DoubleLinkedListNode <T> tempNode = _tail; _tail.Next = new DoubleLinkedListNode <T>(values[i]); _tail = _tail.Next; _tail.Previous = tempNode; } } else { _root = null; _tail = _root; } }
public override bool Equals(object obj) { if (!(obj is null)) { DoubleLinkedList <T> list = (DoubleLinkedList <T>)obj; if (this.Length == list.Length) { if (this.Length == 0 && list.Length == 0) { return(true); } DoubleLinkedListNode <T> currentThisNode = this._root; DoubleLinkedListNode <T> currentListNode = list._root; do { if (!(currentListNode.Next is null) || !(currentThisNode.Next is null)) { if (currentThisNode.Value.CompareTo(currentListNode.Value) != 0) { return(false); } currentListNode = currentListNode.Next; currentThisNode = currentThisNode.Next; } }while (!(currentThisNode.Next is null)); return(true); } return(false); } throw new ArgumentNullException(); }
private DoubleLinkedListNode <T> GetNodeByIndex(int index) { DoubleLinkedListNode <T> currentNode = null; if (index < Length / 2 + 1) { currentNode = _root; for (int i = 0; i < index; i++) { currentNode = currentNode.Next; } } else { currentNode = _tail; for (int i = Length - 1; i > index; i--) { currentNode = currentNode.Previous; } } return(currentNode); }
public void Add(T value) { if (value != null) { if (_tail is null) { _root = new DoubleLinkedListNode <T>(value);; _tail = _root; } else { _tail.Next = new DoubleLinkedListNode <T>(value); _tail.Next.Previous = _tail; _tail = _tail.Next; } ++Length; } else { throw new ArgumentException("Value is null"); } }
public void AddByIndex(int index, T value) { if (value != null) { if (index >= 0 && index <= Length) { if (index == 0) { AddFirst(value); } else if (index == Length) { Add(value); } else { DoubleLinkedListNode <T> insertNode = new DoubleLinkedListNode <T>(value); DoubleLinkedListNode <T> currentNode = GetNodeByIndex(index - 1); insertNode.Next = currentNode.Next; currentNode.Next = insertNode; insertNode.Previous = currentNode; insertNode.Next.Previous = insertNode; ++Length; } } else { throw new IndexOutOfRangeException(); } } else { throw new ArgumentException("Value is null"); } }
public void RemoveRange(int count) { if (Length > 0 && count >= 0) { if (count >= Length) { _root = null; _tail = null; Length = 0; } else { DoubleLinkedListNode <T> current = GetNodeByIndex(index: Length - count - 1); current.Next = null; _tail = current; Length -= count; } } else if (count < 0) { throw new ArgumentException("Invalid count"); } }
public void AddRangeByIndex(int index, IList <T> list) { if (list != null) { DoubleLinkedList <T> doubleList = DoubleLinkedList <T> .Create(list.ToArray()); if (index >= 0 && index <= Length) { if (index == 0) { AddRangeFirst(list); } else if (index == Length) { AddRange(list); } else { DoubleLinkedListNode <T> currentNode = GetNodeByIndex(index - 1); doubleList._root.Previous = currentNode; currentNode.Next.Previous = doubleList._tail; doubleList._tail.Next = currentNode.Next; currentNode.Next = doubleList._root; Length += doubleList.Length; } } else { throw new IndexOutOfRangeException(); } } else { throw new NullReferenceException("List is null"); } }
public T GetMax() { if (Length > 0) { DoubleLinkedListNode <T> current = _root; T maxValue = current.Value; for (int i = 0; i < Length; i++) { if (current.Value.CompareTo(maxValue) == 1) { maxValue = current.Value; } current = current.Next; } return(maxValue); } else { throw new ArgumentException("Array no contain elements"); } }
public int GetMaxIndex() { if (Length > 0) { DoubleLinkedListNode <T> current = _root; T maxValue = current.Value; int indexMaxValue = 0; for (int i = 0; i < Length; i++) { if (current.Value.CompareTo(maxValue) == 1) { maxValue = current.Value; indexMaxValue = i; } current = current.Next; } return(indexMaxValue); } return(-1); }
public void AddFirst(T value) { if (value != null) { DoubleLinkedListNode <T> insertNode = new DoubleLinkedListNode <T>(value); if (Length == 0) { _root = insertNode; _tail = _root; ++Length; } else { insertNode.Next = _root; _root = insertNode; _root.Next.Previous = insertNode; ++Length; } } else { throw new ArgumentException("Value is null"); } }
public DoubleLinkedList() { Length = 0; _root = null; _tail = _root; }
private DoubleLinkedList(T value) { Length = 1; _root = new DoubleLinkedListNode <T>(value); _tail = _root; }