public static void Main() { var list = new DoublyLinkedList<int>(); list.ForEach(Console.WriteLine); Console.WriteLine("--------------------"); list.AddLast(5); list.AddFirst(3); list.AddFirst(2); list.AddLast(10); Console.WriteLine("Count = {0}", list.Count); list.ForEach(Console.WriteLine); Console.WriteLine("--------------------"); list.RemoveFirst(); list.RemoveLast(); list.RemoveFirst(); list.ForEach(Console.WriteLine); Console.WriteLine("--------------------"); list.RemoveLast(); list.ForEach(Console.WriteLine); Console.WriteLine("--------------------"); }
public void AddLastTest() { var list = new DoublyLinkedList<int>(); list.AddLast(2); list.AddLast(4); list.AddLast(6); Assert.AreEqual(list.Head.Value, 2); Assert.AreEqual(list.Tail.Value, 6); }
public void ClearTest() { var list = new DoublyLinkedList<int>(); list.AddLast(2); list.AddLast(4); list.AddLast(6); list.Clear(); Assert.AreEqual(list.Head, null); }
public void Add() { var list = new DoublyLinkedList<int>(2); list.AddFirst(1); list.AddLast(3); list.AddFirst(0); list.AddLast(4); for (var i = 0; i < 5; i++) { Assert.AreEqual(i, list[i].Value); } }
public void AddLast_SeveralElements_ShouldAddElementsCorrectly() { // Arrange var list = new DoublyLinkedList<int>(); // Act list.AddLast(5); list.AddLast(10); list.AddLast(15); // Assert Assert.AreEqual(3, list.Count); var items = new List<int>(); list.ForEach(items.Add); CollectionAssert.AreEqual(items, new List<int>() { 5, 10, 15 }); }
public void BeginTestMethod() { target = new DoublyLinkedList<int>(); Assert.AreEqual(target.Leng, 0); target.Add(2); target.Add(3, 1); target.AddLast(5); }
static void Main(string[] args) { var list = new DoublyLinkedList <int>(); list.ForEach(Console.WriteLine); Console.WriteLine("--------------------"); list.AddLast(5); list.AddFirst(3); list.AddFirst(2); list.AddLast(10); list.AddLast(20); Console.WriteLine("Count = {0}", list.Count); //2 3 5 10 20 list.ForEach(Console.WriteLine); Console.WriteLine("--------------------"); list.RemoveFirst(); list.RemoveLast(); list.RemoveFirst(); //5 10 list.ForEach(Console.WriteLine); Console.WriteLine("-------------------"); Console.WriteLine("Count = {0}", list.Count); list.RemoveLast(); //5 list.ForEach(Console.WriteLine); Console.WriteLine("--------------------"); Console.WriteLine("Count = {0}", list.Count); list.RemoveLast(); //5 10 list.ForEach(Console.WriteLine); }
public void IEnumerable_NonGeneric_MultipleElements() { // Arrange var list = new DoublyLinkedList <object>(); list.AddLast("Five"); list.AddLast(6); list.AddLast(7.77); // Act var enumerator = ((IEnumerable)list).GetEnumerator(); var items = new List <object>(); while (enumerator.MoveNext()) { items.Add(enumerator.Current); } // Assert CollectionAssert.AreEqual(items, new List <object>() { "Five", 6, 7.77 }); }
public void RemoveFirst_SeveralElements_ShouldRemoveElementsCorrectly() { // Arrange var list = new DoublyLinkedList <int>(); list.AddLast(5); list.AddLast(6); list.AddLast(7); // Act var element = list.RemoveFirst(); // Assert Assert.AreEqual(5, element); Assert.AreEqual(2, list.Count); var items = new List <int>(); list.ForEach(items.Add); CollectionAssert.AreEqual(items, new List <int>() { 6, 7 }); }
public void IEnumerable_Foreach_MultipleElements() { // Arrange var list = new DoublyLinkedList <string>(); list.AddLast("Five"); list.AddLast("Six"); list.AddLast("Seven"); // Act var items = new List <string>(); foreach (var element in list) { items.Add(element); } // Assert CollectionAssert.AreEqual(items, new List <string>() { "Five", "Six", "Seven" }); }
void AddItem(IReadOnlyList <int> collection, int item, IReadOnlyList <int> expected) { // Arrange var list = new DoublyLinkedList <int>(collection); var version = list.Version; // Act list.AddLast(item); // Assert list.Count.Should().Be(expected.Count); list.Version.Should().NotBe(version); list.EnumerateForward().Should().Equal(expected); list.EnumerateReversed().Should().Equal(expected.Reverse()); }
void AddNullList() { // Arrange var list = new DoublyLinkedList <int>(); // Act Action action = () => list.AddLast((DoublyLinkedList <int>)null); // Assert action.Should() .ThrowExactly <ArgumentNullException>() .And .ParamName.Should() .Be("list"); }
public void AddLast_EmptyList_ShouldAddElement() { // Arrange var list = new DoublyLinkedList<int>(); // Act list.AddLast(5); // Assert Assert.AreEqual(1, list.Count); var items = new List<int>(); list.ForEach(items.Add); CollectionAssert.AreEqual(items, new List<int>() { 5 }); }
public void ForEach_SingleElement_ShouldEnumerateElementsCorrectly() { var list = new DoublyLinkedList <int>(); list.AddLast(5); var items = new List <int>(); list.ForEach(items.Add); CollectionAssert.AreEqual(items, new List <int>() { 5 }); }
public void WhenMultipleLinkedNodesAreConstructedAndAppended_ThenEachReturnsInForeach() { var initialValues = new[] { 1, 2, 3, 4, 5 }; var appendedValues = new[] { 1, 2, 3, 4, 5 }; var list = new DoublyLinkedList <int>(initialValues); foreach (var appendValue in appendedValues) { list.AddLast(appendValue); } var expectedContents = new[] { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 }; list.Select(x => x.Value).Should().ContainInOrder(expectedContents); }
public void RemoveFirst_OneElement_ShouldMakeListEmpty() { // Arrange var list = new DoublyLinkedList<int>(); list.AddLast(5); // Act var element = list.RemoveFirst(); // Assert Assert.AreEqual(5, element); Assert.AreEqual(0, list.Count); var items = new List<int>(); list.ForEach(items.Add); CollectionAssert.AreEqual(items, new List<int>() { }); }
/// <summary> /// Sequential algorithm. In-place sorting of the given <see cref="DoublyLinkedList{T}"/> elements. Used for descending sort. /// </summary> /// <typeparam name="T">The data type of the <see cref="DoublyLinkedList{T}"/>.</typeparam> /// <param name="list">The <see cref="DoublyLinkedList{T}"/> containing the elements for sorting.</param> /// <param name="startNode">The start <see cref="DoublyLinkedListNode{T}"/> of the current split.</param> /// <param name="endNode">The end <see cref="DoublyLinkedListNode{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>(DoublyLinkedList <T> list, DoublyLinkedListNode <T> startNode, DoublyLinkedListNode <T> endNode, DoublyLinkedListNode <T> nodeAfterEndNode, 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 DoublyLinkedList <T>(); var right = new DoublyLinkedList <T>(); var curNode = startNode; 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 list.Remove(node); // remove the curent node from the list left.AddLast(node); // add the saved node to the left list } else { var node = curNode; // save the current node curNode = curNode.Next; // go to the next node list.Remove(node); // remove the current node from the list right.AddLast(node); // add the saved node to the right list } } // Recursively sort both sublists // NOTE: node after endNode is null because in the left and right splits // we work with all nodes from the list. SequentialSplitMergeDescending(left, left.First, left.Last, null, comparer); SequentialSplitMergeDescending(right, right.First, right.Last, null, comparer); // Merge the two splits into the given list MergeDescending(list, left, right, nodeAfterEndNode, comparer); // NOTE: only the last merging of two lists is done on the given list for sorting }
void AddItem(IReadOnlyList <int> collection, int item, IReadOnlyList <int> expected) { // Arrange var list = new DoublyLinkedList <int>(collection); var version = list.Version; // Act list.AddLast(item); // Assert list.Version.Must() .BeNotEqualTo(version); list.EnumerateForward().Must() .BeEnumerableOf <int>() .BeEqualTo(expected); list.EnumerateReversed().Must() .BeEnumerableOf <int>() .BeEqualTo(expected.Reverse()); }
public void AddFirst_SeveralElements_ShouldAddElementsCorrectly() { // Arrange var list = new DoublyLinkedList <int>(); // Act list.AddLast(10); list.AddFirst(5); list.AddFirst(3); // Assert Assert.AreEqual(3, list.Count); var items = new List <int>(); list.ForEach(items.Add); CollectionAssert.AreEqual(items, new List <int>() { 3, 5, 10 }); }
static void Main(string[] args) { try { DoublyLinkedList<Person> list = new DoublyLinkedList<Person>(); list.AddFirst(new Person("Nikita Vasilyev", 24)); list.AddFirst(new Person("Bill Gates", 59)); list.AddFirst(new Person("Muhhamed Ali", 76)); list.AddLast(new Person("Lennox Lewis", 46)); list.InsertAt(new Person("Steve Jobs", 54), 2); list.Show(); list.RemoveAt(4); Console.WriteLine(list[2]); Person p = new Person("Nikita Vasilyev", 24); Console.WriteLine(list.Find(p)); list.FindLast(p).FullName = "Nikita V. Vasilyev"; list.ShowReverse(); list.Show(); } catch (IndexOutOfRangeException ex) { Console.WriteLine("Message:\t" + ex.Message); Console.WriteLine("Method:\t\t" + ex.TargetSite); } catch (ArgumentNullException ex) { Console.WriteLine("Message:\t" + ex.Message); Console.WriteLine("Method:\t\t" + ex.TargetSite); } catch (InvalidOperationException ex) { Console.WriteLine("Message:\t" + ex.Message); Console.WriteLine("Method:\t\t" + ex.TargetSite); } catch (Exception ex) { Console.WriteLine("Message:\t" + ex.Message); Console.WriteLine("Method:\t\t" + ex.TargetSite); } }
public void CheckIfNodeIsInvalidatedAfterRemoval() { var list = new DoublyLinkedList <int>(); int itemCount = 1000; var firstNode = new DoublyLinkedListNode <int>(0); var lastNode = new DoublyLinkedListNode <int>(0); for (int i = 0; i < itemCount; i++) { lastNode = list.AddLast(i); } firstNode = list.First; list.RemoveFirst(); list.RemoveLast(); int current = list.First.Value; int numberOfElementsInList = 0; foreach (var item in list) { if (current++ != item) { Assert.Fail(); } numberOfElementsInList++; } Assert.IsTrue(list.First != firstNode && firstNode.Next == null && firstNode.List == null && lastNode != list.Last && lastNode.Next == null && lastNode.List == null && numberOfElementsInList == list.Count); }
void AddCollection(IReadOnlyList <int> collection, IReadOnlyList <int> items, bool isMutated, IReadOnlyList <int> expected) { // Arrange var list = new DoublyLinkedList <int>(collection); var version = list.Version; // Act list.AddLast(items); // Assert list.Count.Should().Be(expected.Count); if (isMutated) { list.Version.Should().NotBe(version); } else { list.Version.Should().Be(version); } list.EnumerateForward().Should().Equal(expected); list.EnumerateReversed().Should().Equal(expected.Reverse()); }
static void Main(string[] args) { string repka = "Репка"; var fable = new DoublyLinkedList <string>(); fable.AddFirst(repka); fable.AddLast("Бабка"); fable.AddLast("Дедка"); fable.AddLast("Внучка"); fable.AddLast("Жучка"); fable.AddLast("Кошка"); fable.AddLast("Мышка"); fable.PrintNodes(); fable.Reverse(); fable.PrintNodes(); //Console.WriteLine("Hello World!"); }
public void TestPeeking() { // 5 _list.AddFirst(5); Assert.True(_list.PeekFirst() == 5); Assert.True(_list.PeekLast() == 5); // 6 - 5 _list.AddFirst(6); Assert.True(_list.PeekFirst() == 6); Assert.True(_list.PeekLast() == 5); // 7 - 6 - 5 _list.AddFirst(7); Assert.True(_list.PeekFirst() == 7); Assert.True(_list.PeekLast() == 5); // 7 - 6 - 5 - 8 _list.AddLast(8); Assert.True(_list.PeekFirst() == 7); Assert.True(_list.PeekLast() == 8); // 7 - 6 - 5 _list.RemoveLast(); Assert.True(_list.PeekFirst() == 7); Assert.True(_list.PeekLast() == 5); // 7 - 6 _list.RemoveLast(); Assert.True(_list.PeekFirst() == 7); Assert.True(_list.PeekLast() == 6); // 6 _list.RemoveFirst(); Assert.True(_list.PeekFirst() == 6); Assert.True(_list.PeekLast() == 6); }
public void SortingInDescendingOrderAndCheckingIfSorted() { var list = new DoublyLinkedList <int>(); int minElement = -50000; int maxElement = 50000; int addedElements = 0; //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 = maxElement; while (el >= minElement) { list.AddLast(el); addedElements++; el -= i; } } list.ParallelMergeSortDescending(); var last = int.MaxValue; foreach (var item in list) { if (last < item) { Assert.Fail(); } last = item; } }
public void SortingInAscendingOrderUsingAComparisonAndCheckingIfSorted() { var list = new DoublyLinkedList <int>(); int minElement = -50000; int maxElement = 50000; int addedElements = 0; //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 = minElement; while (el <= maxElement) { list.AddLast(el); addedElements++; el += i; } } list.MergeSort((x, y) => x.CompareTo(y)); var last = int.MinValue; foreach (var item in list) { if (last > item) { Assert.Fail(); } last = item; } }
public void AddingAfterClearingList() { var list = new DoublyLinkedList <int>(); int itemCount = 1000; for (int i = 0; i < itemCount; i++) { list.AddLast(i); } list.Clear(); if (list.Count != 0) { Assert.Fail(); } list.AddFirst(0); Assert.IsTrue(list.Count == 1 && list.First == list.Last && list.First.Value == 0); }
public void Traverse() { var list = new DoublyLinkedList<int>(new[] { 1, 2, 3 }); // Perform various operations list.AddFirst(0); list.RemoveFirst(); list.AddLast(0); list.RemoveLast(); list.Insert(1, 0); list.RemoveAt(1); var current = list.First; while (current.Next != null) { current = current.Next; } Assert.AreEqual(list.Last, current); while (current.Previous != null) { current = current.Previous; } Assert.AreEqual(list.First, current); }
/// <summary> /// Adds an items to the tail of the queue /// </summary> /// <param name="value"></param> public void EnqueueLast(T value) { _Items.AddLast(value); }
public void AddLastNodeTest() { doublyLinkedList.AddLast(Do); doublyLinkedList.AddLast(Re); doublyLinkedList.AddLast(Mi); Assert.IsTrue(Do.Next == Re && Re.Previous == Do); Assert.IsTrue(Re.Next == Mi && Mi.Previous == Re); }
/// <summary> /// Merging two lists into a given <see cref="DoublyLinkedList{T}"/>. Used for descending sort. /// </summary> /// <typeparam name="T">The data type of the <see cref="DoublyLinkedList{T}"/>.</typeparam> /// <param name="mergeList">The <see cref="DoublyLinkedList{T}"/> for merging the left and right lists into.</param> /// <param name="left">The left <see cref="DoublyLinkedList{T}"/> containing some of the elements for sorting.</param> /// <param name="right">The right <see cref="DoublyLinkedList{T}"/> containing some of the elements for sorting.</param> /// <param name="nodeAfterEndNode">The <see cref="DoublyLinkedListNode{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="DoublyLinkedList{T}"/> for the merged elements from the left <see cref="DoublyLinkedList{T}"/> and right <see cref="DoublyLinkedList{T}"/>.</returns> private static void MergeDescending <T>(DoublyLinkedList <T> mergeList, DoublyLinkedList <T> left, DoublyLinkedList <T> right, DoublyLinkedListNode <T> nodeAfterEndNode, IComparer <T> comparer) { bool nodeAfterEndNodeIsNull = nodeAfterEndNode == null; // 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 (nodeAfterEndNodeIsNull) // if the end node is the last in the list { mergeList.AddLast(node); // we add at the end of the list } else// if the end node is not the last { mergeList.AddBefore(nodeAfterEndNode, node);// we add before the node which is after the end node } } else { var node = right.First; right.RemoveFirst(); if (nodeAfterEndNodeIsNull) // if the end node is the last in the list { mergeList.AddLast(node); // we add at the end of the list } else// if the end node is not the last { mergeList.AddBefore(nodeAfterEndNode, node);// we add before the node which is after the end node } } } // add the remaining elements from the left or the right list while (left.Count > 0) { var node = left.First; left.RemoveFirst(); if (nodeAfterEndNodeIsNull) // if the end node is the last in the list { mergeList.AddLast(node); // we add at the end of the list } else// if the end node is not the last { mergeList.AddBefore(nodeAfterEndNode, node);// we add before the node which is after the end node } } while (right.Count > 0) { var node = right.First; right.RemoveFirst(); if (nodeAfterEndNodeIsNull) // if the end node is the last in the list { mergeList.AddLast(node); // we add at the end of the list } else// if the end node is not the last { mergeList.AddBefore(nodeAfterEndNode, node);// we add before the node which is after the end node } } }
public void ForEach_MultipleElements_ShouldEnumerateElementsCorrectly() { // Arrange var list = new DoublyLinkedList<string>(); list.AddLast("Five"); list.AddLast("Six"); list.AddLast("Seven"); // Act var items = new List<string>(); list.ForEach(items.Add); // Assert CollectionAssert.AreEqual(items, new List<string>() { "Five", "Six", "Seven" }); }
public void ToArray_NonEmptyList_ShouldReturnArray() { // Arrange var list = new DoublyLinkedList<string>(); list.AddLast("Five"); list.AddLast("Six"); list.AddLast("Seven"); // Act var arr = list.ToArray(); // Assert CollectionAssert.AreEqual(arr, new string[] { "Five", "Six", "Seven" }); }
public void AddTwoNodesToTail() { doublyLinkedListTest.AddLast(1); doublyLinkedListTest.AddLast(2); Assert.AreEqual(doublyLinkedListTest.GetFirst(), 1, "invalid head element"); Assert.AreEqual(doublyLinkedListTest.GetLast(), 2, "invalid tail element"); }
public void RemoveFirst_SeveralElements_ShouldRemoveElementsCorrectly() { // Arrange var list = new DoublyLinkedList<int>(); list.AddLast(5); list.AddLast(6); list.AddLast(7); // Act var element = list.RemoveFirst(); // Assert Assert.AreEqual(5, element); Assert.AreEqual(2, list.Count); var items = new List<int>(); list.ForEach(items.Add); CollectionAssert.AreEqual(items, new List<int>() { 6, 7 }); }
public void IEnumerable_Foreach_MultipleElements() { // Arrange var list = new DoublyLinkedList<string>(); list.AddLast("Five"); list.AddLast("Six"); list.AddLast("Seven"); // Act var items = new List<string>(); foreach (var element in list) { items.Add(element); } // Assert CollectionAssert.AreEqual(items, new List<string>() { "Five", "Six", "Seven" }); }
public void SearchInListWithNonUniqueItems() { int maxItem = 2000; var list = new DoublyLinkedList <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(); } } }
/// <summary>Adds an item at the end of the Stack</summary> public T AddItem(T value) { values.AddLast(value); return(value); }
public void AddingAfterRemovingAllElements() { var list = new DoublyLinkedList <int>(); int itemCount = 1000; for (int i = 0; i < itemCount; i++) { list.AddLast(i); } for (int i = 0; i < itemCount; i++) { list.RemoveFirst(); } if (list.Count != 0) { Assert.Fail(); } for (int i = 0; i < itemCount; i++) { list.AddFirst(i); } for (int i = 0; i < itemCount; i++) { list.RemoveLast(); } if (list.Count != 0) { Assert.Fail(); } for (int i = 0; i < itemCount; i++) { list.AddLast(i); } for (int i = 0; i < itemCount; i++) { list.RemoveFirst(); } if (list.Count != 0) { Assert.Fail(); } for (int i = 0; i < itemCount; i++) { list.AddLast(i); } for (int i = 0; i < itemCount; i++) { list.RemoveLast(); } if (list.Count != 0) { Assert.Fail(); } list.AddFirst(0); Assert.IsTrue(list.Count == 1 && list.First == list.Last && list.First.Value == 0); }
public void AddLastArrTest(int[] arr, int[] expected) { list.AddLast(arr); int[] actual = list.ToArray(); Assert.AreEqual(expected, actual); }
public void SearchInRangeOfItems() { int itemCount = 2000; var list = new DoublyLinkedList <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 DoublyLinkedListNode <int> startNode = null; DoublyLinkedListNode <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 IEnumerable_NonGeneric_MultipleElements() { // Arrange var list = new DoublyLinkedList<object>(); list.AddLast("Five"); list.AddLast(6); list.AddLast(7.77); // Act var enumerator = ((IEnumerable)list).GetEnumerator(); var items = new List<object>(); while (enumerator.MoveNext()) { items.Add(enumerator.Current); } // Assert CollectionAssert.AreEqual(items, new List<object>() { "Five", 6, 7.77 }); }
public void ForEach_SingleElement_ShouldEnumerateElementsCorrectly() { // Arrange var list = new DoublyLinkedList<int>(); list.AddLast(5); // Act var items = new List<int>(); list.ForEach(items.Add); // Assert CollectionAssert.AreEqual(items, new List<int>() { 5 }); }
/// <summary>Adds an element to the end of the queue</summary> public T Enqueue(T value) { values.AddLast(value); return(value); }