Exemple #1
0
    public static void Main(string[] args)
    {
        StackLinkedList <string> stackLinkedList = new StackLinkedList <string>();

        Console.WriteLine($"Size: {stackLinkedList.Size()}");
        Console.WriteLine($"IsEmpty: {stackLinkedList.IsEmpty()}");

        Console.WriteLine();

        stackLinkedList.Push("John");
        stackLinkedList.Push("Sarah");
        stackLinkedList.Push("Mario");

        Console.WriteLine();

        Console.WriteLine($"Size: {stackLinkedList.Size()}");
        Console.WriteLine($"IsEmpty: {stackLinkedList.IsEmpty()}");

        Console.WriteLine();

        Console.WriteLine($"Pop: {stackLinkedList.Pop().ToString()}");
        Console.WriteLine($"Peek: {stackLinkedList.Peek().ToString()}");
        Console.WriteLine($"Pop: {stackLinkedList.Pop().ToString()}");
        Console.WriteLine($"Peek: {stackLinkedList.Peek().ToString()}");

        Console.WriteLine();

        Console.WriteLine($"Size: {stackLinkedList.Size()}");

        // Expected Output
        // ------------------
        // Size: 0
        // IsEmpty: True
        //
        // Push: John
        // Push: Sarah
        // Push: Mario
        //
        // Size: 3
        // IsEmpty: False
        //
        // Pop: Mario
        // Peek: Sarah
        // Pop: Sarah
        // Peek: John
        //
        // Size: 1
    }
        public void IsEmpty_EmptyStackShouldReturnTrue()
        {
            // Arrange
            StackLinkedList <int> sut = new StackLinkedList <int>();
            bool expected             = true;

            // Act
            bool actual = sut.IsEmpty();

            // Assert
            Assert.Equal(expected, actual);
        }
        public void Clear_ClearStackWithTwoNumbersShouldWork()
        {
            // Arrange
            StackLinkedList <int> sut = new StackLinkedList <int>();
            bool expected             = true;

            // Act
            sut.Add(int.MaxValue);
            sut.Add(int.MinValue);
            sut.Clear();
            bool actual = sut.IsEmpty();

            // Assert
            Assert.Equal(expected, actual);
        }
Exemple #4
0
        /// <summary>
        /// Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures.
        /// Finds all vertices that can be reached by the starting vertex.
        /// </summary>
        /// <param name="rootVertex"></param>
        /// <param name="previsit"></param>
        /// <returns></returns>
        public HashSet <T> DepthFirstSearch(T rootVertex, Action <T> preVisit = null)
        {
            // Traversed graph information
            var visitedVerticesInfo = new HashSet <T>();

            if (!_aList.ContainsKey(rootVertex))
            {
                return(visitedVerticesInfo);
            }

            // Create a stack and add root vertex
            var stack = new StackLinkedList <T>();

            stack.Push(rootVertex);

            // As long as the stack is not empty:
            while (!stack.IsEmpty())
            {
                // Repeatedly pop a vertex u from the queue.
                var vertex = stack.Pop();

                // Ignore if neigbor is already visited
                if (visitedVerticesInfo.Contains(vertex))
                {
                    continue;
                }

                // Trace the path
                preVisit?.Invoke(vertex);

                // Add vertex info to the visited list
                visitedVerticesInfo.Add(vertex);

                // For each neighbor v of u that has not been visited:
                foreach (var neighbor in _aList[vertex])
                {
                    if (!visitedVerticesInfo.Contains(neighbor))
                    {
                        // Push v
                        stack.Push(neighbor);
                    }
                }
            }

            return(visitedVerticesInfo);
        }