I've got a linklist class and would like to have the ability to create a new node that can instantiate it's own new linklist. (some what of a multidimensional link list)

I've tried the line: linklist *self = this;

but my program crashes when i exit. any thoughts?

Recommended Answers

All 8 Replies

A linked list can only contain elements of the same type. You could make a list of list like

std::list< std::list< int > >;

I see what you want. You want the list to be able to point to a list as well a leaf element. We'll what you
want now is a tree, or more general a graph. Google it up. But buy your definition, a regular linked list is already a match. You can think of each subset of the list as its own list

You say that the program crashes when it exits, which implies that the problem is in how you are cleaning up the program run, most likely in when you are deleting the nodes of the list. Can you post your code where the problem is occurring?

my LL header file has:

#ifndef __LINKEDLIST_H
#define __LINKEDLIST_H
#include <string>

using namespace std;

class linklist
{private:
         int number;
         linklist *self;
         struct node
         {
              string data;
              unsigned short ATTRIBUTE[5];
            node *link;
         }*p;

   public:

         linklist();
         void AttachNode();
         void append( string num );

and when i add the line self = this; to a function in the ll.cpp file the program errors. even when i do not call the function itself. if i comment out the line the proram exits fine..

void linklist::AttachNode()
{
        self = this;
}

the naming convention for the function to attach a link list to the current node is not the best, but im just going for proof of concept now.

I guess what i want is to have part of the data contained in a node would have a dummy ll that could be extend if needed..

ok so i've changed the node to contain the LL, but as a pointer variable.

struct node
         {
              string data;
              linklist * LL;
              unsigned short ATTRIBUTE[5];
            node *link;
         }*p;

in my linklist.cpp file i've added. whats commented out give "append is not a type" error at compile..

void linklist::AttachNode()
{
     
        p->LL = new linklist();
        //p->>LL.append("stuff");
        
}

LL is a pointer so you would ned to write

p->ll->appened("stuff");

Thanks for the response Nathan, but i still can't seem to figure this out.. my code is described below step by step. What i'm trying to do is within the ProfileManager.cpp class i will create a new profile. I will also create new LL when i need to append a ChildLL to the current Node i am accesses. a Nnode can be the top profile or a childLL (a leaf) or deeper, since each node owns a pointer to a new LL.

1) Here is the linklist.h file that defines what a node is:

class linklist
{
     public:
      
     struct node
         {
              string data;
              linklist * ChildLL;
              unsigned short ATTRIBUTE[5];
            node * link;
            node * mother; // The parent node, the owner of this node.
            node * child;  // The child node, the child of this node.

         }*p;



         linklist();
         void linklist::CreateChild(linklist Profile);
         linklist::node * ReturnNode();
         linklist::node * FindNode(string num);
         linklist::node * linklist::append(string num);
         linklist::node * append( string num , unsigned short ATTR[5]);
         void add_as_first( string num );
         void addafter( int c, string num );
         void del( string num );
         void display();
         void display(linklist::node * Profile);
         string ReturnElement(int c);
         unsigned short * ReturnAttribute(int c);
         int count();
         ~linklist();
};

2) in ProfileManager i use the LL class. Each new profile is considered a node. so first, create the top linklist PROF_HANDLE. This will contain all of the "profiles". create linklist pointer variables. one pointer for the current node and one pointer for the current node childLL.

//Profile Manager will contain all the profiles. This LL contains those profiles
//Each Item added to the PROF_HANDLE is a new node
//You can open, close, add items and delete items to profiles and set the attributes
linklist PROF_HANDLE;

//This is the Current node that is being accessed. it can be the top profile, or a leaf node 
linklist::node * CURRENT_NODE_PTR;

//this is the Current Node Child LL. it is the ChildLL of the CURRENT_NODE
linklist * CURRENT_NODE_CHILDLL_PTR;

3) create a new profiles

int ProfileManager::NewProfile(string ProfName, unsigned short Attribute[5]) 
{
    //Check to see if the Profile already exists
    //      FindNode() returns NULL if it did not find anything
    //      FindNode returns a pointer to the location of the node if it exists
    if(PROF_HANDLE.FindNode(ProfName) != NULL)
    {
        cout<<"ERROR: There is already a profile by the name of \""<<ProfName<<"\""<<endl;
        cout<<"ERROR: Please try a different name"<<endl;
    }
    else
    {
        //Append the new Profile to the PROF_HANDLE
        //       append returns a pointer to the new node
        //       assign Current_PROFILE to the new node
        CURRENT_NODE_PTR = PROF_HANDLE.append(ProfName, Attribute);

        //Assign the mother and child of the new profile = null. There is nothing higher then a brand new profile.
        CURRENT_NODE_PTR->mother = NULL;
                
        //for the newly created Profile, create a childLL
        linklist CURRENT_NODE_CHILDLL;
        //define the location of the ChildLL as the "Address of" CURRENT_NODE_CHILDLL
        CURRENT_NODE_PTR->ChildLL = & CURRENT_NODE_CHILDLL;
        
        
        //OTC
        cout<<"$$"<<endl;
        cout<<"$$ Successfully created profile : \""<<CURRENT_NODE_PTR->data<<"\" at location "<<CURRENT_NODE_PTR<<endl;
        cout<<"$$ Successfully created child   : "<<CURRENT_NODE_PTR->ChildLL<<endl;
    }
    
    //display all the profiles that have been created
    PROF_HANDLE.display();
}

4) open a profile (access the profile contents and set the dummy pointer variables to the correct location)

void ProfileManager::OpenProfile(string Profile_Name)
{
     //Find the profile
     //      FindNode() returns NULL if it did not find anything
     //      FindNode returns a pointer to the location of the node if it exists
     CURRENT_NODE_PTR = PROF_HANDLE.FindNode(Profile_Name);
     
     //If FindNode = null, it did not find the profile
     if(CURRENT_NODE_PTR == NULL)
     {
          cout<<"Could not find profile"<<endl;
     }
     //else, it should assign the dummy variable (CURRENT_NODE_CHILDLL_PTR) to access the contents of the profiles (CURRENT_NODE) child LL
     else
     {
         CURRENT_NODE_CHILDLL_PTR = CURRENT_NODE_PTR->ChildLL;
         //CURRENT_NODE_CHILDLL_PTR->display();
     }
          
}

5) finally add items to the "open" profile

int ProfileManager::AddItem(string num, unsigned short Attribute[5])
{
     CURRENT_NODE_PTR->ChildLL->append(num, Attribute);  
     //CURRENT_NODE_PTR->ChildLL->display();  
}

-----

So, i used your suggestion in the additem function but my program crashes when it executes

CURRENT_NODE_PTR->ChildLL->append(num, Attribute);

furthermore, I'm not sure if I am assigned the "dummy variables" (CURRENT_NODE_PTR, CURRENT_NODE_CHILDLL_PTR) the correct locations of the node/childLL that i want to set the "current profile" as..

Does that make sense? Please help! if i can get this i can move forward!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.