Click here to Skip to main content
14,303,069 members
Rate this:
Please Sign up or sign in to vote.
See more:
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)
Posted
Updated 21-Jan-11 6:12am
v3
Rate this:
Please Sign up or sign in to vote.

Solution 2

You seem to have a tree structure implemented, and apparently you also have a linked list structure. So all you need to do is to set the tree node's data to point to an instance of the linked list structure. What exactly is the problem in doing this?
   
Comments
Datta Bawarae 21-Jan-11 12:51pm
   
i know that but the problem is i dont know how to access the whole list through that node what should i do?
actually im making a dummy file system which has drive node as root node and it contains folders and the files..folder and files are the linked list that is to be added on the root node
Nish Nishant 21-Jan-11 12:55pm
   
That's implementation dependent. It's very hard to say without knowing how you've implemented this.
But here's some hypothetical code that can do this:

((linkedlist*)tree->getnode(...)->getdata())->getfirst();

Okay that may seem crazy, but you get the point.
Sergey Alexandrovich Kryukov 21-Jan-11 13:01pm
   
Makes sense - a 5.
Nish Nishant 21-Jan-11 13:02pm
   
Thanks.
Espen Harlinn 21-Jan-11 13:10pm
   
5+ Should have been enough :)
Rate this:
Please Sign up or sign in to vote.

Solution 3

Here is a simple implementation solving your problem.

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


Regards
Espen Harlinn
   
v2
Comments
Sergey Alexandrovich Kryukov 21-Jan-11 13:00pm
   
Good work - a 5.
Nish Nishant 21-Jan-11 13:02pm
   
Good effort, my 5 too!
Espen Harlinn 21-Jan-11 13:09pm
   
Thanks SAKryukov!
Espen Harlinn 21-Jan-11 13:09pm
   
Thanks Nishant!
Datta Bawarae 21-Jan-11 14:04pm
   
thnx its really a great effort but my problem is still unsolved what i really want to do is first get the desired node in a simple tree than add link list,i just want to know how to attach a link list to that node..
eg
root
\
\
node->(having a linklist)
Rate this:
Please Sign up or sign in to vote.

Solution 4

I think this will solve your problem :-D
#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();}
   
Comments
Datta Bawarae 25-Jan-11 10:29am
   
yes this was the thing what i was talking about
thank u "irtz"
irtz 25-Jan-11 10:33am
   
Thanks no problem :)
Rate this:
Please Sign up or sign in to vote.

Solution 1

Here is one[^] of thousand examples I found googling for "C++ linked list example"
   
v2
Comments
Datta Bawarae 21-Jan-11 12:52pm
   
thnx but bhaai jan mae tree kii baat ker raha hoon i am not talking about simple linked list
Sergey Alexandrovich Kryukov 21-Jan-11 13:02pm
   
Useful, but not exactly to the point.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100