AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > 数据结构

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

51自学网 http://www.51zixue.net

原帖及讨论:http://bbs.bccn.net/thread-149370-1-1.html

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();
}

 

 

 
上一篇:C语言完成一个学生成绩管理程序  下一篇:KMP的理解