问题描述 对计算机中的目录树实现建立目录、修改目录结构、查询和删除等操作。 基本要求 (1) 按二叉链表的存储方式存储计算机中的目录树。 (2)实现目录树的建立、遍历及插入结点和删除结点操作。 测试数据 由读者依据软件工程的测试技术自己确定。注意测试边界数据。 实现提示. (1)根据树与二叉树的对应关系,对树的操作最终要借助对二叉树进行操作来实现。 (2)查询操作可以实现是否存在某目录或查询指定目录的父目录或子目录。用c语言
时间: 2024-02-18 16:03:58 浏览: 36
以下是一份C语言代码,实现建立目录、修改目录结构、查询和删除等操作:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* 目录树结点 */
typedef struct TreeNode {
char name[50]; // 目录名
struct TreeNode *first_child; // 第一个子结点
struct TreeNode *next_sibling; // 下一个兄弟结点
} TreeNode;
/* 在指定结点下插入新结点 */
void insertNode(TreeNode *parent, TreeNode *node) {
if (parent == NULL || node == NULL) return;
if (parent->first_child == NULL) {
parent->first_child = node;
} else {
TreeNode *p = parent->first_child;
while (p->next_sibling != NULL) {
p = p->next_sibling;
}
p->next_sibling = node;
}
}
/* 删除指定结点 */
void deleteNode(TreeNode *root, char *name) {
if (root == NULL) return;
TreeNode *parent = NULL;
TreeNode *p = root;
while (p != NULL && strcmp(p->name, name) != 0) {
parent = p;
p = p->next_sibling;
}
if (p == NULL) return; // 没有找到该结点
if (parent == NULL) {
root = p->next_sibling; // 删除根结点
} else {
parent->next_sibling = p->next_sibling; // 删除普通结点
}
free(p);
}
/* 查询指定结点 */
TreeNode *findNode(TreeNode *root, char *name) {
if (root == NULL) return NULL;
if (strcmp(root->name, name) == 0) return root;
TreeNode *p = root->first_child;
while (p != NULL) {
TreeNode *result = findNode(p, name);
if (result != NULL) return result;
p = p->next_sibling;
}
return NULL;
}
/* 遍历目录树并打印 */
void traverseTree(TreeNode *root, int level) {
if (root == NULL) return;
for (int i = 0; i < level; i++) {
printf(" "); // 每个层级占4个空格
}
printf("%s\n", root->name);
TreeNode *p = root->first_child;
while (p != NULL) {
traverseTree(p, level + 1);
p = p->next_sibling;
}
}
/* 主函数 */
int main() {
// 构建一个目录树
TreeNode *root = (TreeNode*)malloc(sizeof(TreeNode));
strcpy(root->name, "root");
root->first_child = NULL;
root->next_sibling = NULL;
TreeNode *dir1 = (TreeNode*)malloc(sizeof(TreeNode));
strcpy(dir1->name, "dir1");
dir1->first_child = NULL;
dir1->next_sibling = NULL;
insertNode(root, dir1);
TreeNode *dir2 = (TreeNode*)malloc(sizeof(TreeNode));
strcpy(dir2->name, "dir2");
dir2->first_child = NULL;
dir2->next_sibling = NULL;
insertNode(root, dir2);
TreeNode *dir11 = (TreeNode*)malloc(sizeof(TreeNode));
strcpy(dir11->name, "dir11");
dir11->first_child = NULL;
dir11->next_sibling = NULL;
insertNode(dir1, dir11);
TreeNode *dir12 = (TreeNode*)malloc(sizeof(TreeNode));
strcpy(dir12->name, "dir12");
dir12->first_child = NULL;
dir12->next_sibling = NULL;
insertNode(dir1, dir12);
TreeNode *dir21 = (TreeNode*)malloc(sizeof(TreeNode));
strcpy(dir21->name, "dir21");
dir21->first_child = NULL;
dir21->next_sibling = NULL;
insertNode(dir2, dir21);
// 遍历目录树并打印
traverseTree(root, 0);
// 删除指定结点
deleteNode(root, "dir12");
// 查询指定结点
TreeNode *node = findNode(root, "dir21");
if (node != NULL) {
printf("Found node: %s\n", node->name);
} else {
printf("Node not found.\n");
}
return 0;
}
```
输出结果如下:
```
root
dir1
dir11
dir12
dir2
dir21
Found node: dir21
```
其中,每个层级占4个空格,实现了目录树的建立、遍历、插入和删除等操作,并且可以查询指定结点。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)