public void RemoveValuesFromEnd(int number) { if (number > Length) { throw new ArgumentOutOfRangeException("Ваш список меньше количества удаляемых элементов"); } if (number <= 0) { throw new ArgumentOutOfRangeException("колличество удаляемых элементов должно быть больше 0"); } if (number < Length) { DoubleNode current = _tail; for (int i = 1; i <= number; i++) { current = current.Previous; } _tail = current; current.Next = null; Length -= number; } else { Empty(); } }
public override bool Equals(object obj) { DoubleLinkedList list = (DoubleLinkedList)obj; if (this.Length != list.Length) { return(false); } DoubleNode currentThis = this._root; DoubleNode currentList = list._root; if (currentList is null && currentThis is null) { return(true); } while (!(currentThis.Next is null)) { if (currentThis.Value != currentList.Value) { return(false); } currentList = currentList.Next; currentThis = currentThis.Next; } if (currentList.Value != currentThis.Value) { return(false); } return(true); }
public void AddFirst(int value) { _root.Previous = new DoubleNode(value); _root.Previous.Next = _root; _root = _root.Previous; Length++; }
public void RemoveFirst() { if (Length == 0) { throw new ArgumentException("net elementov"); } _root = _root.Next; Length--; }
public void AddListFromEnd(DoubleLinkedList list) { DoubleNode currentList = list._root; for (int i = 1; i <= list.Length; i++) { Add(currentList.Value); currentList = currentList.Next; } }
public void AddListFromBeginning(DoubleLinkedList list) { DoubleNode currentList = list._tail; for (int i = 1; i <= list.Length; i++) { AddFirst(currentList.Value); currentList = currentList.Previous; } }
public override string ToString() { string Str = ""; DoubleNode now = _head; while (now != null) { Str += now.Data; now = now.Next; } return(Str); }
public void RemoveLastElement() { if (Length == 0) { throw new ArgumentException("net elementov"); } DoubleNode tmpDoubleNode = DoubleNodeByIndex(Length - 2); tmpDoubleNode.Next = null; _tail = tmpDoubleNode; Length--; }
public void AddListByIndex(int index, DoubleLinkedList list) { if (index >= Length || index < 0) { throw new IndexOutOfRangeException("Неверный индекс элемента"); } else if (index == 0) { AddListFromBeginning(list); } else if (index <= Length / 2) { DoubleNode currentThis = _root; for (int i = 1; i < index; i++) { currentThis = currentThis.Next; } DoubleNode tmp1 = currentThis.Next; DoubleNode currentList = list._root; for (int i = 0; i < list.Length; i++) { DoubleNode tmp2 = currentThis.Next; currentThis.Next = new DoubleNode(currentList.Value); currentThis.Next.Next = tmp2; currentList = currentList.Next; currentThis = currentThis.Next; } currentThis.Next = tmp1; Length += list.Length; } else { DoubleNode currentThis = _tail; for (int i = 1; i <= Length - index; i++) { currentThis = currentThis.Previous; } DoubleNode tmp1 = currentThis.Next; DoubleNode currentList = list._root; for (int i = 0; i < list.Length; i++) { DoubleNode tmp2 = currentThis.Next; currentThis.Next = new DoubleNode(currentList.Value); currentThis.Next.Next = tmp2; currentList = currentList.Next; currentThis = currentThis.Next; } currentThis.Next = tmp1; Length += list.Length; } }
public int SearchIndexByValue(int value) { DoubleNode current = _root; for (int i = 0; i < Length; i++) { if (current.Value == value) { return(i); } current = current.Next; } return(-1); }
public void Revers() { DoubleNode currentOne = _root; DoubleNode currentTwo = _tail; for (int i = 0; i < Length / 2; i++) { int tmp = currentOne.Value; currentOne.Value = currentTwo.Value; currentTwo.Value = tmp; currentOne = currentOne.Next; currentTwo = currentTwo.Previous; } }
//构造方法 public DoubleList(string Str) { str = Str; _head = new DoubleNode(str[0]); int i = 1; DoubleNode now = _head; while (i < str.Length) { now = new DoubleNode(str[i]); i++; now.Next = _head; _head.Prior = now; _head = now; } }
public void SortDescending() { int[] firstElement = new int[2]; firstElement = SearchIndexAndValueMaximumElement(); RemoveByIndex(firstElement[0]); DoubleLinkedList tmpList = new DoubleLinkedList(firstElement[1]); while (Length != 0) { int[] maximum = SearchIndexAndValueMaximumElement(); RemoveByIndex(maximum[0]); tmpList.Add(maximum[1]); } this._root = tmpList._root; Length = tmpList.Length; }
private int[] SearchIndexAndValueMinimumElement() { int indexMinElement = 0; int minElement = _root.Value; DoubleNode current = _root; for (int i = 0; i < Length; i++) { if (current.Value < minElement) { minElement = current.Value; indexMinElement = i; } current = current.Next; } return(new int[] { indexMinElement, minElement }); }
public void Insert(string Str) { int i = 0; DoubleNode HeadTemp; while (i < Str.Length) { HeadTemp = _head; _head = new DoubleNode(Str[i]); _head.Next = HeadTemp; if (HeadTemp != null) { HeadTemp.Prior = _head; } i++; } }
public void Revers() { DoubleNode current = _root; DoubleNode tmpDoubleNode = new DoubleNode(0); while (current.Next != null) { tmpDoubleNode = current.Next; current.Next = tmpDoubleNode.Next; tmpDoubleNode.Next = _root; _root = tmpDoubleNode; } DoubleLinkedList tmpdoubleLinkedList = new DoubleLinkedList(DoubleLinkedListTomMassInt()); _root = tmpdoubleLinkedList._root; _tail = tmpdoubleLinkedList._tail; }
public void Add(int value) { if (Length != 0) { Length++; //DoubleNode tmp = _tail; _tail.Next = new DoubleNode(value); _tail.Next.Previous = _tail; _tail = _tail.Next; //Console.WriteLine($"значение рута = {_root.Value} значение первиуса = {_tail.Previous.Value} значение тейла = {_tail.Value}"); } else { Length++; _tail = new DoubleNode(value); _root = _tail; } }
public void RemoveXElementsByStart(int x) { if (x > Length) { throw new ArgumentException("удаление больше элементов чем в массиве"); } for (int i = 0; i < x; i++) { RemoveFirst(); } if (Length == 0) { _root = null; _tail = null; } }
public void AddByIndex(int index, int value) { if (index == Length) { Add(value); } else if (index == 0) { AddFirst(value); } else if (index < 0 || index >= Length) { throw new IndexOutOfRangeException("Неверный индекс элемента"); } else if (index <= Length / 2) { DoubleNode current = _root; for (int i = 1; i < index; i++) { current = current.Next; } current.Next.Previous = new DoubleNode(value); current.Next.Previous.Next = current.Next; current.Next = current.Next.Previous; current.Next.Previous = current; Length++; } else { if (index > Length / 2) { DoubleNode current = _tail; for (int i = Length - 1; i > index; i--) { current = current.Previous; } current.Previous.Next = new DoubleNode(value); current.Previous.Next.Previous = current.Previous; current.Previous = current.Previous.Next; current.Previous.Next = current; Length++; } } }
public DoubleLinkedList(int[] values) { if (values.Length != 0) { Length = 1; _root = new DoubleNode(values[0]); _tail = _root; for (int i = 1; i < values.Length; i++) { Add(values[i]); } } else { _root = null; _tail = null; } }
public void Add(int value) { DoubleNode tmpDoubleNode = new DoubleNode(value); DoubleNode tmpPrevDoubleNode = _tail; if (Length != 0) { _tail.Next = tmpDoubleNode; _tail = _tail.Next; _tail.Prev = tmpPrevDoubleNode; } else { _root = tmpDoubleNode; _tail = _root; } Length++; }
public void AddLinkedListByIndex(int index, DoubleLinkedList insertLinkedList) { Length += insertLinkedList.Length; DoubleNode tmpNode = DoubleNodeByIndex(index); int[] tmpArray = new int[Length]; DoubleNodeByIndex(index - 1).Next = insertLinkedList._root; DoubleNodeByIndex(index + insertLinkedList.Length - 1).Next = tmpNode; for (int i = 0; i < Length; i++) { tmpArray[i] = DoubleNodeByIndex(i).Value; } DoubleLinkedList tmpLinkedList = new DoubleLinkedList(tmpArray); _root = tmpLinkedList._root; _tail = tmpLinkedList._tail; }
public void RemoveByIndex(int index) { DoubleNode tmpNode = DoubleNodeByIndex(index); if (index == 0) { RemoveFirst(); } else if (index == Length - 1) { RemoveLastElement(); } else { DoubleNodeByIndex(index - 1).Next = DoubleNodeByIndex(index + 1); DoubleNodeByIndex(index + 1).Prev = DoubleNodeByIndex(index - 1); Length--; } }
public override string ToString() { if (Length != 0) { DoubleNode current = _root; string s = current.Value + " "; while (!(current.Next is null)) { current = current.Next; s += current.Value + " "; } return(s); } else { return(String.Empty); } }
public void AddByZeroIndex(int value) { DoubleNode tmpDoubleNode = new DoubleNode(value); DoubleNode tmpPrevDoubleNode = _root; if (Length != 0) { tmpDoubleNode.Next = tmpPrevDoubleNode; _root = tmpDoubleNode; tmpPrevDoubleNode.Prev = tmpDoubleNode; } else { _root = tmpDoubleNode; _tail = _root; } Length++; }
public void RemoveValuesFromBeginning(int number) { if (number > Length) { throw new ArgumentOutOfRangeException("Ваш список меньше количества удаляемых элементов"); } if (number <= 0) { throw new ArgumentOutOfRangeException("колличество удаляемых элементов должно быть больше 0"); } DoubleNode current = _root; for (int i = 1; i <= number; i++) { current = current.Next; } _root = current; current.Previous = null; Length -= number; }
public void Delete(string Str) { int Times = 0, pTimes = 0; DoubleNode HeadTemp, PointTemp, preTemp; preTemp = new DoubleNode('0'); HeadTemp = preTemp; preTemp.Next = _head; PointTemp = preTemp.Next; while (PointTemp != null) { for (int j = 0; PointTemp != null && j < Str.Length; j++) { if ((char)PointTemp.Data != Str[j]) { continue; } preTemp.Next = PointTemp.Next; PointTemp = PointTemp.Next; if (PointTemp != null) { PointTemp.Prior = preTemp; } Times++; break; } if (Times > pTimes) { pTimes = Times; continue; } preTemp = preTemp.Next; PointTemp = PointTemp.Next; } if (Times == 0) { MessageBox.Show("不存在所要删除的元素.", "提示", MessageBoxButtons.OK, MessageBoxIcon.None); } _head = HeadTemp.Next; }
public int this[int index] { get { DoubleNode current = _root; for (int i = 1; i <= index; i++) { current = current.Next; } return(current.Value); } set { DoubleNode current = _root; for (int i = 1; i <= index; i++) { current = current.Next; } current.Value = value; } }
public DoubleLinkedList(int[] values) { Length = values.Length; if (values.Length != 0) { _root = new DoubleNode(values[0]); _tail = _root; DoubleNode tmpDoubleNode = _root; for (int i = 1; i < values.Length; i++) { tmpDoubleNode = _tail; _tail.Next = new DoubleNode(values[i]); _tail = _tail.Next; _tail.Prev = tmpDoubleNode; } } else { _root = null; _tail = null; } }
public void AddByIndex(int index, int value) { if (index == 0) { AddByZeroIndex(value); } else if (index == Length - 1) { Add(value); } else { DoubleNode firstDoubleNode = DoubleNodeByIndex(index - 1); DoubleNode endDoubleNode = DoubleNodeByIndex(index); DoubleNode insertDoubleNode = new DoubleNode(value); insertDoubleNode.Next = endDoubleNode; insertDoubleNode.Prev = firstDoubleNode; firstDoubleNode.Next = insertDoubleNode; endDoubleNode.Prev = insertDoubleNode; Length++; } }