C利用树型结构设计并实现一个简单的目录管理系统,该系统可以对所有目录进行管理,如目录的新建、删除、查询、目录名称修改、按某种顺序输出所有目录(树的遍历操作)、以树型结构输出所有目录等功能代码
时间: 2024-01-25 15:05:07 浏览: 72
以下是一个包含新建、删除、查询、目录名称修改、按某种顺序输出所有目录、以树型结构输出所有目录等功能的树型目录管理系统的 C 代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CHILDREN 10 // 树节点最多的子节点数量
// 定义树节点结构体
struct TreeNode {
char name[20]; // 目录或文件名
int isDir; // 是否是目录
struct TreeNode* children[MAX_CHILDREN]; // 子节点数组
int numChildren; // 子节点数量
};
// 创建一个新的树节点
struct TreeNode* createTreeNode(char* name, int isDir) {
struct TreeNode* node = (struct TreeNode*) malloc(sizeof(struct TreeNode));
strcpy(node->name, name);
node->isDir = isDir;
node->numChildren = 0;
return node;
}
// 添加子节点到父节点
void addChild(struct TreeNode* parent, struct TreeNode* child) {
if (parent->numChildren >= MAX_CHILDREN) {
printf("父节点%s的子节点数量已达到最大值%d\n", parent->name, MAX_CHILDREN);
return;
}
parent->children[parent->numChildren] = child;
parent->numChildren++;
}
// 根据名称查找子节点
struct TreeNode* findChild(struct TreeNode* parent, char* name) {
for (int i = 0; i < parent->numChildren; i++) {
if (strcmp(parent->children[i]->name, name) == 0) {
return parent->children[i];
}
}
return NULL;
}
// 删除子节点
void deleteChild(struct TreeNode* parent, char* name) {
for (int i = 0; i < parent->numChildren; i++) {
if (strcmp(parent->children[i]->name, name) == 0) {
struct TreeNode* child = parent->children[i];
for (int j = i; j < parent->numChildren - 1; j++) {
parent->children[j] = parent->children[j + 1];
}
parent->numChildren--;
free(child);
return;
}
}
}
// 修改节点名称
void renameNode(struct TreeNode* node, char* newName) {
strcpy(node->name, newName);
}
// 按名称排序
void sortByName(struct TreeNode* node) {
for (int i = 0; i < node->numChildren - 1; i++) {
for (int j = i + 1; j < node->numChildren; j++) {
if (strcmp(node->children[i]->name, node->children[j]->name) > 0) {
struct TreeNode* temp = node->children[i];
node->children[i] = node->children[j];
node->children[j] = temp;
}
}
}
for (int i = 0; i < node->numChildren; i++) {
if (node->children[i]->isDir) {
sortByName(node->children[i]);
}
}
}
// 按深度优先遍历输出
void printTree(struct TreeNode* node, int level) {
for (int i = 0; i < level; i++) {
printf(" "); // 缩进
}
printf("%s", node->name);
if (node->isDir) {
printf("/");
}
printf("\n");
for (int i = 0; i < node->numChildren; i++) {
printTree(node->children[i], level + 1);
}
}
// 按树型结构输出
void printTreeStructure(struct TreeNode* node, int level) {
for (int i = 0; i < level; i++) {
printf(" "); // 缩进
}
printf("%s", node->name);
if (node->isDir) {
printf("/");
}
printf("\n");
for (int i = 0; i < node->numChildren; i++) {
printTreeStructure(node->children[i], level + 1);
}
}
// 删除节点及其子节点
void deleteTree(struct TreeNode* node) {
for (int i = 0; i < node->numChildren; i++) {
deleteTree(node->children[i]);
}
free(node);
}
int main() {
// 创建根节点
struct TreeNode* root = createTreeNode("", 1);
// 添加目录和文件
struct TreeNode* dir1 = createTreeNode("dir1", 1);
addChild(root, dir1);
struct TreeNode* file1 = createTreeNode("file1", 0);
addChild(dir1, file1);
struct TreeNode* dir2 = createTreeNode("dir2", 1);
addChild(root, dir2);
struct TreeNode* file2 = createTreeNode("file2", 0);
addChild(dir2, file2);
// 打印树形结构
printf("树形结构:\n");
printTree(root, 0);
printf("\n");
// 查找节点
struct TreeNode* node = findChild(root, "dir1");
if (node != NULL) {
printf("查找到节点%s\n", node->name);
} else {
printf("未找到节点\n");
}
printf("\n");
// 修改节点名称
renameNode(file2, "newfile2");
printf("修改后的节点名称:%s\n", file2->name);
printf("\n");
// 按名称排序
sortByName(root);
printf("按名称排序后的树形结构:\n");
printTree(root, 0);
printf("\n");
// 按树型结构输出
printf("树型结构:\n");
printTreeStructure(root, 0);
printf("\n");
// 删除节点
deleteChild(dir2, "file2");
printf("删除后的树形结构:\n");
printTree(root, 0);
printf("\n");
// 删除树
deleteTree(root);
return 0;
}
```
上述代码在原有的树型目录管理系统基础上,增加了新建、删除、查询、目录名称修改、按某种顺序输出所有目录、以树型结构输出所有目录等功能。通过树形结构,可以方便地管理和操作目录和文件,有利于文件管理的可视化和交互化。
阅读全文