public override bool Equals(object obj) { DLinkedList dlinkedList = (DLinkedList)obj; if (Lenght != dlinkedList.Lenght) { return(false); } DNode tmpHead1 = _head; DNode tmpTail1 = _tail; DNode tmpHead2 = dlinkedList._head; DNode tmpTail2 = dlinkedList._tail; for (int i = 0; i < Lenght; i++) { if (tmpHead1.Value != tmpHead2.Value || tmpTail1.Value != tmpTail2.Value) { return(false); } tmpHead1 = tmpHead1.Next; tmpHead2 = tmpHead2.Next; tmpTail1 = tmpTail1.Previous; tmpTail2 = tmpTail2.Previous; } return(true); }
private void Merge(DLinkedList list, DLinkedList supList, DLinkedList finishList) { while (list._head != null && supList._head != null) { if (list._head.Value <= supList._head.Value) { finishList.AddToEnd(list._head.Value); list._head = list._head.Next; } else { finishList.AddToEnd(supList._head.Value); supList._head = supList._head.Next; } } if (list._head == null) { while (supList._head != null) { finishList.AddToEnd(supList._head.Value); supList._head = supList._head.Next; } } else { while (list._head != null) { finishList.AddToEnd(list._head.Value); list._head = list._head.Next; } } }
public void Sort() { DLinkedList list = new DLinkedList(); list._head = _head; list._tail = _tail; list.Lenght = Lenght; list = MergeSort(list); _head = list._head; _tail = list._tail; }
public void AddToEnd(int[] array) { DLinkedList added = new DLinkedList(array); if (Lenght == 0) { _head = added._head; _tail = added._tail; } else if (added.Lenght != 0) { _tail.Next = added._head; added._head.Previous = _tail; _tail = added._tail; } Lenght += added.Lenght; }
public void AddToBiginning(int[] array) { DLinkedList added = new DLinkedList(array); if (Lenght == 0) { _head = added._head; _tail = added._tail; } else if (added.Lenght != 0) { DNode tmp = _head; _head = added._head; added._tail.Next = tmp; tmp.Previous = added._tail; } Lenght += added.Lenght; }
static private DLinkedList MergeSort(DLinkedList list) { DLinkedList finishList = new DLinkedList(); DLinkedList supList = new DLinkedList(); DNode current = list._head; DNode tmp = list._head; int curLength = list.Lenght; if (curLength < 2) { return(list); } int tL = curLength / 2; for (int i = 1; i < tL; i++) { current = current.Next; } tmp = current.Next; DNode tmp2 = current.Next.Previous; supList._tail = list._tail; current.Next.Previous = null; current.Next = null; list._tail = current; supList._head = tmp; if (curLength % 2 == 0) { list.Lenght /= 2; supList.Lenght = tL; } else { list.Lenght /= 2; supList.Lenght = tL + 1; } list = MergeSort(list); supList = MergeSort(supList); finishList.Merge(list, supList, finishList); return(finishList); }
public void AddTo(int idx, int[] array) { if (idx != 0 && (idx < 0 || idx >= Lenght)) { throw new IndexOutOfRangeException(); } DLinkedList added = new DLinkedList(array); if (Lenght == 0) { _head = added._head; _tail = added._tail; } else if (added.Lenght != 0) { DNode cur = GetDNodeByIndex(idx - 1); DNode tmp = cur.Next; cur.Next = added._head; added._head.Previous = cur; added._tail.Next = tmp; tmp.Previous = added._tail; } Lenght += added.Lenght; }