#C4226. File System Simulation
File System Simulation
File System Simulation
You are required to simulate a simple file system that supports three commands: mkdir
, cd
, and ls
. The file system has a root directory named /
. Each directory may contain zero or more child directories.
- mkdir <dir_name>: Create a new subdirectory with name
dir_name
in the current directory if it does not already exist. - cd <dir_name>: Change the current directory to the given subdirectory. The special command
cd ..
moves to the parent directory. If acd
command refers to a non-existent child, ignore it. - ls: List the names of all immediate subdirectories in the current directory in lexicographical order.
You will be given a series of commands, and you must process them sequentially. Each time an ls
command is issued, output the list of directory names (space-separated) in one line. Ensure that your solution reads input from stdin and writes output to stdout.
Note: If there are no subdirectories when ls
is called, output an empty line.
inputFormat
The input is given from stdin in the following format:
T command_1 command_2 ... command_T
Where T (an integer) is the number of commands, and each subsequent line contains one command.
outputFormat
For every ls
command in the input, print one line containing the names of the subdirectories in the current directory, sorted in lexicographical order and separated by a single space. If there are no directories, print an empty line.
10
mkdir home
mkdir var
mkdir usr
ls
cd home
mkdir user
mkdir documents
ls
cd ..
ls
home usr var
documents user
home usr var
</p>