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:

  • Initializes the iterator with the nested list nestedList.
  • next() returns the next file in the nested directories.
  • 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:

class nestedDirectories {
// Constructor initializes an empty nested list and a single file.
constructor(opt_parm = null){
if (opt_parm != null){
this.file = opt_parm
}
else{
this.list = [];
this.file = null;
}
}
// Constructor initializes a single file.
setfile(value){
this.list = null
this.file = value
}
// @return true if this nestedDirectories holds a single file, rather than a nested list.
isfile(){
if(this.file != null)
return true;
return false;
}
// @return the single file that this nestedDirectories holds, if it holds a single file
// Return null if this nestedDirectories holds a nested list
getfile(){
return this.file;
}
// Set this nestedDirectories to hold a nested list and adds a nested file to it.
add(ni){
if (this.file != null){
this.list = [];
this.list.push(new nestedDirectories(this.file));
this.file = null;
}
this.list.push(ni);
}
// @return the nested list that this nestedDirectories holds, if it holds a nested list
// Return null if this nestedDirectories holds a single file
getList(){
return this.list;
}
}
class nestedIterator {
constructor(nestedList){
this.stack = [];
for (var i = nestedList.length - 1; i >= 0; i--) {
this.stack.push(nestedList[i]);
}
}
hasNext() {
while (this.stack.length > 0) {
top = this.stack[this.stack.length-1]
if (top.isfile())
return true;
var topList = this.stack.pop().getList();
i = topList.length-1
while (i >= 0) {
this.stack.push(topList[i])
i -= 1
}
}
return false;
}
next() {
if(this.hasNext())
return this.stack.pop().getfile();
return false;
}
}
// Driver code
var nestedList = [];
var l1 = new nestedDirectories();
nestedList.push(new nestedDirectories("F1"));
l1.add(new nestedDirectories("F2"));
l1.add(new nestedDirectories("D1"));
nestedList.push(l1);
nestedList.push(new nestedDirectories("D2"));
var itr = new nestedIterator(nestedList);
console.log("Original structure: [F1, [F2, D1], D2]");
console.log("");
console.log("Output:");
while(itr.hasNext()){
console.log("itr.next(): ",itr.next());
}
Directory iterator

Complexity measures

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