static void DepthFirstSearchUsingStack(int vertices, List <int>[] lists) { int rootNode = 1; int currentNode = rootNode; bool[] visitedNodes = new bool[vertices + 1]; SinglyLinkedListStack <int> stack = new SinglyLinkedListStack <int>(); stack.Push(rootNode); while (stack.IsEmpty() == false) { currentNode = stack.Pop(); if (visitedNodes[currentNode] == false) { visitedNodes[currentNode] = true; Console.WriteLine("Odwiedzono wierzchołek " + currentNode); for (int i = 0; i < lists[currentNode].Count; i++) { if (visitedNodes[lists[currentNode][i]] == false) { stack.Push(lists[currentNode][i]); } } } } }
public void TestIsEmpty() { IStack <int> sut = new SinglyLinkedListStack <int>(); Assert.True(sut.IsEmpty()); sut.Push(5); Assert.False(sut.IsEmpty()); }
public void TestPush() { IStack <int> sut = new SinglyLinkedListStack <int>(); Assert.Throws <IndexOutOfRangeException>(() => sut.Top()); sut.Push(3); Assert.Equal(3, sut.Top()); sut.Push(4); sut.Push(5); Assert.Equal(5, sut.Top()); }
public void TestPop() { IStack <int> sut = new SinglyLinkedListStack <int>(); sut.Push(1); sut.Push(2); sut.Push(3); sut.Push(4); Assert.Equal(4, sut.Pop()); Assert.Equal(3, sut.Top()); sut.Pop(); sut.Pop(); Assert.Equal(1, sut.Pop()); Assert.True(sut.IsEmpty()); }