Linked list within a class

Is this a possibility? I want a class that was certain values and a linked list in that class.
The class should look something like this:
1
2
3
4
5
6
7
class C {
    public:
        int v;
        struct list{
            int z;
            list *next;
};


How could I use this list, if there is tutorial for this just point me to it, thank you for any help
Last edited on
I'm not really sure what you are asking, but if you're using C++ and not C you can use a std::list rather than that struct:
http://en.cppreference.com/w/cpp/container/list
or
http://en.cppreference.com/w/cpp/container/forward_list
Last edited on
Using C++. I want a class with a linked list belonging to that class, if that makes any sense. Thank you for the sources.
Last edited on
You'll need a class with the data you wanna list then the class List... kinda:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class X{
    public:
        string name;
        void Capture();
        void Print();
};

class List{
    public:
        Lista *top,*next;
        X data;
        void insert(X obj);
};

void List::insert(X obj){
    ptr=new List;
    ptr->data=obj;

    if(tam==0){
        ptr->next=NULL;
        top=ptr;
    }
    else{
        ptr->next=top;
        top=ptr;
    }

}

int main(){
       X x;
       List list;

       x.Capture();
       list.insert(x);

       return 0;


I doubt you find something specific as a tutorial... but hope this helps... as far as i wrote here, the code works, but many functions missing... good luck.
Last edited on
Topic archived. No new replies allowed.