public static void Main() { var list = new SinglyLinkedList<int>(); for (int i = 0; i < 10; i++) { list.Add(i); Console.Write("Added " + i); Console.WriteLine(" Count -> " + list.Count); } Console.WriteLine("Items:"); foreach (var item in list) { Console.Write(item + " "); } Console.WriteLine(); int index = 0; for (int i = 0; i < 10; i++) { list.Remove(i - index++); Console.Write("Remmoved " + i); Console.WriteLine(" Count -> " + list.Count); } Console.WriteLine("Items:"); foreach (var item in list) { Console.Write(item + " "); } }
private static void Main() { var list = new SinglyLinkedList<int>(); list.ForEach(Console.WriteLine); Console.WriteLine("--------------------"); list.Add(5); list.Add(3); list.Add(2); list.Add(10, 1); list.Add(5); list.Add(99); Console.WriteLine("Count = {0}", list.Count); list.ForEach(Console.WriteLine); Console.WriteLine("--------------------"); list.Remove(1); Console.WriteLine("Count = {0}", list.Count); list.ForEach(Console.WriteLine); Console.WriteLine("--------------------"); Console.WriteLine(list.FirstIndexOf(5)); Console.WriteLine("--------------------"); Console.WriteLine(list.LastIndexOf(5)); }
static void Main() { SinglyLinkedList<int> list = new SinglyLinkedList<int>(); list.AddLast(1); list.AddLast(2); list.AddLast(3); list.AddLast(4); list.AddLast(5); list.AddLast(6); list.AddLast(3); Console.WriteLine("FirstIndexOf(6): {0}", list.FirstIndexOf(6)); Console.WriteLine("FirstIndexOf(1): {0}", list.FirstIndexOf(1)); Console.WriteLine("FirstIndexOf(16): {0}", list.FirstIndexOf(16)); Console.WriteLine("LastIndexOf(3): {0}", list.LastIndexOf(3)); Console.WriteLine("LastIndexOf(30): {0}", list.LastIndexOf(30)); Console.WriteLine(list.Count); list.Remove(0); foreach (var item in list) { Console.WriteLine(item); } }
public void AddFirst_Should_change_tail_when_tail_is_null() { ISinglyLinkedList<int> list = new SinglyLinkedList<int>(new int[0]); list = list.AddFirst(1); Assert.That(list.Head.Data, Is.EqualTo(1)); Assert.That(list.Tail.Data, Is.EqualTo(1)); }
public void AddTest() { var list = new SinglyLinkedList<int> { 1, 0, -1 }; Assert.AreEqual(3, list.Count, "Did not add the items"); list.Add(1); Assert.AreEqual(4, list.Count, "Did not add the item"); }
public void BeginTestMethod() { target = new SinglyLinkedList<int>(); Assert.AreEqual(target.Leng, 0); target.Add(2); target.Add(3, 1); target.AddLast(5); }
public void SinglyLinkedListIntIsNull() { _singlyLinkedListInt = null; _index = 5; int expected = 0; var actual = _linkedListOperationsInt.GetNthElementFromLast(_singlyLinkedListInt, _index); Assert.AreEqual(expected, actual); }
public void SetupUnitTests() { _linkedListOperationsInt = new LinkedListOperations<int>(); _singlyLinkedListInt = new SinglyLinkedList<int>(); _linkedListOperationsString = new LinkedListOperations<string>(); _singlyLinkedListString = new SinglyLinkedList<string>(); }
public static SinglyLinkedList<string> Create( SinglyLinkedListNode<string> node_singlyLinkedListNode, string[] items ) { PexAssume.IsNotNull(items); SinglyLinkedList<string> singlyLinkedList = new SinglyLinkedList<string>(items); return singlyLinkedList; }
public void AddLast_Should_change_tail_and_set_next_of_previous_tail() { ISinglyLinkedList<int> list = new SinglyLinkedList<int>(new[] { 1 }); var previous = list.Tail; list = list.AddLast(2); Assert.That(list.Tail.Data, Is.EqualTo(2)); Assert.That(previous.Next, Is.EqualTo(list.Tail)); }
public void TestSinglyLinkedListAdd() { SinglyLinkedList<string> list = new SinglyLinkedList<string>(); list.Add("One"); list.Add("Two"); list.Add("Three"); Assert.AreEqual(3, list.Count); }
public static SinglyLinkedList<int> Create( SinglyLinkedListNode<int> node_singlyLinkedListNode, int[] elements ) { PexAssume.IsNotNull(elements); SinglyLinkedList<int> singlyLinkedList = new SinglyLinkedList<int>(elements); return singlyLinkedList; }
public void Append_value_to_end_of_single_item_list() { var list = new SinglyLinkedList<int>(new ListNode<int>(12)); list.Append(42); Assert.That(list.Length, Is.EqualTo(2)); Assert.That(list.Head.Next.Value, Is.EqualTo(42)); }
public void AddAfterMiddleNodeTest() { SinglyLinkedList<int> sll = new SinglyLinkedList<int> {10, 20, 30}; sll.AddAfter(sll.Head.Next, 25); Assert.AreEqual(25, sll.Head.Next.Next.Value); Assert.AreEqual(30, sll.Head.Next.Next.Next.Value); Assert.AreEqual(4, sll.Count); }
public void Append_single_node_to_empty_list() { var list = new SinglyLinkedList<int>(null); var node = new ListNode<int>(42); list.Append(node); Assert.That(list.Length, Is.EqualTo(1)); Assert.That(list.Head, Is.EqualTo(node)); }
public void AddAfterTailTest() { SinglyLinkedList<int> sll = new SinglyLinkedList<int> {10, 20}; sll.AddAfter(sll.Tail, 30); Assert.AreEqual(30, sll.Tail.Value); Assert.AreEqual(30, sll.Head.Next.Next.Value); Assert.AreEqual(3, sll.Count); }
public void CopyToTest() { // NOTE: the Add method, adds them to the front of the list, therefore the list is in reverse var list = new SinglyLinkedList<int> { 1, 0, -1 }; var expected = new int[] { -1, 0, 1 }; var actual = new int[list.Count]; list.CopyTo(actual, 0); CollectionAssert.AreEqual(expected, actual, "The arrays do not match"); }
public void GetEnumeratorTest() { // NOTE: the Add method, adds them to the front of the list, therefore the list is in reverse var list = new SinglyLinkedList<int> { 1, 0, -1, 44, 22, 22, 11, 2, 3, 4, 5 }; var expected = new int[] { 5, 4, 3, 2, 11, 22, 22, 44, -1, 0, 1 }; var actual = new System.Collections.Generic.List<int>(); foreach (var item in list) { actual.Add(item); } CollectionAssert.AreEqual(expected, actual, "The collections do not match"); }
public void Append_single_node_to_end_of_multiple_item_list() { var head = new ListNode<int>(12) { Next = new ListNode<int>(77) }; var list = new SinglyLinkedList<int>(head); var node = new ListNode<int>(42); list.Append(node); Assert.That(list.Length, Is.EqualTo(3)); Assert.That(list.Head.Next.Next, Is.EqualTo(node)); }
public void AddAfterOnlyOneNodeInListTest() { SinglyLinkedList<int> sll = new SinglyLinkedList<int> {10}; sll.AddAfter(sll.Head, 20); Assert.AreEqual(20, sll.Tail.Value); Assert.AreEqual(10, sll.Head.Value); Assert.AreEqual(20, sll.Head.Next.Value); Assert.AreEqual(2, sll.Count); }
public void LastIndexOf_ExistingItem_ShouldReturnCorrectIndex() { var list = new SinglyLinkedList<int>(); list.Add(1); list.Add(101); list.Add(1); list.Add(101); var index = list.LastIndexOf(1); Assert.AreEqual(2, index); }
public void FirstIndexOf_NonExistingItem_ShouldReturnMinusOne() { var list = new SinglyLinkedList<int>(); list.Add(1); list.Add(101); list.Add(1); list.Add(101); var index = list.FirstIndexOf(1001); Assert.AreEqual(-1, index); }
public void Add_EmptyList_ShouldIncrementCount() { const int N = 100; var list = new SinglyLinkedList<int>(); for (int i = 0; i < N; i++) { list.Add(i); } Assert.AreEqual(N, list.Count); }
public void TestAddBefore() { var expected = new int[3] { 0, 1, 2 }; var list = new SinglyLinkedList<int>(); list.AddLast(0); var node = list.AddLast(2); list.AddBefore(node, 1); Verify(expected, list); }
public void TestSinglyLinkedListIndexer() { SinglyLinkedList<string> list = new SinglyLinkedList<string>(); list.Add("One"); list.Add("Two"); list.Add("Three"); list[0] = "Zero"; string[] array = new string[list.Count]; list.CopyTo(array); Assert.AreEqual("Zero, Two, Three", string.Join(", ", array)); }
public void TestAddAfterNoNext() { var expected = new int[3] { 0, 1, 2 }; var list = new SinglyLinkedList<int>(); list.AddLast(0); var node = list.AddLast(1); list.AddAfter(node, 2); Verify(expected, list); }
public void SortListTest() { var list = new SinglyLinkedList<int>(); foreach (int n in a) { list.Add(n); } MergeSort.Sort(list); string expected = "[ -59 -45 -12 11 11 44 53 62 80 94 ]"; string actual = list.ToString(); Assert.AreEqual(actual, expected); }
public void AddAfter_Should_change_tail_and_set_next_item_of_previous_item() { ISinglyLinkedList<int> list = new SinglyLinkedList<int>(new [] {1}); var previous = list.Head; list = list.AddAfter(2,previous); Assert.That(list.Tail.Data,Is.EqualTo(2)); Assert.That(previous.Next, Is.EqualTo(list.Tail)); list = list.AddAfter(3, previous); Assert.That(list.Tail.Data, Is.EqualTo(2)); Assert.That(previous.Next.Data, Is.EqualTo(3)); }
public void TestSinglyLinkedListInsert_InsertInTheMiddle() { SinglyLinkedList<string> list = new SinglyLinkedList<string>(); list.Add("One"); list.Add("Two"); list.Add("Three"); list[0] = "Zero"; list.Insert(1, "Ten"); string[] array = new string[list.Count]; list.CopyTo(array); Assert.AreEqual("Zero, Ten, Two, Three", string.Join(", ", array)); }
public void TestAddBeforeFirst() { var expected = new int[3] { 0, 1, 2 }; var list = new SinglyLinkedList<int>(); list.AddLast(1); list.AddLast(2); var node = list.AddBefore(list.First, 0); Verify(expected, list); Assert.AreSame(list.First, node); }
/// <summary> /// Splays the <see cref="SplayTree{T}"/> given a <see cref="SinglyLinkedList{T}"/> containing the traversed nodes. The first node being the one for splaying and the last being the root. /// </summary> /// <param name="traversedNodes">A <see cref="SinglyLinkedList{T}"/> containing the traversed nodes.</param> internal void Splay(SinglyLinkedList <SplayTreeNode <T> > traversedNodes) { if (traversedNodes == null) { throw new ArgumentNullException(nameof(traversedNodes)); } if (traversedNodes.Last.Value != Root) { throw new ArgumentException(nameof(traversedNodes) + "list does not end with the root node!"); } if (traversedNodes.Count == 1) { return; } var nodeToSplay = traversedNodes.First.Value; traversedNodes.RemoveFirst(); while (traversedNodes.Count != 0) { var parent = traversedNodes.First.Value; traversedNodes.RemoveFirst(); var grandParent = traversedNodes.First?.Value; if (grandParent != null) { traversedNodes.RemoveFirst(); } var greatGrandParent = traversedNodes.First?.Value; if (grandParent == null) { //one more rotation to make node root if (parent.Left == nodeToSplay) { Root = RotateRight(parent); } else { Root = RotateLeft(parent); } return; } else// if grandparent is not null { // check is grandparent is left child of great grandparent // NOTE: this also returns false if great grandparent is null bool grandParentIsLeftChild = greatGrandParent?.Left == grandParent; // if parent is left child of grandparent if (grandParent.Left == parent) { // if splay node is left child of its parent if (parent.Left == nodeToSplay) { // Left Left Case - fixed by 2 right rotations RotateRight(grandParent); RotateRight(parent); } else// if splay node is right child of its parent { //Left Right Case - fixed by left and right rotation grandParent.Left = RotateLeft(parent); RotateRight(grandParent); } } else// if parent is right child of grandparent { // if splay node is right child of its parent if (parent.Right == nodeToSplay) { // Right Right Case - fixed by 2 left rotations RotateLeft(grandParent); RotateLeft(parent); } else// if splay node is left child of its parent { //Right Left Case - fixed by right and left rotation grandParent.Right = RotateRight(parent); RotateLeft(grandParent); } } // After the rotations the splay node is on the grandparent // position so now the great grandparent have to point to it if (greatGrandParent == null) { // if there is no great grandparent // then the splay node is the new root Root = nodeToSplay; return; } else// if we have great grand parent { // we update were it points if (grandParentIsLeftChild) { greatGrandParent.Left = nodeToSplay; } else { greatGrandParent.Right = nodeToSplay; } } } } // The method returns as soon as splay node becomes the root // so this should never happen is the traversed nodes list is correct throw new Exception("Splaying was not performed correctly! The traversed nodes list is corrupted!"); }
public void SearchInRangeOfItems() { int itemCount = 2000; var list = new SinglyLinkedList <int>(); // Add only even numbers for (int i = 0; i < itemCount; i++) { list.AddLast(i); } int startIndex = list.Count / 3; int rangeCount = list.Count / 2; // Find corresponding nodes SinglyLinkedListNode <int> startNode = null; SinglyLinkedListNode <int> endNode = null; int curNodeIndex = 0; int nodesTraversedAfterStartNode = 0; var curNode = list.First; while (curNode != null) { // if we at the node on the given index save the start node if (curNodeIndex == startIndex) { startNode = curNode; } // if we are at the node or after the given index, increment the node counter if (curNodeIndex >= startIndex) { nodesTraversedAfterStartNode++; } // if we traversed enought nodes as the given count save the end node and break if (nodesTraversedAfterStartNode == rangeCount) { endNode = curNode; break; } // go onwards curNodeIndex++; curNode = curNode.Next; } // Check for items for (int i = startIndex; i < startIndex + rangeCount - 1; i++) { var fi = list.LinearSearchFirstNode(i, startNode, endNode, null); var li = list.LinearSearchLastNode(i, startNode, endNode, null); if (fi != li) { Assert.Fail(); } if (fi.Value != li.Value) { Assert.Fail(); } if (fi.Value != i) { Assert.Fail(); } } }
public void DeleteTest() { Node <string> node; var list = new SinglyLinkedList <string>(); // add/dell one el list.Add("x"); Assert.IsTrue(list.Count == 1); node = list.Contains("x"); list.Delete(node); Assert.IsTrue(list.Count == 0); Assert.IsTrue(list.First == null); Assert.IsTrue(list.Last == null); // add two els, del first list.Add("x"); list.Add("y"); Assert.IsTrue(list.Count == 2); node = list.Contains("x"); list.Delete(node); Assert.IsTrue(list.Count == 1); Assert.IsTrue(list.First.Value == "y"); Assert.IsTrue(list.Last.Value == "y"); node = list.Contains("y"); list.Delete(node); Assert.IsTrue(list.Count == 0); // add two els, del last list.Add("x"); list.Add("y"); Assert.IsTrue(list.Count == 2); node = list.Contains("y"); list.Delete(node); Assert.IsTrue(list.Count == 1); Assert.IsTrue(list.First.Value == "x"); Assert.IsTrue(list.Last.Value == "x"); node = list.Contains("x"); list.Delete(node); Assert.IsTrue(list.Count == 0); // add three els, del middle list.Add("x"); list.Add("y"); list.Add("z"); Assert.IsTrue(list.Count == 3); node = list.Contains("y"); list.Delete(node); Assert.IsTrue(list.Count == 2); Assert.IsTrue(list.First.Value == "x"); Assert.IsTrue(list.Last.Value == "z"); // now 'x' has ref to 'z' Assert.IsTrue(list.First.Next.Value == "z"); // double delete node = list.Contains("z"); list.Delete(node); Assert.ThrowsException <Exception>(() => { list.Delete(node); }); node = list.Contains("x"); list.Delete(node); Assert.ThrowsException <Exception>(() => { list.Delete(node); }); }
/// <summary> /// Merging two lists into a given <see cref="SinglyLinkedList{T}"/>. Used for descending sort. /// </summary> /// <typeparam name="T">The data type of the <see cref="SinglyLinkedList{T}"/>.</typeparam> /// <param name="mergeList">The <see cref="SinglyLinkedList{T}"/> for merging the left and right lists into.</param> /// <param name="left">The left <see cref="SinglyLinkedList{T}"/> containing some of the elements for sorting.</param> /// <param name="right">The right <see cref="SinglyLinkedList{T}"/> containing some of the elements for sorting.</param> /// <param name="nodeBeforeStartNode">The <see cref="SinglyLinkedListNode{T}"/> which is the node after the sorted sequence.</param> /// <param name="comparer">The <see cref="IComparable{T}"/> implementation used for comparing the elements.</param> /// <returns>Returns the given <see cref="SinglyLinkedList{T}"/> for the merged elements from the left <see cref="SinglyLinkedList{T}"/> and right <see cref="SinglyLinkedList{T}"/>.</returns> private static void MergeDescending <T>(SinglyLinkedList <T> mergeList, SinglyLinkedList <T> left, SinglyLinkedList <T> right, SinglyLinkedListNode <T> nodeBeforeStartNode, IComparer <T> comparer) { bool nodeBeforeStartNodeIsNull = nodeBeforeStartNode == null; SinglyLinkedListNode <T> lastAddedNode = nodeBeforeStartNode; // merging until one of the lists becomes empty while (left.Count > 0 && right.Count > 0) { if (comparer.Compare(left.First.Value, right.First.Value) >= 0) { var node = left.First; left.RemoveFirst(); if (nodeBeforeStartNodeIsNull)// if the start node is the first in the list { // Add after the last added node(if null add first) if (lastAddedNode == null) { mergeList.AddFirst(node); } else { mergeList.AddAfter(lastAddedNode, node); } } else// if the start node is not the first { mergeList.AddAfter(lastAddedNode, node);// add after the last added node } lastAddedNode = node;// Update last added node } else { var node = right.First; right.RemoveFirst(); if (nodeBeforeStartNodeIsNull)// if the start node is the first in the list { // Add after the last added node(if null add first) if (lastAddedNode == null) { mergeList.AddFirst(node); } else { mergeList.AddAfter(lastAddedNode, node); } } else// if the start node is not the first { mergeList.AddAfter(lastAddedNode, node);// add after the last added node } lastAddedNode = node;// Update last added node } } // add the remaining elements from the left or the right list while (left.Count > 0) { var node = left.First; left.RemoveFirst(); if (nodeBeforeStartNodeIsNull)// if the start node is the first in the list { // Add after the last added node(if null add first) if (lastAddedNode == null) { mergeList.AddFirst(node); } else { mergeList.AddAfter(lastAddedNode, node); } } else// if the start node is not the first { mergeList.AddAfter(lastAddedNode, node);// add after the last added node } lastAddedNode = node;// Update last added node } while (right.Count > 0) { var node = right.First; right.RemoveFirst(); if (nodeBeforeStartNodeIsNull)// if the start node is the first in the list { // Add after the last added node(if null add first) if (lastAddedNode == null) { mergeList.AddFirst(node); } else { mergeList.AddAfter(lastAddedNode, node); } } else// if the start node is not the first { mergeList.AddAfter(lastAddedNode, node);// add after the last added node } lastAddedNode = node;// Update last added node } }
/// <summary> /// Sorts the elements in the entire <see cref="SinglyLinkedList{T}"/> in descending order using the default comparer. /// </summary> /// <typeparam name="T">The data type of the <see cref="SinglyLinkedList{T}"/>.</typeparam> /// <param name="list">The <see cref="SinglyLinkedList{T}"/> containing the elements for sorting.</param> /// <returns>Returns the given <see cref="SinglyLinkedList{T}"/> when sorted.</returns> public static SinglyLinkedList <T> ParallelMergeSortDescending <T>(this SinglyLinkedList <T> list) { return(ParallelMergeSortDescending(list, Comparer <T> .Default)); }
public void CountEmptyList() { SinglyLinkedList list = new SinglyLinkedList(); Assert.AreEqual(0, list.Count()); }
public Stack(int val) { Zelda = new SinglyLinkedList(val); }
public void ToStringOnEmptyList() { SinglyLinkedList list = new SinglyLinkedList(); Assert.AreEqual("{ }", list.ToString()); }
public void ListBracketAccessor() { SinglyLinkedList list = new SinglyLinkedList("foo", "bar", "grille"); Assert.AreEqual("bar", list[1]); }
public void LastOnEmptyList() { SinglyLinkedList list = new SinglyLinkedList(); Assert.AreEqual(null, list.Last()); }
public void Init() { _list = new SinglyLinkedList <int>(); }
public void IsSortedOnLargerSortedList() { SinglyLinkedList list = new SinglyLinkedList("apple", "bar", "foo", "grille"); Assert.IsTrue(list.IsSorted()); }
public void TestAddAtTheEnd() { SinglyLinkedList <char> sut = new SinglyLinkedList <char>(); Assert.Equal(default, sut.GetLastNode());
public void ElementAt0OnEmptyList() { SinglyLinkedList list = new SinglyLinkedList(); list.ElementAt(0); }
public void InitializingWithInt() { SinglyLinkedList <int> list = new SinglyLinkedList <int>(new int[] { 1, 2, 3, 4 }); Assert.IsNotNull(list); }
public void IndexOfNodeInLastPosition() { SinglyLinkedList list = new SinglyLinkedList("foo", "bar", "grille"); Assert.AreEqual(2, list.IndexOf("grille")); }
public Stack() { Zelda = new SinglyLinkedList(); }
public void IndexOfStringThatDoesntExist() { SinglyLinkedList list = new SinglyLinkedList("foo", "bar", "grille"); Assert.AreEqual(-1, list.IndexOf("cat")); }
/// <summary> /// Sequential algorithm. In-place sorting of the given <see cref="SinglyLinkedList{T}"/> elements. Used for descending sort. /// </summary> /// <typeparam name="T">The data type of the <see cref="SinglyLinkedList{T}"/>.</typeparam> /// <param name="list">The <see cref="SinglyLinkedList{T}"/> containing the elements for sorting.</param> /// <param name="nodeBeforeStartNode">The <see cref="SinglyLinkedListNode{T}"/> which is the node before the sorted sequence.</param> /// <param name="startNode">The start <see cref="SinglyLinkedListNode{T}"/> of the current split.</param> /// <param name="endNode">The end <see cref="SinglyLinkedListNode{T}"/> of the current split.</param> /// <param name="comparer">The <see cref="IComparable{T}"/> implementation used for comparing the elements.</param> private static void SequentialSplitMergeDescending <T>(SinglyLinkedList <T> list, SinglyLinkedListNode <T> nodeBeforeStartNode, SinglyLinkedListNode <T> startNode, SinglyLinkedListNode <T> endNode, IComparer <T> comparer) { // End of recursion. If we have 1 item or less we consider it sorted if (list.Count < 2) { return; } // spliting the list into two lists var left = new SinglyLinkedList <T>(); var right = new SinglyLinkedList <T>(); var curNode = startNode; var nodeAfterEndNode = endNode.Next; bool nodeBeforeStartNodeIsNull = nodeBeforeStartNode == null; SinglyLinkedListNode <T> lastLeftNode = null; SinglyLinkedListNode <T> lastRightNode = null; int i = 0; int leftItemsNumber = list.Count / 2; while (curNode != nodeAfterEndNode) { if (i++ < leftItemsNumber) { var node = curNode; // save the current node curNode = curNode.Next; // go to the next node if (nodeBeforeStartNodeIsNull) // if the start node is first in the list { list.Remove(node); // remove the current node(which is the first node) } else// if the start node is not the first in the list { list.RemoveAfter(nodeBeforeStartNode); // remove the node after the node before the start node(i.e. the current node) } if (lastLeftNode == null) // if first time(no last node) { left.AddFirst(node); // add at the beginning } else// if we have last node { left.AddAfter(lastLeftNode, node); // add after it(faster than AddLast method) } lastLeftNode = node; // update last node } else { var node = curNode; // save the current node curNode = curNode.Next; // go to the next node if (nodeBeforeStartNodeIsNull) // if the start node is first in the list { list.Remove(node); // remove the current node(which is the first node) } else// if the start node is not the first in the list { list.RemoveAfter(nodeBeforeStartNode); // remove the node after the node before the start node(i.e. the current node) } if (lastRightNode == null) // if first time(no last node) { right.AddFirst(node); // add at the beginning } else// if we have last node { right.AddAfter(lastRightNode, node); // add after it(faster than AddLast method) } lastRightNode = node; // update last node } } // Recursively sort both sublists // NOTE: node before startNode is null because in the left and right splits // we work with all nodes from the list. SequentialSplitMergeDescending(left, null, left.First, left.Last, comparer); SequentialSplitMergeDescending(right, null, right.First, right.Last, comparer); // Merge the two splits into the given list MergeDescending(list, left, right, nodeBeforeStartNode, comparer); // NOTE: only the last merging of two lists is done on the given list for sorting }
public void IndexOfDuplicateNode() { SinglyLinkedList list = new SinglyLinkedList("foo", "bar", "bar", "grille"); Assert.AreEqual(1, list.IndexOf("bar")); }
public void SearchInListWithNonUniqueItems() { int maxItem = 2000; var list = new SinglyLinkedList <int>(); //Adding every seventh number, then every fifth number, //every third and at last all numbers //NOTE: some items are added more than once for (int i = 7; i > 0; i -= 2) { int el = 0; while (el <= maxItem) { list.AddLast(el); el += i; } } list.MergeSort(); for (int i = 0; i <= maxItem; i++) { var fi = list.LinearSearchFirstNode(i); var li = list.LinearSearchLastNode(i); // Calculate number of occurrences of current number int occurNum = 1; if (i % 7 == 0) { occurNum++; } if (i % 5 == 0) { occurNum++; } if (i % 3 == 0) { occurNum++; } int cnt = 0; var curNode = fi; while (curNode != li.Next) { if (curNode.Value != fi.Value) { Assert.Fail(); } if (curNode.Value != i) { Assert.Fail(); } curNode = curNode.Next; cnt++; } if (cnt != occurNum) { Assert.Fail(); } } }
public void IndexOfOnEmptyList() { SinglyLinkedList list = new SinglyLinkedList(); Assert.AreEqual(-1, list.IndexOf("bar")); }
public void Setup() { this._fastList = new DoublyLinkedList <int>(); this._slowList = new SinglyLinkedList <int>(); }
public void IsSortedOnEmptyList() { SinglyLinkedList list = new SinglyLinkedList(); Assert.IsTrue(list.IsSorted()); }
static void Main(string[] args) { Console.WriteLine("\n*********************** March2018 First *****************************\n"); var singlyLikedList = new SinglyLinkedList(); Random rnd = new Random(); for (int i = 0; i < 20; i++) { singlyLikedList.AddLast(rnd.Next(1, 211)); } singlyLikedList.PrintAllNodes(); var current = singlyLikedList.Head; var divisibleThree = new SinglyLinkedList(); var evenNumbers = new SinglyLinkedList(); var oddNumbers = new SinglyLinkedList(); while (current != null) { if (current.data % 3 == 0) { divisibleThree.AddLast(current.data); } else if (current.data % 2 == 0) { evenNumbers.AddLast(current.data); } else { oddNumbers.AddLast(current.data); } current = current.next; } Console.WriteLine("\ndivisibleThree before: "); divisibleThree.PrintAllNodes(); Console.WriteLine("\nevenNumbers before: "); evenNumbers.PrintAllNodes(); Console.WriteLine("\noddNumbers before: "); oddNumbers.PrintAllNodes(); //var orderedList = new SinglyLinkedList(); divisibleThree.Last.next = evenNumbers.First; divisibleThree.Last.next = oddNumbers.First; Console.WriteLine("\ndivisibleThree after: "); divisibleThree.PrintAllNodes(); Console.WriteLine("\n*********************** ExamplesJun2018 Example01 *****************************\n"); Example01 ex = new Example01(); if (ex.IsIsomorphic2("brain", "space")) { Console.WriteLine("IsIsomorphic"); } else { Console.WriteLine("IsNotIsomorphic"); } Console.WriteLine("\n*********************** ExamplesJun2018 Example04 *****************************\n"); var inputLikedList = new SinglyLinkedList(); for (int i = 0; i < 10; i++) { inputLikedList.AddLast(rnd.Next(1, 211)); } Console.WriteLine("Input list before changes: \n"); inputLikedList.PrintAllNodes(); Example04 ex04 = new Example04(); ex04.ReverseNodes(inputLikedList.Head, 2, 4); Console.WriteLine("Input list after changes: \n"); inputLikedList.PrintAllNodes(); Console.WriteLine("\n*********************** November2017 Second *****************************\n"); Novembar2017_Second sec = new Novembar2017_Second(); if (sec.AreAnagram("A@A@", "BAAC")) { Console.WriteLine("Strings are anagram!"); } else { Console.WriteLine("Strings arn't anagram!"); } Console.WriteLine("Press escape for exit!"); ConsoleKeyInfo keyInfo; do { keyInfo = Console.ReadKey(); }while (keyInfo.Key != ConsoleKey.Escape); }
public static string Front(this SinglyLinkedList linkedlist) { return(linkedlist.Head == null ? "null" : linkedlist.Head.Value.ToString()); }
public void IsSortedOnUnsortedList() { SinglyLinkedList list = new SinglyLinkedList("foo", "bar"); Assert.IsFalse(list.IsSorted()); }
/// <summary> /// Removes an element from the <see cref="SplayTree{T}"/>. /// </summary> /// <param name="value">The value to remove.</param> /// <returns>true if the item is successfully removed; otherwise false. Also returns false if item is not found.</returns> public override bool Remove(T value) { if (Root == null) { return(false); } var curNode = Root; var traversedNodes = new SinglyLinkedList <SplayTreeNode <T> >(); bool lastWasLeftSide = true; while (curNode != null) { traversedNodes.AddFirst(curNode); int cmp = comparer.Compare(value, curNode.Value); if (cmp < 0) { curNode = curNode.Left; lastWasLeftSide = true; } else if (cmp > 0) { curNode = curNode.Right; lastWasLeftSide = false; } else// found the value { Count--; // remove the current node from the traversed nodes we don't need it traversedNodes.RemoveFirst(); // deleting the node and replacing it with the min node in the right subtree if (curNode.Right == null) { if (lastWasLeftSide) { if (curNode == Root) { // if node for removal is the root node // no more operations are required Root = curNode.Left; curNode.Invalidate(); return(true); } else { traversedNodes.First.Value.Left = curNode.Left; } } else { traversedNodes.First.Value.Right = curNode.Left; } } else { SplayTreeNode <T> min = null; var rightNode = FindAndRemoveMin(curNode.Right, ref min); min.Right = rightNode; min.Left = curNode.Left; if (lastWasLeftSide) { if (curNode == Root) { // if node for removal is the root node // no more operations are required Root = min; curNode.Invalidate(); return(true); } else { traversedNodes.First.Value.Left = min; } } else { traversedNodes.First.Value.Right = min; } } // Note: if the node for deletion was the root we want be here // because everything is ok and no splaying is needed // When removing a node from the tree we have to // splay the tree for its parent Splay(traversedNodes); curNode.Invalidate(); return(true); } } return(false); }
private static void CheckStateWithSingleNode(SinglyLinkedList <int> list) { Assert.AreEqual(1, list.Count); Assert.IsFalse(list.IsEmpty); Assert.AreSame(list.Head, list.Tail); }
public void IsSortedOnSortedList() { SinglyLinkedList list = new SinglyLinkedList("bar", "foo"); Assert.IsTrue(list.IsSorted()); }