Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Data Structure using C++ Write a program that will simulate a Unix File System.

ID: 3703415 • Letter: D

Question

Data Structure using C++

Write a program that will simulate a Unix File System. This program will use a tree to represent the file system. It should provide, at a minimum, the following functionality to the user: mkdir, touch, cd, ls, and exit. When this program runs it should display a welcome prompt and then wait for input from the user. The user should be then be able type in their commands. The idea is to simulate the command prompt of windows or the shell prompt of Linux. Your program should feel similar to either of these shells.

mkdir <dirname> – This should create a new “directory” in the current directory with ‘dirname’ as it’s name.

touch <filename> – This should create a new “file” in the current directory with ‘filename’ as it’s name.

cd <path> – This should change the current directory to the directory specified by ‘path’. It should be able to accept both relative and absolute paths. A relative path starts with a ‘/’ and describes a path from the root. A relative path will start with a ‘.’ and describes a path from the current directory. ‘..’ indicates the parent directory. ‘.’ Indicates the current directory. Paths should use a ‘/’ to separate folders in the path. For example ‘../home/fconlon’. A ‘/’ by itself indicates the root directory. If a user attempts to cd to a file or a directory that does not exist an error message should be displayed, and the program should continue to run.

ls <path> – This should display all of the files and directories in the directory specified by ‘path’. If not path is specified then it should display all the files and directories in the current directory. This command should first display all the directories in the current directory in alphabetical order the display all of the files in the current directory in alphabetical order. ‘path’ should follow the same conventions for the ‘path’ argument from cd. It should display the same errors under the same conditions as cd.

Exit – This should exit the program. Don’t forget to clear any memory that you allocated.

Explanation / Answer

In this problem Iam using tree structure in C Program.

Surce Code:

# include <stdio.h>

# include <conio.h>

# include <stdlib.h>

typedef struct BinarySearchTree {

   int data;

   struct BinarySearchTree leftchild, rightchild;

} node;

void insert(node , node );

void inorder(node *);

void preorder(node *);

void postorder(node *);

node search(node , int, node **);

void main() {

   int ch;

   char ans = 'N';

   int key;

   node new_node, root, tmp, par;

   node *get_node();

   root = NULL;

   clrscr();

   do {

      printf(" 1.Create");

      printf(" 2.Search");

      printf(" 3.Recursive Traversals");

      printf(" 4.Exit");

      printf(" Enter your choice :");

      scanf("%d", &ch);

      switch (ch) {

      case 1:

         do {

            new_node = get_node();

            printf(" Enter The Element ");

            scanf("%d", &new_node->data);

            if (root == NULL)

               root = new_node;

            else

               insert(root, new_node);

            printf(" Want To enter More Elements?(y/n)");

            ans = getch();

         } while (ans == 'y');

         break;

      case 2:

         printf(" Enter Element to be searched :");

         scanf("%d", &key);

         tmp = search(root, key, &par);

         printf(" Parent of node %d is %d", tmp->data,par->data);

         break;

      case 3:

         if (root == NULL)

            printf("Tree Is Not Created");

         else {

            printf(" The Inorder display : ");

            inorder(root);

            printf(" The Preorder display : ");

            preorder(root);

            printf(" The Postorder display : ");

            postorder(root);

         }

         break;

      }

   } while (ch != 4);

}

node *get_node() {

   node *temp;

   temp = (node *) malloc(sizeof(node));

   temp->leftchild = NULL;

   temp->rightchild = NULL;

   return temp;

}

void insert(node root, node new_node) {

   if (new_node->data < root->data) {

      if (root->leftchild == NULL)

         root->leftchild = new_node;

      else

         insert(root->leftchild, new_node);

   }

   if (new_node->data > root->data) {

      if (root->rightchild == NULL)

         root->rightchild = new_node;

      else

         insert(root->rightchild, new_node);

   }

}

node search(node root, int key, node **par) {

   node *temp;

   temp = root;

   while (temp != NULL) {

      if (temp->data == key) {

         printf(" The %d Element is Present", temp->data);

         return temp;

      }

      *par = temp;

      if (temp->data > key)

         temp = temp->leftchild;

      else

         temp = temp->rightchild;

   }

   return NULL;

}

void inorder(node *temp) {

   if (temp != NULL) {

      inorder(temp->leftchild);

      printf("%d", temp->data);

      inorder(temp->rightchild);

   }

}

void preorder(node *temp) {

   if (temp != NULL) {

      printf("%d", temp->data);

      preorder(temp->leftchild);

      preorder(temp->rightchild);

   }

}

void postorder(node *temp) {

   if (temp != NULL) {

      postorder(temp->leftchild);

      postorder(temp->rightchild);

      printf("%d", temp->data);

   }

}