public void Should_Delete_Node() { var original = new LinkedNodeList <int>(new[] { -3, 5, -99 }); var expected = new LinkedNodeList <int>(new[] { 5, -99 }); LinkedNodeUtilities.DeleteNode(original.Head, -3).Should().BeEquivalentTo(expected.Head); }
public void Should_Reverse_LinkedNodeList() { var original = new LinkedNodeList <int>(new[] { 1, 2, 3, 4, 5 }); var reversed = new LinkedNodeList <int>(new[] { 5, 4, 3, 2, 1 }); original.Reverse(); original.Should().BeEquivalentTo(reversed); }
public void TestMultiConstruct() { INodeList nodeListToAdd = new LinkedNodeList(new Node()); nodeListToAdd.Add(new Node()); nodeListToAdd.Add(new Node()); INodeList nodeList = new LinkedNodeList(nodeListToAdd); Assert.That(nodeList.Length, Is.EqualTo(3)); }
public void Should_Get_Common_Node() { var a = new LinkedNodeList <int>(new[] { 1, 2, 3 }); var b = new LinkedNodeList <int>(new[] { 4, 5 }); a.Last().Next = b.Head; LinkedNodeUtilities.CommonNodeOfTwoLinkedList(a.Head, b.Head).Should().BeEquivalentTo(b.Head); LinkedNodeUtilities.GetIntersectionNode(a.Head, b.Head).Should().BeEquivalentTo(b.Head); }
public void Should_Remove_Target_Node_Without_List() { var original = new LinkedNodeList <int>(new[] { 1, 2, 3, 4, 5 }); var expected = new LinkedNodeList <int>(new[] { 1, 2, 3, 4 }); var target = original.Last(); var res = LinkedNodeUtilities.RemoveTargetNode(original.Head, target); res.Should().BeEquivalentTo(expected.Head); }
public void Should_Remove_Target_Tail() { var original = new LinkedNodeList <int>(new[] { 1, 2, 3, 4, 5 }); var expected = new LinkedNodeList <int>(new[] { 1, 2, 3, 4 }); var target = original.Head.Next.Next.Next.Next; original.RemoveTargetNode(target); original.Should().BeEquivalentTo(expected); }
public void Should_RecursiveReverse_LinkedNodeHead() { var original = new LinkedNodeList <int>(new[] { 1, 2, 3, 4, 5 }); var reversed = new LinkedNodeList <int>(new[] { 5, 4, 3, 2, 1 }); var res = LinkedNodeUtilities.RecursiveReserve(original.Head); res.Should().BeEquivalentTo(reversed.Head); }
public void Should_Get_Kth_From_Tail() { var input = new LinkedNodeList <int>(new[] { 1, 2, 3, 4, 5 }); var expected = new LinkedNodeList <int>(new[] { 4, 5 }); LinkedNodeUtilities.GetKthFromEnd(input.Head, 2).Should().BeEquivalentTo(expected.Head); LinkedNodeUtilities.GetKthFromEnd(input.Head, 5).Should().BeEquivalentTo(input.Head); LinkedNodeUtilities.GetKthFromEnd(input.Head, 1).Should().BeEquivalentTo(input.Last()); }
public void Should_Notice_Circled_LinkedNodeList() { var listWithoutCycle = new LinkedNodeList <int>(new[] { 1, 2, 3, 4, 5 }); listWithoutCycle.IsCircled().Should().BeFalse(); var listWithCycle = new LinkedNodeList <int>(new[] { 1, 2, 3, 4, 5 }); listWithCycle.Last().Next = listWithCycle.Head; listWithCycle.IsCircled().Should().BeTrue(); LinkedNodeUtilities.IsCircled(listWithCycle.Head).Should().BeTrue(); }
public void Should_Notice_Circled_LinkedNodeHead() { var headWithoutCycle = new LinkedNodeList <int>(new[] { 1, 2, 3, 4, 5 }).Head; var headWithCycle = new LinkedNode <int>(2); var next = new LinkedNode <int>(3); headWithCycle.Next = next; next.Next = headWithCycle; LinkedNodeUtilities.IsCircled(headWithoutCycle).Should().BeFalse(); LinkedNodeUtilities.IsCircled(headWithCycle).Should().BeTrue(); }
public void Should_Append_New_Node() { var res = new LinkedNodeList <int>(); res.AddLast(3); res.Head.Value.Should().Be(3); res.Head.Next.Should().BeNull(); res.AddLast(5); res.Head.Value.Should().Be(3); res.Head.Next.Value.Should().Be(5); }
public void Should_Reverse_Print() { var input = new LinkedNodeList <int>(new[] { 1, 2, 3, 4, 5 }); var expected = new [] { 5, 4, 3, 2, 1 }; var withStack = LinkedNodeUtilities.ReversePrintWithStack(input.Head); withStack.Should().BeEquivalentTo(expected); var noStack = LinkedNodeUtilities.ReversePrintWithStack(input.Head); noStack.Should().BeEquivalentTo(expected); }
public void Should_Throw_Exception_For_Invalid_Kth_From_Tail() { var input = new LinkedNodeList <int>(new[] { 1, 2, 3, 4, 5 }); var ex = Assert.Throws <Exception>(() => LinkedNodeUtilities.GetKthFromEnd(input.Head, 6)); ex.Message.Should().Be("Invalid k is larger than linked list length"); var ex2 = Assert.Throws <Exception>(() => LinkedNodeUtilities.GetKthFromEnd(input.Head, 0)); ex2.Message.Should().Be("K should be larger than 0"); LinkedNodeUtilities.GetKthFromEnd <int>(null, 0).Should().BeNull(); }
private Dictionary <long, long> GetPlayerScores2(int numPlayers, int lastMarbleScore) { Dictionary <long, long> playerScores = new Dictionary <long, long>(); for (int i = 0; i < numPlayers; i++) { playerScores.Add(i, 0); } int score = 0; int marble = 0; int playerTurn = 0; LinkedNode current = LinkedNodeList.AddFirst(marble); marble++; while (marble != lastMarbleScore) { LinkedNode nextNode = current.Next; current = LinkedNodeList.AddAfter(nextNode, marble); marble++; playerTurn++; playerTurn = playerTurn % numPlayers; if (marble % 23 == 0) { for (int i = 0; i < 7; i++) { current = current.Previous; } score = marble + current.Value; nextNode = current.Next; LinkedNodeList.Remove(current); current = nextNode; playerScores[playerTurn] += score; marble++; playerTurn++; playerTurn = playerTurn % numPlayers; } } return(playerScores); }
private void DrawButton_Click(object sender, EventArgs e) { try { string regularExpString = regularExpTextbox.Text; regularExpString = regularExpString.Replace(" ", ""); if (regularExpString == "") { // nothing :) } else { list = new LinkedNodeList(); ParseRegularExpression(regularExpString); list.SynchroniseToRenderLibrary(); RenderENFA(); } } catch (Exception ex) { MessageBox.Show("RE çözümlenirken bir hata oluştu!\n\n" + ex.ToString(), "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
static void Main(string[] args) { //Part A - Puzzle One - Linked List Console.WriteLine("Part A - Puzzle One - Linked List"); LinkedNodeList numberList = new LinkedNodeList(new LinkedNode(0)); numberList.Add(new LinkedNode(1)); numberList.Add(new LinkedNode(2)); numberList.Add(new LinkedNode(3)); numberList.Add(new LinkedNode(4)); numberList.Add(new LinkedNode(5)); numberList.Add(new LinkedNode(6)); numberList.Add(new LinkedNode(7)); numberList.Add(new LinkedNode(8)); numberList.Add(new LinkedNode(9)); numberList.Add(new LinkedNode(10)); numberList.Add(new LinkedNode(11)); Console.WriteLine("Singly Linked List elements:"); numberList.DisplayAll(); int n = 5; numberList.GetNthElementFromTail(n); //Part A - Puzzle Two - Reverse Words Console.WriteLine("Part A - Puzzle Two - Reverse Words"); string strSentence = "Hello! My name is 'Neeraj Joshi' and I'm doing good. How are you doing?"; Console.WriteLine("Input String : " + strSentence); string modifiedSentence = ReverseCharInWords(strSentence); Console.WriteLine("Output String : " + modifiedSentence); Console.ReadKey(); }
/// <summary> /// Removes all references to this Node in Parents, being a new Tree-top afterwards. /// </summary> /// <remarks>This function does not always have a loop-detection.</remarks> public virtual void Split() { NodeSplittedEventHandler handler; LinkedNodeList nodeList; lock (childNodesSplittedLock) { handler = nodeSplitted; } if (handler != null) { nodeList = new LinkedNodeList(); nodeList.Add(parents); splitFromParents(); OnNodeSplitted(new NodeSplittedEventArgs(parents)); } else splitFromParents(); }
/// <summary> /// Default contructor for Node. /// </summary> public Node() { parents = new LinkedNodeList(); nodes = new LinkedNodeList(); }
public void TestSingleConstruct() { INodeList nodeList = new LinkedNodeList(new Node()); Assert.That(nodeList.Length, Is.EqualTo(1)); }
public void Should_Find_Middle_Value() { var head = new LinkedNodeList <int>(new[] { 1, 2, 3, 4, 5 }).Head; LinkedNodeUtilities.MiddleValue(head).Should().Be(3); }
public void TestConstruct() { INodeList nodeList = new LinkedNodeList(); Assert.That(nodeList.Length, Is.EqualTo(0)); }