dimanche 20 décembre 2020

Assign array in Linked List node

I recently got into programming and I just learned about Linked Lists. I know how to assign individual elements like char or int in a node in a Linked List.

I was trying to assign a char array in a single node, and then for each node print the elements of the array.

Here is the part of my code in which I have tried to do the above:

node* ReadList() {
  node *head = NULL, *n, *m;
  char data[20], c;
  bool lmfao = false;
  int i;

  for (i=0; i<20; i++) {
    c = getchar();
    if (c == ' ') break;
    data[i] = c;
  }

  n = new node;
  n->next = NULL;
  n->info = data[];
  head = n;

  while (!lmfao) {

   for (i=0; i<20; i++) {
     c = getchar();
     if (c == ' ') break;
     if (c == EOF) lmfao = true;
     data[i] = c;
   }

   if (!lmfao) {
     m = new node;
     n->next = m;
     m->info = data[];
     m->next = NULL;
     n = m;

   }

   if (lmfao) {
     m = new node;
     n->next = m;
     m->info = data[];
     m->next = NULL;
   }
  }

  return head;
}

For the sake of this example lets say that the series of letters is not longer than 20 characters.

When I try to compile this I get the error message:

test.cpp:23:18: error: expected primary-expression before ']' token
test.cpp:38:21: error: expected primary-expression before ']' token
test.cpp:47:21: error: expected primary-expression before ']' token

I have tried to use data instead of data[] but then i get this:

test.cpp:36:16: error: invalid conversion from 'char*' to 'char' [-fpermissive]

which I totally understand, because I tried to convert a pointer to a char value.

Either way I have no clue as to what I can do to assign the entire array in a single node.

Assigning a single element in each node would not work for what I'm trying to do.

Any suggestions?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire