Feature #11: Directory Iterator

Implementing the "Directory Iterator" feature for our "Operating System" project.

Description

In this feature, we will create a directory tree iterator. A directory can contain files or other directories. Similarly, subdirectories can contain both files and other directories. We will be given a directory structure for a specific directory in the file system. This directory will be available as a list. Each element of this list is either a file represented as a scalar element, or a directory represented as a nested list. We will have to iterate over all of the files one by one, using an iterator.

The task is to implement the NestedIterator class:

  • NestedIterator(IList<NestedDirectories> nestedList) initializes the iterator with the nested list nestedList.
  • string Next() returns the next file in the nested directories.
  • boolean hasNext() returns true if there are still some files in the nested list. If there are no files left in the nested list, it returns false.

Solution

We will use a stack to solve this feature. The stack will be used to store the directories and files on the iterator object. In the constructor, we will push all the files and the directories in the stack in reverse order.

The hasNext function checks if the top element of the stack is a file or directory. If so, then it returns true. Otherwise, if the top element is a list of files or directories, then it pops from the stack and pushes each element of the list in the stack in reverse order. This way, the lists at the top of the stack are converted into individual files or a directory whenever the hasNext function is called.

The next function first calls the hasNext function to check if there is any file or directory in the stack. If the hasNext function returns true, then it pops from the stack and returns the topmost value.

Here is an example of how this function works:

Let’s look at the code for this solution:

main.kt
NestedDirectories.kt
public class NestedIterator(nestedList:List<NestedDirectories>) {
var stack: Stack<NestedDirectories> = Stack<NestedDirectories>();
init {
//for ( i in nestedList.size - 1..0)
for ( i in nestedList.indices)
{
stack.push(nestedList[nestedList.size-1-i]);
}
}
fun hasNext():Boolean {
while (stack.size > 0) {
var top = stack.peek();
if (top.isFile())
return true;
var topList = stack.pop().getList();
if(topList != null) {
for (i in topList.indices)
this.stack.push(topList[topList.size-1-i]);
}
}
return false;
}
fun next():String? {
if(hasNext())
return this.stack.pop().getfile();
return null;
}
}
fun main()
{
var nestedList = mutableListOf<NestedDirectories>();
nestedList.add(NestedDirectories("F1"));
var l1 = NestedDirectories(null);
l1.add(NestedDirectories("F2"));
l1.add(NestedDirectories("D1"));
nestedList.add(l1);
nestedList.add(NestedDirectories("D2"));
var itr = NestedIterator(nestedList);
println("Original structure: [F1, [F2, D1], D2]");
print("");
print("Output:");
while(itr.hasNext()){
print("itr.Next(): ");
println(itr.next());
}
}
Directory iterator

Complexity measures

Time complexity Space complexity
O(n+l)O(n + l)
...