本文介绍了如何在树的任何节点中添加链接列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,有一棵带有根节点的树,我想在一个节点上添加一个链接列表;
根
/\
/\
节点1. (在node2上添加链接列表)
eg.there is a tree with a root node and I want to add a link list on a node;
root
/ \
/ \
node1 node2-> (adding linked list on node2)
推荐答案
#include <list>
class Node;
typedef std::list<Node> node_list;
class NodeData
{
private:
unsigned referenceCount_;
node_list children_;
public:
NodeData()
: referenceCount_(1),
children_(0)
{
}
unsigned add_reference()
{
referenceCount_++;
return referenceCount_;
}
unsigned remove_reference()
{
referenceCount_--;
if(referenceCount_ == 0)
{
delete this;
}
return referenceCount_;
}
node_list& children()
{
return children_;
}
const node_list& children() const
{
return children_;
}
}
class Node
{
private:
NodeData* data_;
public:
Node()
{
data_ = new NodeData();
}
Node(const Node& other)
{
data_ = other.data_;
if(data_)
{
data_->add_reference();
}
}
virtual ~Node()
{
if(data_)
{
data_->remove_reference();
}
}
Node& operator = (const Node& other)
{
if(other.data_ != data_)
{
if(data_)
{
data_->remove_reference();
}
data_ = other.data_;
if(data_)
{
data_->add_reference();
}
}
}
node_list& children()
{
return data_->children();
}
const node_list& children() const
{
return data_->children();
}
}
void main()
{
Node rootNode;
Node childNode1;
Node childNode2;
Node childNode2_1;
rootNode.children().push_back(childNode1);
rootNode.children().push_back(childNode2);
childNode2.children().push_back(childNode2_1);
}
问候
Espen Harlinn
Regards
Espen Harlinn
#include<conio.h>
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<process.h>
#include<fstream.h>
class File{
char* FileName;
char* Extension;
char* Attributes;
public:
File(){
FileName=NULL;
Extension=NULL;
Attributes="false";
}
void setattribute(char *a){
Attributes=new char[strlen(a)+1];
strcpy(Attributes,a);
}
void setfilename(char * n){
FileName=new char[strlen(n)+1];
strcpy(FileName,n);
}
void setextension(char *e){
Extension=new char[strlen(e)+1];
strcpy(Extension,e);
}
char *getattribute(){
return Attributes;
}
char *getfilename(){
return FileName;
}
char *getextension(){
return Extension;
}
};
class Folder{
char* FolderName;
char* Attributes;
File *PointertoFile;
Folder *PointertoFolder;
Folder *PointertoSubFolder;
public:
Folder(){
FolderName=NULL;
Attributes="false";
PointertoFile=NULL;
PointertoFolder=NULL;
PointertoSubFolder=NULL;
}
void setfoldername(char * n){
FolderName=new char[strlen(n)+1];
strcpy(FolderName,n);
}
void setattribute(char *a){
Attributes=new char[strlen(a)+1];
strcpy(Attributes,a);
}
void setPointertoFile(File* p){
PointertoFile=p;}
void setPointertoFolder(Folder* p){
PointertoFolder=p;}
void setPointertoSubFolder(Folder* p){
PointertoSubFolder=p;}
char *getfoldername(){
return FolderName;
}
char *getattribute(){
return Attributes;
}
File* getPointertoFile(){
return PointertoFile;}
Folder* getPointertoFolder(){
return PointertoFolder;}
Folder* getPointertoSubFolder(){
return PointertoSubFolder;}
};
class Drive{
char *DriveName;
Folder *PointertoFolder;
public:
Drive(){
DriveName=NULL;
PointertoFolder=NULL;
}
void setDriveName(char* n){
DriveName=new char[strlen(n)+1];
strcpy(DriveName,n);
}
Folder* getPointertoFolder(){
return PointertoFolder;}
void setPointertoFolder(Folder* p){
PointertoFolder=p;}
char* getDriveName(){
return DriveName;}
};
class FileSystem{
Drive *root;
Folder *curr;
public:
FileSystem(){
root=new Drive();
root->setDriveName("Drive C");
curr=NULL;
}
/////////////////////////////////////
void createFolder(){
char n[30];
cout<<"Enter FolderName "<<endl;
gets(n);
Folder *f=new Folder();
f->setfoldername(n);
if(root->getPointertoFolder()==NULL)root->setPointertoFolder(f);
else{
int n=0;
cout<<"Enter 1 For Create Folder in Current Folder\nEnter 2 for Create Subfolder of Current Folder "<<endl;
cin>>n;
if(n==1)curr->setPointertoFolder(f);
else if(n==2) curr->setPointertoSubFolder(f);
}
curr=f;
}
//////////////////////////////////////
void createFile(){
char n[30],e[30];
cout<<"Enter FileName then Extension"<<endl;
gets(n);
gets(e);
File *f=new File();
f->setfilename(n);
f->setextension(e);
if(root->getPointertoFolder()==NULL)cout<<"File Must Save In Some Folder"<<endl;
else curr->setPointertoFile(f);
}
////////////////////////////////////////
void DisplayFolder(){
if(root->getPointertoFolder()==NULL )cout<<"There Is No Folder OR File Exist"<<endl;
else
{
Folder *temp=curr;
viewall(temp);
}
}
void viewall(Folder *f){
if(f==NULL)return;
else
{
Folder *temp=f;
while(temp!=NULL){
if(strcmp( temp->getattribute(),"false")==0 )cout<<"Folder :"<<temp->getfoldername()<<endl;
File *temp2=temp->getPointertoFile();
if(temp2!=NULL){cout<<"\nFile :"<<temp2->getfilename()<<endl;
cout<<"Exe :"<<temp2->getextension()<<endl;
}
temp=temp->getPointertoFolder();}
f=f->getPointertoSubFolder();
viewall(f);
}
}
//////////////////////////////////
void NavigatetoFolder(char *name){
if(root->getPointertoFolder()==NULL)cout<<"There Is No Folder and File Exist"<<endl;
else
{
int found=0;
Folder *temp=root->getPointertoFolder();
curr=navviewall(temp,name,found);
if(found==1)cout<<"Folder Navigated "<<endl;
else cout<<"Folder Doesnt Exist "<<endl;
}
}
Folder *navviewall(Folder *f,char* name,int &found){
if(f==NULL)return curr;
else if( strcmp(f->getfoldername(),name)==0){found=1;return f;}
else
{
Folder *temp=f;
while(temp!=NULL){
if( strcmp(temp->getfoldername(),name)==0){found=1;return temp;}
temp=temp->getPointertoFolder();}
f=f->getPointertoSubFolder();
navviewall(f,name,found);}
}
//////////////////////////////////////
void SearchFolder(char *name){
if(root->getPointertoFolder()==NULL)cout<<"There Is No Folder and File Exist"<<endl;
else
{
Folder *temp=root->getPointertoFolder();
int found=0;
fviewall(temp,name,found);
if(found==0)cout<<"Folder Doesnt Exist "<<endl;
}
}
void fviewall(Folder *f,char *name,int &found){
if(f==NULL)return;
else
{
Folder *temp=f;
while(temp!=NULL){
if(strcmp(temp->getfoldername(),name)==0 && strcmp( temp->getattribute(),"false")==0 ){
cout<<"\nFolder :"<<temp->getfoldername()<<endl; found=1; }
temp=temp->getPointertoFolder();}
f=f->getPointertoSubFolder();
fviewall(f,name,found);}
}
/////////////////////////////////////////////////////////
void SearchFile(char *name){
Folder *temp=root->getPointertoFolder();
int found=0;
fileviewall(temp,name,found);
if(found==0)cout<<"File Doesnt Exist "<<endl;
}
void fileviewall(Folder *f,char *name,int &found){
if(f==NULL)return;
else
{
Folder *temp=f;
while(temp!=NULL){
File *temp2=temp->getPointertoFile();
if(temp2!=NULL){if(strcmp(temp2->getfilename(),name)==0 && strcmp( temp2->getattribute(),"false")==0 ){
cout<<"\nFile :"<<temp2->getfilename()<<endl;
cout<<"Exe :"<<temp2->getextension()<<endl;
found=1;
}}
temp=temp->getPointertoFolder();}
f=f->getPointertoSubFolder();
fileviewall(f,name,found);}
}
/////////////////////////////////
void DisplayAll(){
if(root->getPointertoFolder()==NULL )cout<<"There Is No Folder OR File Exist"<<endl;
else
{
cout<<root->getDriveName()<<endl;
Folder *temp=root->getPointertoFolder();
viewall(temp);
}
}
////////////////////////////////
void EditAttributes(){
int n=0;
char name[30];
cout<<"Enter 1 for Folder Attribite Change\nEnter 2 for File Attribute Change"<<endl;
cin>>n;
if(n==1){
cout<<"Enter Folder Name "<<endl;
gets(name);
editFolder(name);
}
else if(n==2){
cout<<"Enter File Name "<<endl;
gets(name);
editFile(name);
}
}
/////////////////////////////////////////////////////
void editFolder(char *name){
if(root->getPointertoFolder()==NULL)cout<<"There Is No Folder and File Exist"<<endl;
else
{
Folder *temp=root->getPointertoFolder();
int found=0;
eviewall(temp,name,found);
if(found==0)cout<<"Folder Doesnt Exist "<<endl;
}
}
void eviewall(Folder *f,char *name,int &found){
if(f==NULL)return;
else
{
Folder *temp=f;
while(temp!=NULL){
if(strcmp(temp->getfoldername(),name)==0 && strcmp( temp->getattribute(),"false")==0 ){
char a[30];
cout<<"\nEnter New Attribute of This Folder (true\false):"<<endl;
cin>>a;
temp->setattribute(a);
found=1; }
temp=temp->getPointertoFolder();}
f=f->getPointertoSubFolder();
eviewall(f,name,found);}
}
///////////////////////////////////////////////////////
void editFile(char *name){
Folder *temp=root->getPointertoFolder();
int found=0;
efileviewall(temp,name,found);
if(found==0)cout<<"File Doesnt Exist "<<endl;
}
void efileviewall(Folder *f,char *name,int &found){
if(f==NULL)return;
else
{
Folder *temp=f;
while(temp!=NULL){
File *temp2=temp->getPointertoFile();
if(temp2!=NULL){if(strcmp(temp2->getfilename(),name)==0 && strcmp( temp2->getattribute(),"false")==0 ){
char a[30];
cout<<"\nEnter New Attribute of This File (true\false):"<<endl;
cin>>a;
temp2->setattribute(a);
found=1;
}}
temp=temp->getPointertoFolder();}
f=f->getPointertoSubFolder();
efileviewall(f,name,found);}
}
/////////////////////////////////////////////////////////////
void DeleteFolder(char *name){
if(root->getPointertoFolder()==NULL)cout<<"There Is No Folder and File Exist"<<endl;
else
{
Folder *temp2=root->getPointertoFolder();
Folder *last=temp2;
while(temp2!=NULL){
if(strcmp(temp2->getfoldername(),name)==0 && (temp2==root->getPointertoFolder()) ){
root->setPointertoFolder(temp2->getPointertoFolder());
delete temp2;
temp2=root->getPointertoFolder();
}
else {if( strcmp(temp2->getfoldername(),name)==0 ){
last->setPointertoFolder(temp2->getPointertoFolder());
delete temp2;
temp2=last;
}}
last=temp2;
temp2=temp2->getPointertoFolder();}
}
}
void DeleteFile(char *name){
Folder *temp=root->getPointertoFolder();
int found=0;
delfile(temp,name,found);
if(found==0)cout<<"File Doesnt Exist "<<endl;
}
void delfile(Folder *f,char *name,int &found){
if(f==NULL)return;
else
{
Folder *temp=f;
while(temp!=NULL){
File *temp2=temp->getPointertoFile();
if(temp2!=NULL){if(strcmp(temp2->getfilename(),name)==0 ){
delete temp2;
temp->setPointertoFile(NULL);
found=1;
}}
temp=temp->getPointertoFolder();}
f=f->getPointertoSubFolder();
delfile(f,name,found);}
}
};
void menu(FileSystem *&s){
int op;
char name[30];
cout<<"\nPress Number For Operation\n> 1. Create File\n> 2. Create Folder\n> 3. Navigate to Folder\n> 4. Search Folder\n> 5. Search File\n> 6. Display Folder\n> 7. Display All\n> 8. Delete Folder\n> 9. Delete File\n> 10. Set Attributes \n> 0. EXIT\n\n"<<endl;
cin>>op;
switch(op){
case 1:{
s->createFile();
menu(s);
break;}
case 2:{
s->createFolder();
menu(s);
break;}
case 3:{
cout<<"Enter Folder Name"<<endl;
gets(name);
s->NavigatetoFolder(name);
menu(s);
break;}
case 4:{
cout<<"Enter Folder Name"<<endl;
gets(name);
s->SearchFolder(name);
menu(s);
break;}
case 5:{
cout<<"Enter File Name"<<endl;
gets(name);
s->SearchFile(name);
menu(s);
break;}
case 6:{
s->DisplayFolder();
menu(s);
break;}
case 7:{
s->DisplayAll();
menu(s);
break;}
case 8:{
cout<<"Enter Folder Name"<<endl;
gets(name);
s->DeleteFolder(name);
menu(s);
break;}
case 9:{
cout<<"Enter File Name"<<endl;
gets(name);
s->DeleteFile(name);
menu(s);
break;}
case 10:{
s->EditAttributes();
menu(s);
break;}
case 0:{
fstream file;
file.open("C:/FileSystem.txt",ios:: app );
file.write((char *)&s,sizeof(FileSystem));
file.close();
exit(0);
break;}
}
}
void main(){
FileSystem *s=new FileSystem;
menu(s);
getch();}
这篇关于如何在树的任何节点中添加链接列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!