/// <summary> /// Moves the head and grows the snake if growAmount>0, otherwise moves the body /// </summary> public void Move() { if (growAmount > 0) { var headpos = head.transform.position; if (MoveHead()) { GameGrid.instance.grid[(int)headpos.x, (int)headpos.y].data = GameGrid.tileData.obstacle; bodies.AddAfter(bodies.First, Instantiate(bodyPrefab, headpos, Quaternion.identity, transform)); growAmount--; } } else { var headpos = head.transform.position; if (MoveHead()) { if (bodies.Count > 1) { MoveBodies(headpos); } else { GameGrid.instance.grid[(int)head.transform.position.x, (int)head.transform.position.y].data = GameGrid.tileData.empty; } } } }
public void AddAfter(int oldItem, int item) { for (int i = 1; i <= 5; i++) { singlyLinkedList.AddLast(i); } singlyLinkedList.AddAfter(oldItem, item); Assert.AreEqual(singlyLinkedList.Count(), 6, "invalid count"); Assert.IsTrue(singlyLinkedList.Contains(item), "Item doesn't exists."); }
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 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)); }
static void Main(string[] args) { // create integer type singly linked list var list = new SinglyLinkedList <int>(); // add 0~4 to list for (int i = 0; i < 5; i++) { list.Add(new SinglyLinkedListNode <int>(i)); } // delete node index 2 var node = list.GetNode(2); list.Remove(node); // get node index 1 node = list.GetNode(1); // append node value 100 after index 1 node list.AddAfter(node, new SinglyLinkedListNode <int>(100)); // check list count int count = list.Count(); // print all nodes in list for (int i = 0; i < count; i++) { var n = list.GetNode(i); Console.WriteLine(n.Data); } }
public void AddAfterTheMiddleNodeAndCheckForOrder() { var list = new SinglyLinkedList <int>(); int itemCount = 1000; var middleNode = new SinglyLinkedListNode <int>(0); for (int i = 0; i < itemCount; i++) { if (i == itemCount / 2) { middleNode = list.AddLast(i); } else { list.AddLast(i); } } for (int i = 0; i < itemCount; i++) { list.AddAfter(middleNode, i); } int j = 0; int k = 0; int numberOfElementsInList = 0; foreach (var item in list) { if (j <= itemCount / 2) { if (j != item) { Assert.Fail("1"); } } else if (j > itemCount / 2 && j <= itemCount + itemCount / 2) { if (itemCount - 1 - k++ != item) { Assert.Fail("2"); } } else { if (j - itemCount != item) { Assert.Fail("3"); } } j++; numberOfElementsInList++; } Assert.IsTrue(list.Count == 2 * itemCount && numberOfElementsInList == list.Count); }
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 AddAfterItemThatDoesntExist() { SinglyLinkedList list = new SinglyLinkedList(); list.AddLast("foo"); list.AddLast("bar"); list.AddAfter("cat", "grille"); }
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); }
/// Summary /// Time: 2 min 17 sec /// Pattern: AAA, State Relation, Round Trip public void AddAfterTailTest([PexAssumeUnderTest] SinglyLinkedList <string> sll, string newelement) { int prevCount = sll.Count; sll.AddAfter(sll.Tail, newelement); PexAssert.AreEqual(newelement, sll.Tail.Value); //Assert.AreEqual(30, sll.Head.Next.Next.Value); PexAssert.AreEqual(prevCount + 1, sll.Count); }
public void WhatIsElement0() { SinglyLinkedList list = new SinglyLinkedList(); list.AddLast("foo"); list.AddLast("bar"); list.AddAfter("bar", "grille"); //var expected = new string[] { "foo", "bar", "grille" }; CollectionAssert.Equals("foo", list[0]); }
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 AddAfterLastItem() { SinglyLinkedList list = new SinglyLinkedList(); list.AddLast("foo"); list.AddLast("bar"); list.AddAfter("bar", "grille"); var expected = new string[] { "foo", "bar", "grille" }; CollectionAssert.AreEqual(expected, list.ToArray()); }
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 SinglyLinkedListCountIsTen() { SinglyLinkedList <int> list = new SinglyLinkedList <int>(); list.AddFirst(1); for (int i = 2; i < 11; i++) { list.AddAfter(i - 1, i); } Assert.Equal(10, list.Count); }
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 TestAddAfterNoPrevious() { var expected = new int[3] { 0, 1, 2 }; var list = new SinglyLinkedList<int>(); var node = list.AddLast(0); list.AddLast(2); list.AddAfter(node, 1); Verify(expected, list); }
/// Summary /// Time: 5 min 20 sec /// Pattern: AAA, State Relation, Round Trip public void AddAfterMiddleNodeTest([PexAssumeUnderTest] SinglyLinkedList <int> sll, int newelement) { PexAssume.IsTrue(sll.Count > 1); int prevHead = sll.Head.Value; int prevCount = sll.Count; sll.AddAfter(sll.Head.Next, newelement); PexAssert.AreEqual(newelement, sll.Head.Next.Next.Value); PexAssert.AreEqual(prevHead, sll.Head.Value); PexAssert.AreEqual(prevCount + 1, sll.Count); }
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 TestAddAfterLast() { var expected = new int[3] { 0, 1, 2 }; var list = new SinglyLinkedList<int>(); list.AddLast(0); list.AddLast(1); var node = list.AddAfter(list.Last, 2); Verify(expected, list); Assert.AreSame(list.Last, 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); }
/// Summary /// Time: 3 min46 sec /// Pattern: AAAA, State Relation, Round Trip public void AddAfterOnlyOneNodeInListTest([PexAssumeUnderTest] SinglyLinkedList <int> sll, int newelement) { PexAssume.IsTrue(sll.Count == 1); int prevHead = sll.Head.Value; sll.AddAfter(sll.Head, newelement); PexAssert.AreEqual(newelement, sll.Tail.Value); PexAssert.AreEqual(prevHead, sll.Head.Value); PexAssert.AreEqual(newelement, sll.Head.Next.Value); PexAssert.AreEqual(2, sll.Count); }
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 AddNullItem() { var list = new SinglyLinkedList <string>(); list.AddFirst(null); list.AddLast(null); list.AddAfter(null, null); list.AddBefore(null, null); Assert.AreEqual(list.Count(), 4, "invalid count"); list.Remove(null); list.RemoveFirst(); list.RemoveLast(); Assert.AreEqual(list.Count(), 1, "invalid count"); }
public void AddAfter() { SinglyLinkedList list = new SinglyLinkedList(); list.AddLast("foo"); list.AddLast("grille"); // NOTE: This assert isn't necessary. It is merely here to remind you of / verify the state of the list prior to inserting the new node. var expected = new string[] { "foo", "grille" }; CollectionAssert.AreEqual(expected, list.ToArray()); list.AddAfter("foo", "bar"); expected = new string[] { "foo", "bar", "grille" }; CollectionAssert.AreEqual(expected, list.ToArray()); }
public void CanAddAfterSpecificElement() { SinglyLinkedList <int> singlyLinkedList = new SinglyLinkedList <int>(5); singlyLinkedList.AddLast(7); singlyLinkedList.AddLast(13); Assert.AreEqual(5, singlyLinkedList.First.Value); singlyLinkedList.AddAfter(7, 100); string expectedOutput = "5 7 100 13"; string output = singlyLinkedList.ToString().Trim(); Assert.AreEqual(expectedOutput, output); }
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)); }
private static void Run() { var singleyLinkedList = new SinglyLinkedList <int>(); var firstNode = new SinglyLinkedListNode <int>(10); var secondNode = new SinglyLinkedListNode <int>(20); var thirdNode = new SinglyLinkedListNode <int>(30); var forthNode = new SinglyLinkedListNode <int>(40); singleyLinkedList.Add(firstNode); singleyLinkedList.Add(thirdNode); singleyLinkedList.Add(forthNode); singleyLinkedList.AddAfter(firstNode, secondNode); singleyLinkedList.Remove(firstNode); PrintAllElements(singleyLinkedList); }
public void AddAfter() { SinglyLinkedList <int> list = new SinglyLinkedList <int>(); list.AddEnd(0); list.AddEnd(1); list.AddEnd(2); list.AddEnd(3); list.AddAfter(new SinglyLinkedNode <int>(0, new SinglyLinkedNode <int>(1)), 4); int i = 0; /*foreach (var num in list) * { * Assert.True(num == i); * i++; * }*/ Assert.True(list[1] == 4); //Assert.True(list.Count == 5); }
public void AddAfterEmptyNullNodeTest(int elementToAdd) { SinglyLinkedList<int> sll = new SinglyLinkedList<int>(); sll.AddAfter(sll.Head, elementToAdd); }
public void AddAfterEmptyNullNodeTest() { SinglyLinkedList <int> sll = new SinglyLinkedList <int>(); sll.AddAfter(sll.Head, 10); }
/// Summary /// Time: 1 min 31 sec /// Pattern: Constructor Test, Allowed Exception public void AddAfterEmptyNullNodeTest(int elementToAdd) { SinglyLinkedList <int> sll = new SinglyLinkedList <int>(); sll.AddAfter(sll.Head, elementToAdd); }
/// <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 }
/// <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 } }
public void AddAfterEmptyNullNodeTest() { SinglyLinkedList<int> sll = new SinglyLinkedList<int>(); sll.AddAfter(sll.Head, 10); }