Scrolling through folders without recursion

I was looking for an algorithm, so the OS is not a problem, how to scroll through folders without using recursion.

Recursion is not the answer, because recursion cannot go on to "infinity" further, while a "while loop" can get there.

The programming language does not matter.

+3
source share
1 answer

You can use the stack data structure for depth-first traversal. Here is a sample C # code:

    var stack = new Stack<string>();

    stack.Push(@"C:\");

    while (stack.Count > 0)
    {
        var currentDirectory = stack.Pop();
        Console.WriteLine("Visiting: " + currentDirectory);

        foreach (var childDirectory in Directory.GetDirectories(currentDirectory))
        {
            stack.Push(childDirectory);
        }
    }
+4
source

Source: https://habr.com/ru/post/1795987/


All Articles