敬业的IT人 >> 编程开发 >> 数据结构&算法分析 >> 关于文件管理系统的数据结构模拟

关于文件管理系统的数据结构模拟

敬业的IT人 互联网 佚名 2008-1-4 15:22:00

原帖及讨论: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();
}

粤ICP备06119539号
Copyright CiscoSky.Org,Some Rights Reserved.
Email:me1228#tom.com