public void TestCase1() { Program1.DoublyLinkedList linkedList = new Program1.DoublyLinkedList(); Program1.Node one = new Program1.Node(1); // Program1.Node two = new Program1.Node(2); // Program1.Node three = new Program1.Node(3); // Program1.Node four = new Program1.Node(4); // Program1.Node five = new Program1.Node(5); // Program1.Node six = new Program1.Node(6); // Program1.Node seven = new Program1.Node(7); linkedList.SetHead(one); linkedList.Remove(one); // linkedList.InsertAfter(one, two); // linkedList.InsertAfter(two, three); // linkedList.InsertAfter(three, four); // linkedList.InsertAfter(four, five); // linkedList.InsertAfter(five, six); // linkedList.InsertAfter(six, seven); // linkedList.InsertAtPosition(7,one); // linkedList.InsertAtPosition(1,one); // linkedList.InsertAtPosition(2,one); // linkedList.InsertAtPosition(3,one); // linkedList.InsertAtPosition(4,one); // linkedList.InsertAtPosition(2,one); // linkedList.InsertAtPosition(6,one); var forward = getNodeValuesHeadToTail(linkedList); var reversed = getNodeValuesTailToHead(linkedList); // 2-1-3-4-5-6-7 2-3-4-5-6-1-7 // Assert.True(compare(getNodeValuesHeadToTail(linkedList), new int[] {4, 1, 2, 3, 5})); // Assert.True(compare(getNodeValuesTailToHead(linkedList), new int[] {5, 3, 2, 1, 4})); // Assert.True(linkedList.ContainsNodeWithValue(5)); }
private List <int> getNodeValuesTailToHead(Program1.DoublyLinkedList linkedList) { List <int> values = new List <int>(); Program1.Node node = linkedList.Tail; while (node != null) { values.Add(node.Value); node = node.Prev; } return(values); }
private List <int> getNodeValuesHeadToTail(Program1.DoublyLinkedList linkedList) { List <int> values = new List <int>(); Program1.Node node = linkedList.Head; while (node != null) { values.Add(node.Value); node = node.Next; } return(values); }
public void TestCase1() { Program1.DoublyLinkedList linkedList = new Program1.DoublyLinkedList(); Program1.Node one = new Program1.Node(1); Program1.Node two = new Program1.Node(2); Program1.Node three = new Program1.Node(3); Program1.Node three2 = new Program1.Node(3); Program1.Node three3 = new Program1.Node(3); Program1.Node four = new Program1.Node(4); Program1.Node five = new Program1.Node(5); Program1.Node six = new Program1.Node(6); bindNodes(one, two); bindNodes(two, three); bindNodes(three, four); bindNodes(four, five); linkedList.Head = one; linkedList.Tail = five; linkedList.SetHead(four); Assert.True(compare(getNodeValuesHeadToTail(linkedList), new int[] { 4, 1, 2, 3, 5 })); Assert.True(compare(getNodeValuesTailToHead(linkedList), new int[] { 5, 3, 2, 1, 4 })); linkedList.SetTail(six); Assert.True(compare(getNodeValuesHeadToTail(linkedList), new int[] { 4, 1, 2, 3, 5, 6 })); Assert.True(compare(getNodeValuesTailToHead(linkedList), new int[] { 6, 5, 3, 2, 1, 4 })); linkedList.InsertBefore(six, three); Assert.True(compare(getNodeValuesHeadToTail(linkedList), new int[] { 4, 1, 2, 5, 3, 6 })); Assert.True(compare(getNodeValuesTailToHead(linkedList), new int[] { 6, 3, 5, 2, 1, 4 })); linkedList.InsertAfter(six, three2); Assert.True(compare(getNodeValuesHeadToTail(linkedList), new int[] { 4, 1, 2, 5, 3, 6, 3 })); Assert.True(compare(getNodeValuesTailToHead(linkedList), new int[] { 3, 6, 3, 5, 2, 1, 4 })); linkedList.InsertAtPosition(1, three3); Assert.True(compare(getNodeValuesHeadToTail(linkedList), new int[] { 3, 4, 1, 2, 5, 3, 6, 3 })); Assert.True(compare(getNodeValuesTailToHead(linkedList), new int[] { 3, 6, 3, 5, 2, 1, 4, 3 })); linkedList.RemoveNodesWithValue(3); Assert.True(compare(getNodeValuesHeadToTail(linkedList), new int[] { 4, 1, 2, 5, 6 })); Assert.True(compare(getNodeValuesTailToHead(linkedList), new int[] { 6, 5, 2, 1, 4 })); linkedList.Remove(two); Assert.True(compare(getNodeValuesHeadToTail(linkedList), new int[] { 4, 1, 5, 6 })); Assert.True(compare(getNodeValuesTailToHead(linkedList), new int[] { 6, 5, 1, 4 })); Assert.True(linkedList.ContainsNodeWithValue(5)); }
private void bindNodes(Program1.Node nodeOne, Program1.Node nodeTwo) { nodeOne.Next = nodeTwo; nodeTwo.Prev = nodeOne; }