您的位置:逆风者 数据结构 正文
原作者:www.upwinder.com 添加时间:2007-09-02 原文发表:2007-08-31 人气:16 来源:本站原创

原帖及讨论:http://bbs.bc-cn.net/dispbbs.asp?boardid=56&id=149370

windows操作系统中的文件夹的数据结构貌似树,不是二叉树,而是普通数.
逆风编程精品
可以如下设计,不过只是框架,很多都没有考虑到

程序代码:

template <class T>
class DIRSTRUCT
{
DIRSTRUCT * lpParentDir;
vector<DIRSTRUCT*> SubDir;
int m_nSubDir;
T file;
private:
void DisPlay(const DIRSTRUCT<T>* lpDir)
{
cout<<lpDir->file<<endl;
for(int i=0;i<lpDir->m_nSubDir;i )
{
DisPlay(lpDir->SubDir[i]);
}
}
void Delete(DIRSTRUCT<T> *lpDir)
{
if(lpDir->m_nSubDir)
{
for(int i=lpDir->m_nSubDir-1;i>=0;i--)
{
Delete(lpDir->GetChild(i));
delete lpDir->GetChild(i);
lpDir->SubDir.pop_back();
}
}
lpDir->lpParentDir=NULL;
lpDir->m_nSubDir=0;
}
void Clone(DIRSTRUCT<T> *lp,const DIRSTRUCT<T> *lpDir)
{
for(int i=0;i<lpDir->m_nSubDir;i )
{
lp->AddChild(lpDir->SubDir[i]->file);
Clone(lp->SubDir[i],lpDir->SubDir[i]);
}
}
public:
DIRSTRUCT<T>()
{
file=0;
m_nSubDir=0;
lpParentDir=NULL;
}
DIRSTRUCT<T>(T t,DIRSTRUCT* lpParent=NULL)
{
m_nSubDir=0;
lpParentDir=lpParent;
file=t;
}
DIRSTRUCT(const DIRSTRUCT<T>& dir)
{
Copy(&dir);
}
DIRSTRUCT<T> &operator=(const DIRSTRUCT<T>& dir)
{
Copy(&dir);
return *this;
}
~DIRSTRUCT<T>()
{
remove();
}
void Copy(const DIRSTRUCT<T>* lpDir)
{
remove();
file=lpDir->file;
Clone(this,lpDir);
}
public:
void AddChild(T t)
{
m_nSubDir ;
DIRSTRUCT* lpDir=new DIRSTRUCT<T>(t,this);
SubDir.push_back(lpDir);
}
int GetCount()
{
return SubDir.size();
}
DIRSTRUCT<T>* GetChild(int i)
{
return SubDir.at(i);
}
DIRSTRUCT<T>* operator[](int i)
{
return SubDir.at(i);
}
DIRSTRUCT<T>* GetParent()
{
return lpParentDir;
}
bool IsChild()
{
return lpParentDir;
}
void show()
{
DisPlay(this);
}
void remove()
{
Delete(this);
}
};



测试代码,主要是针对几个构造函数,'='的测试。

程序代码:


void main()
{
DIRSTRUCT<int> dir;
dir.AddChild(1);
dir.AddChild(2);
dir[0]->AddChild(11);
DIRSTRUCT<int> dir2;
dir.show();
cout<<endl;
dir2.show();
cout<<endl;
dir2.Copy(&dir);
dir2.show();
dir.remove();
cout<<endl;
dir2.show();
cout<<endl;
dir.show();
dir.Copy(&dir2);
cout<<endl;
dir.show();
DIRSTRUCT<int> dir3(dir);
cout<<endl;
dir3.show();
DIRSTRUCT<int> dir4;
dir4=dir3;
cout<<endl;
dir4.show();
cout<<endl;
dir4.GetChild(0)->show();
cout<<endl;
dir4.GetChild(0)->GetParent()->show();
}

相关文章

数据结构教程 第二十课 广义表
数据结构教程 第三十六课 选择排序,归并排
线索二叉树算法
数据结构--序言
数据结构教程 第十课 栈的表示与实现
数据结构教程 第二十六课 图的定义与术语
排序及查找方法
数据结构教程 第十六课 串操作应用举例
数据结构教程 第三十二课 哈希表(一)
数据结构教程 第三十一课 动态查找表
链表基本操作的程序实现
用栈设置密码
数据结构教程 第三十课 静态查找表(二)有
有向图转换
JAVA编写的拼图游戏移动算法,简单易懂
数据结构教程 第四十课 总复习
无向图转换
简单的行编辑器
数据结构教程 第五课 线性表的类型定义
Huffman编码生成程序

相关评论


本文章所属分类:首页 数据结构

  热门关键字: