private void PrintLinkedList(OneLinkedList <int> list) { foreach (int item in list) { outTextBox.AppendText(item + " -> "); } outTextBox.AppendText("null"); PrintNewLine(); }
//Сортировка пузырьком O(n^2) public void BubbleSorting <U>(OneLinkedList <U> list) where U : IComparable <U> { if (list == null) { return; } OneLinkedList <U> .Node <U> head = list.head; OneLinkedList <U> .Node <U> temp, firstElem, secondElem, forSwap; int step = 1; bool stop = false; while (!stop) { stop = true; firstElem = temp = head; secondElem = head.next; while (secondElem != null) { if (firstElem.item.CompareTo(secondElem.item) > 0) // Если > 0 тогда firstElem > { if (temp == firstElem) { head = secondElem; } else { temp.next = secondElem; } firstElem.next = secondElem.next; secondElem.next = firstElem; // Меняем местами firstElem и secondElem forSwap = firstElem; firstElem = secondElem; secondElem = forSwap; stop = false; } temp = firstElem; firstElem = firstElem.next; secondElem = secondElem.next; } outTextBox.AppendText($"Зв'язний список після кроку №{step++}"); PrintNewLine(); PrintLinkedList(head); } list.head = head; }
private void PrintLinkedList <U>(OneLinkedList <U> .Node <U> head) { OneLinkedList <U> .Node <U> t = head.Clone(); while (t != null) { outTextBox.AppendText(t.item + " -> "); t = t.next; } outTextBox.AppendText("null"); PrintNewLine(); }