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.swift
NestedDirectories.swift
Stack.swift
import Swift
class NestedIterator {
private var stack: Stack<NestedDirectories>
init(nestedList: [NestedDirectories]) {
self.stack = Stack<NestedDirectories>()
var i: Int = nestedList.count - 1
while i >= 0 {
self.stack.Push(nestedList[i])
i -= 1
}
}
func HasNext() -> Bool {
while stack.Count > 0 {
let top: NestedDirectories = stack.Peek()!
if top.Isfile() {
return true
}
let topList: [NestedDirectories] = stack.Pop()!.GetList()!
var i: Int = topList.count - 1
while i >= 0 {
self.stack.Push(topList[i])
i -= 1
}
}
return false
}
func Next() -> String? {
if HasNext() {
return self.stack.Pop()!.Getfile()
}
return nil
}
}
var nestedList: [NestedDirectories] = []
var l1: NestedDirectories = NestedDirectories()
nestedList.append(NestedDirectories(value: "F1"))
l1.Add(ni: NestedDirectories(value: "F2"))
l1.Add(ni: NestedDirectories(value: "D1"));
nestedList.append(l1);
nestedList.append(NestedDirectories(value: "D2"));
let itr: NestedIterator = NestedIterator(nestedList: nestedList)
print("Original structure: [F1, [F2, D1], D2]")
print("");
print("Output:")
while itr.HasNext() {
print("itr.Next(): ", terminator: "")
print(itr.Next()!)
}
Directory iterator

Complexity measures

Time complexity Space complexity
O(n+l)O(n + l)
...
Access this course and 1400+ top-rated courses and projects.