static void Main(string[] args) { //Linked List //This project gives a better understanding to C# linkedlists //Node and a Pointer to the next node Node firstNode = new Node(1); Node secondNode = new Node(2); secondNode.Next = firstNode; SinglyLinkedList mySList = new SinglyLinkedList(); mySList.AddFirst(1); mySList.AddFirst(2); mySList.AddFirst(3); mySList.Append(10); mySList.Append(20); mySList.Append(30); mySList.InsertNode(11, 4); mySList.DeleteFirst(); mySList.DeleteLast(); mySList.AddFirst(4); mySList.AddLast(40); mySList.Print(); }
public void AddFirst() { var list = new SinglyLinkedList <int>(); Assert.Equal(0, list.Count); Assert.Null(list.First); Assert.Null(list.Last); list.AddFirst(1); Assert.Equal(1, list.Count); Assert.NotNull(list.First); Assert.NotNull(list.Last); Assert.Same(list.First, list.Last); Assert.Null(list.First !.Next); Assert.Equal(1, list.First); list.AddFirst(2); Assert.Equal(2, list.Count); Assert.NotNull(list.First); Assert.NotNull(list.Last); Assert.NotSame(list.First, list.Last); Assert.Same(list.First.Next, list.Last); Assert.Null(list.Last !.Next); Assert.Equal(2, list.First); Assert.Equal(1, list.Last); }
public void AddFirst_And_AddLast_OneItem_CorrectState() { _list.AddFirst(1); CheckStateWithSingleNode(_list); _list.RemoveFirst(); _list.AddLast(1); CheckStateWithSingleNode(_list); }
/// <summary> /// Removes a word from the <see cref="SuffixTreeMap{TValue}"/>. /// </summary> /// <param name="word">The word to remove.</param> /// <returns>true if the word is successfully removed; otherwise false. Also returns false if word is not found.</returns> public bool Remove(string word) { var curNode = Root; bool removedSuccessfully = true; if (!ContainsWord(word)) { return(false); } var nodesList = new SinglyLinkedList <SuffixTreeMapNode <TValue> >(); nodesList.AddFirst(curNode); for (int i = word.Length - 1; i >= 0; i--) { curNode = curNode.children[word[i]]; nodesList.AddFirst(curNode); } if (nodesList.First.Value.children.Count > 0) { nodesList.First.Value.IsTerminal = false; Count--; return(true); } var curListNode = nodesList.First; while (curListNode.Next != null) { if (curListNode.Value.children.Count == 0) { if (!curListNode.Next.Value.children.Remove(curListNode.Value.Key)) { removedSuccessfully = false; } curListNode = curListNode.Next; } else { break; } } if (removedSuccessfully) { Count--; } return(removedSuccessfully); }
/// <summary> /// Adds an element with the specified key and value into the <see cref="SplayTreeMap{TKey, TValue}"/>. /// </summary> /// <param name="key">The key of the element to add.</param> /// <param name="value">The value of the element to add.</param> public override void Add(TKey key, TValue value) { if (Root == null) { Root = new SplayTreeMapNode <TKey, TValue>(key, value); Count++; return; } var curNode = Root; bool addToLeftSide = true; var traversedNodes = new SinglyLinkedList <SplayTreeMapNode <TKey, TValue> >(); while (curNode != null) { traversedNodes.AddFirst(curNode); int cmp = comparer.Compare(key, curNode.Key); if (cmp < 0) { curNode = curNode.Left; addToLeftSide = true; } else if (cmp > 0) { curNode = curNode.Right; addToLeftSide = false; } else { throw new ArgumentException("Tried to insert duplicate value!"); } } if (addToLeftSide) { var newNode = new SplayTreeMapNode <TKey, TValue>(key, value); traversedNodes.First.Value.Left = newNode; traversedNodes.AddFirst(newNode); Count++; } else { var newNode = new SplayTreeMapNode <TKey, TValue>(key, value); traversedNodes.First.Value.Right = newNode; traversedNodes.AddFirst(newNode); Count++; } //Have to splay tree after adding Splay(traversedNodes); }
public void AddFirstTest() { SinglyLinkedList <int> sll = new SinglyLinkedList <int>(); sll.AddFirst(10); sll.AddFirst(20); sll.AddFirst(30); Assert.AreEqual(30, sll.Head.Value); Assert.AreEqual(20, sll.Head.Next.Value); Assert.AreEqual(10, sll.Tail.Value); }
public void Add_First_And_AddLast_Correct() { _list.AddFirst(1); CheckSinglyLinkedListWithSingleState(_list); _list.RemoveFirst(); _list.AddLast(1); CheckSinglyLinkedListWithSingleState(_list); }
public static void GetElementByIndex_IndexOutOfRange_ArgumentOutOfRangeExceptionThrown() { // Arrange var list = new SinglyLinkedList <int>(); // Act _ = list.AddFirst(1); _ = list.AddFirst(2); _ = list.AddFirst(3); // Assert _ = Assert.Throws <ArgumentOutOfRangeException>(() => list.GetElementByIndex(-1)); _ = Assert.Throws <ArgumentOutOfRangeException>(() => list.GetElementByIndex(3)); }
public void Add() { // Arrange var suit = new SinglyLinkedList <int>(); // Act suit.AddFirst(3); suit.AddFirst(2); suit.AddFirst(1); // Assert Assert.Equal(3, suit.Count); Assert.Equal(0, suit.IndexOf(1)); Assert.Equal(1, suit.IndexOf(2)); Assert.Equal(2, suit.IndexOf(3)); }
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 AddFirstForAllElementsAndCheckForOrder() { var list = new SinglyLinkedList <int>(); int itemCount = 1000; for (int i = 0; i < itemCount; i++) { list.AddFirst(i); } int previous = itemCount; int numberOfElementsInList = 0; foreach (var item in list) { if (previous < item) { Assert.Fail(); } previous = item; numberOfElementsInList++; } Assert.IsTrue(list.Count == itemCount && numberOfElementsInList == list.Count); }
public void RemoveFirstTest() { SinglyLinkedList <int> sll = new SinglyLinkedList <int>(); sll.AddFirst(40); sll.AddFirst(30); sll.AddFirst(20); sll.AddFirst(10); sll.AddLast(50); sll.RemoveFirst(); sll.RemoveFirst(); Assert.AreEqual(30, sll.Head.Value); Assert.AreEqual(50, sll.Tail.Value); Assert.AreEqual(3, sll.Count); }
public void AddFirstNodeValueSuccessCases(int[] testCase) { SinglyLinkedList <int> list = new SinglyLinkedList <int>(); foreach (int value in testCase) { list.AddFirst(new Node <int>(value)); } Assert.AreEqual(testCase.Length, list.Count, "There was an unexpected number of list items"); Assert.AreEqual(testCase.Last(), list.Head.Value, "The first item value was incorrect"); Assert.AreEqual(testCase.First(), list.Tail.Value, "The last item value was incorrect"); int[] reversed = testCase.Reverse().ToArray(); int current = 0; foreach (int value in list) { Assert.AreEqual(reversed[current], value, "The list value at index {0} was incorrect.", current); current++; } }
/// <summary> /// Determines whether an element with the specified key is contained in the <see cref="SplayTreeMap{TKey, TValue}"/> and then it splays it. /// </summary> /// <param name="key">The key to check.</param> /// <returns>true if key is found; otherwise false.</returns> public override bool ContainsKey(TKey key) { if (Root == null) { return(false); } var curNode = Root; var traversedNodes = new SinglyLinkedList <SplayTreeMapNode <TKey, TValue> >(); while (curNode != null) { traversedNodes.AddFirst(curNode); int cmp = comparer.Compare(key, curNode.Key); if (cmp < 0) { curNode = curNode.Left; } else if (cmp > 0) { curNode = curNode.Right; } else// found the value { // have to splay for the searched node Splay(traversedNodes); return(true); } } return(false); }
public void AddFirst_Should_change_head() { ISinglyLinkedList <int> list = new SinglyLinkedList <int>(new[] { 1 }); list = list.AddFirst(2); Assert.That(list.Head.Data, Is.EqualTo(2)); }
public void testIndexOfNodeInFirst() { SinglyLinkedList list = new SinglyLinkedList(); list.AddFirst("node1"); Assert.AreEqual(0, list.IndexOf("node1")); }
public void InitializeListFromIEnumeraable() { var list = new SinglyLinkedList <int>(); int itemCount = 1000; for (int i = 0; i < itemCount; i++) { list.AddFirst(i); } var list2 = new SinglyLinkedList <int>(list); if (list.Count != list2.Count) { Assert.Fail(); } foreach (var item in list) { if (item != list2.First.Value) { Assert.Fail(); } list2.RemoveFirst(); } Assert.IsTrue(list.Count == itemCount && list2.Count == 0); }
public void AddFirstOnEmptyList() { SinglyLinkedList list = new SinglyLinkedList(); list.AddFirst("foo"); string[] expected = new string[] { "foo" }; CollectionAssert.AreEqual(expected, list.ToArray()); }
public void SinglyLinkedListCountIsOne() { SinglyLinkedList <int> list = new SinglyLinkedList <int>(); list.AddFirst(3); Assert.Equal(1, list.Count); }
public void CanAddFirst() { SinglyLinkedList <int> singlyLinkedList = new SinglyLinkedList <int>(5); singlyLinkedList.AddFirst(7); Assert.AreEqual(7, singlyLinkedList.First.Value); }
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 InsertAfter() { ISinglyLinkedList <int> list = new SinglyLinkedList <int>(); list.AddFirst(new SinglyLinkedListNode <int>(22)); list.AddAfter(22, new SinglyLinkedListNode <int>(1)); Assert.AreEqual(list.FindLastNode().Value, 1); }
public void testIndexOfNodeForMultipleItems() { SinglyLinkedList list = new SinglyLinkedList(); list.AddFirst("node1"); list.AddLast("node2"); Assert.AreEqual(1, list.IndexOf("node2")); }
public override void Push(T item) { if (!IsValidObject(item)) { throw new ArgumentException(nameof(item)); } items.AddFirst(item); }
public void AddFirst_ToNonEmptyList_Success(int nodeValue) { ISinglyLinkedList <int> singlyLinkedList = new SinglyLinkedList <int>(-1); singlyLinkedList.AddFirst(nodeValue); Assert.Equal(nodeValue, singlyLinkedList.GetFirst()); Assert.Equal(-1, singlyLinkedList.GetLast()); }
/// <summary> /// Determines whether an element with the specified value is contained in the <see cref="SplayTreeMap{TKey, TValue}"/> and then it splays it. /// </summary> /// <param name="value">The value to check.</param> /// <returns>true if value is found; otherwise false.</returns> public override bool ContainsValue(TValue value) { if (Root == null) { return(false); } // Pre order traversal of the tree, because we need to get the traversed nodes if values is found HashSet <SplayTreeMapNode <TKey, TValue> > cleared = new HashSet <SplayTreeMapNode <TKey, TValue> >(); SinglyLinkedList <SplayTreeMapNode <TKey, TValue> > sList = new SinglyLinkedList <SplayTreeMapNode <TKey, TValue> >(); sList.AddFirst(Root); while (sList.Count > 0) { SplayTreeMapNode <TKey, TValue> curNode = sList.First.Value; if (object.Equals(value, curNode.Value)) { // have to splay for the searched node Splay(sList); return(true); } if (curNode.Left == null || cleared.Contains(curNode.Left)) { if (curNode.Right == null || cleared.Contains(curNode.Right)) { cleared.Add(curNode); sList.RemoveFirst(); } else { sList.AddFirst(curNode.Right); } } else { sList.AddFirst(curNode.Left); } } return(false); }
public void TestAddFirst() { int[] values = new int[] { 5, 25, 1, -9, 18, 26, 25, 2, 9, 3, 7 }; SinglyLinkedList <int> list = new SinglyLinkedList <int>(values); list.AddFirst(99); Assert.IsTrue(list.SequenceEqual(Enumerable.Repeat(99, 1).Concat(values))); }
/// Summary /// Time: 2 min 11 sec /// Pattern: State Relation, AAAA /// Combines two unit tests into one public void RemoveOnlyNodeInListTest([PexAssumeUnderTest] SinglyLinkedList <int> actual, int newNode) { PexAssume.IsTrue(actual.Count == 0); actual.AddFirst(newNode); actual.Remove(newNode); PexAssert.IsNull(actual.Head); PexAssert.IsNull(actual.Tail); }
public void CheckIfContainedAfterAddingAndAfterRemoving() { var list = new SinglyLinkedList <int>(); int itemCount = 1000; for (int i = 0; i < itemCount; i++) { if (!list.Contains(i)) { list.AddLast(i); } } if (list.Count != itemCount) { Assert.Fail(); } for (int i = 0; i < itemCount; i++) { if (list.Contains(i)) { list.RemoveFirst(); } } if (list.Count != 0) { Assert.Fail(); } for (int i = 0; i < itemCount; i++) { if (!list.Contains(i)) { list.AddFirst(i); } } if (list.Count != itemCount) { Assert.Fail(); } for (int i = 0; i < itemCount; i++) { if (list.Contains(i)) { list.RemoveLast(); } } Assert.IsTrue(list.Count == 0); }
public void RemovedFirstSecondIsHead() { SinglyLinkedList <int> list = new SinglyLinkedList <int>(); for (int i = 10; i > 0; i--) { list.AddFirst(i); } list.RemoveFirst(); Assert.Equal(2, list.GetHead().Item); }
public void AddFirstOnLongerList() { SinglyLinkedList list = new SinglyLinkedList(); list.AddLast("foo"); list.AddLast("bar"); list.AddFirst("grille"); var expected = new string[] { "grille", "foo", "bar" }; CollectionAssert.AreEqual(expected, list.ToArray()); }
public void Adding_Should_increment_length() { ISinglyLinkedList<int> list = new SinglyLinkedList<int>(new[] { 1 }); Assert.That(list.Length, Is.EqualTo(1)); list = list.AddAfter(2,list.Tail); Assert.That(list.Length, Is.EqualTo(2)); list = list.AddFirst(3); Assert.That(list.Length, Is.EqualTo(3)); list = list.AddLast(4); Assert.That(list.Length, Is.EqualTo(4)); }
public void AddFirst_Should_change_head() { ISinglyLinkedList<int> list = new SinglyLinkedList<int>(new[] { 1 }); list = list.AddFirst(2); Assert.That(list.Head.Data, Is.EqualTo(2)); }
public void AddFirstTest() { SinglyLinkedList<int> sll = new SinglyLinkedList<int>(); sll.AddFirst(10); sll.AddFirst(20); sll.AddFirst(30); Assert.AreEqual(30, sll.Head.Value); Assert.AreEqual(20, sll.Head.Next.Value); Assert.AreEqual(10, sll.Tail.Value); }
public void TestRemoveFirst() { var expected = new int[2] { 0, 1 }; var list = new SinglyLinkedList<int>(expected); list.AddFirst(-1); Assert.AreEqual(-1, list.First.Value); list.RemoveFirst(); Verify(expected, list); }
public void TestAddFirst() { var expected = new int[2] { 0, 1 }; var list = new SinglyLinkedList<int>(); list.AddFirst(1); list.AddFirst(0); Verify(expected, list); }
public void RemoveFirstTest() { SinglyLinkedList<int> sll = new SinglyLinkedList<int>(); sll.AddFirst(40); sll.AddFirst(30); sll.AddFirst(20); sll.AddFirst(10); sll.AddLast(50); sll.RemoveFirst(); sll.RemoveFirst(); Assert.AreEqual(30, sll.Head.Value); Assert.AreEqual(50, sll.Tail.Value); Assert.AreEqual(3, sll.Count); }