linked lists... pointers

Hi! I got a question...

Ive been programing on java for a while and i kind of got adapted to it... so there is something i dont understand here:

for example I wanna do A linked list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class lista {
      private:
              nodol *cabecera;
              nodol *ult;
              int N;
      public:
             lista():cabecera(NULL),N(0){}
             int getN(){return N;}
             nodol getCabecera(){return *cabecera;}
             void setCabecera(nodol *n){cabecera=n;}
             void insertar (char c){
                  nodol *n;
                  n=new nodol(c);
                  if (cabecera==NULL){
                     cabecera=n;
                  }else{
                        ult->setSiguiente(n);
                  }
                  ult=n;
             }
};


1
2
3
4
5
6
7
8
9
10
11
class nodol{
      private:
              char c;
              nodol *siguiente;
      public:
             nodol(char b):c(b),siguiente(NULL){}
             char getC(){return c;}
             void setC(char ch){c=ch;}
             nodol** getSiguiente(){return &siguiente;}
             void setSiguiente(nodol *n){siguiente = n;}
};


My questions comes on the getSiguiente() function: It is suppoused to return the next object refered (in java)... And I do not know what means nodol** and why it returns &siguiente... The code works fine... though i belive it can be improved obviously:

test code:

1
2
3
4
5
6
7
8
9
10
     lista *cont;
     cont = new lista();
    cont->insertar('1');
    cont->insertar('2');
    nodol *c;
    c=&cont->getCabecera();// here my code to get is different
    while (c!=NULL){
          cout<<c->getC();
          c=*c->getSiguiente();// if I use a similar function to getCabecera(); the programa crashes
    }


if I use a similar function to getCabecera() on getSiguiente(); the programa crashes...why?
Last edited on
For linked lists, I don't recommend using classes. Though it's possible, most programmers here will use a far simpler type of data structure called struct.
http://cplusplus.com/doc/tutorial/structures/

I have no idea what the heck that code is supposed to do, but it's contorted, overblown, and completely messed up. All you need for a linked list using classes is an instance of the class within itself (private), some other value (also private), and a function to return the value of the pointer, and maybe a function to return the value of that other value.

-Albatross
Last edited on
Actually that code works fine... for the moment. Im using clases cuz its my homework... im supposed to use clases. I know it can b used with structures...

Well the idea of the code (test code) adds chars '1','2' and print each ('1','2') char.

It works... but my question indeed is about pointers...

nodol getCabecera(){return *cabecera;}

nodol** getSiguiente(){return &siguiente;}

There is abviously a difference between those too.

My actuall Question is why... and why when i use the code from getCabecera IN PLACE OF getSiguiente it crashes...?

Thanks By the way.
Last edited on
nodol** is a pointer to a pointer to nodol. One of those pointers could be an array pointer, or maybe both of them, though probably not both. Either way, this is a way of returning an array of pointers.

As for why it crashes... well... those are two completely different return values, for all practical purposes. If one of those pointers was an array pointer, then the reference operator would have some trouble chewing through that, and probably return something that wasn't quite nodol* with a normal pointer as everyone knows and loves them... that and you're messing around with pointers regarding what the value is passed into.
http://cplusplus.com/doc/tutorial/pointers/

getCabecera en getSiguiente

I can see that you aren't a native English speaker, so just so you know, in English one would say:
getCabecera in place of getSiguiente


-Albatross
Last edited on
I know... I mistyped... Sorry.
Sorry for all I write wrong Im out of practice and im forgetting English...

Thanks guess I got it.
Last edited on
Topic archived. No new replies allowed.