I have this implementation of a class that handles linked lists.
The issue is that I am using primitive functions in the struct that I use inside the class, however I don't know much about it, and I don't know how to call the functions. Here I reproduce my problem. Thanks a lot
#include <iostream>
#include <stdlib.h>
using namespace std;
class linkedList
{
private:
struct node
{
char valor;
node *next;
node( char nuevoValor, node * nuevoNext)
: valor( nuevoValor ), next( nuevoNext )
{ }
// prinmitivos
static node * stringALista(const char *s); // para el constructor
};
node *head;
public:
explicit linkedList( const char * s = "");
//linkedList( const linkedList & s );
};
int main()
{
linkedList list1 ("hola mundo");
}
linkedList::linkedList(const char *s)
{
// como llamar a la funcion stringALista???
}
// aquí ↓ dice 45 11 Error] expected unqualified-id before '.' token
linkedList.node * linkedList::stringALista(const char *s)
{
node * aux = NULL;
node * cur = NULL;
int i = 0;
while ( ( s + i * sizeof(char)) != '\0' )
{
if ( cur == NULL)
{
cur = new node;
cur->valor = &s;
cur->next = NULL
aux = cur;
}
else
{
while (cur->next != NULL)
cur = cur->next;
cur->next = new node;
cur->next->valor = &s;
cur->next->next = NULL;
}
}
return aux;
}
What I want is to be able to use the struct function in the linkedList class.
node
is a nested structure inside the classlinkedList
. The operator.
is used to access the functionality of a specific instance of the class and this is not the case... the structurenode
does not belong to an instance oflinkedList
, so the operator to use is::
. Also, the methodstringALista
belongs to the classnode
, so you need to indicate that scope as well:On the other hand this is wrong:
s
is a pointer and the compiler is smart enough to apply proper offsets to that pointer. So:In pointer arithmetic you don't have to take into account the size of each pointer register. That is a responsibility of the compiler. So that line should look more like this:
In any case, notice that you are not incrementing the value of
i
so the loop will repeat indefinitely.For loops with a defined range, it is usually more practical and clean to have a loop
for
Also, the content of the loop has certain problems. If you look closely you will see that the loop will create as many nodes as there are characters in the string
s
and in each node you try to store a pointer to the string , when what you want is to store each individual character. The loop should look more like this: