Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - Linked list next node is null

Tags:

c

linked-list

I'm trying to obtain the next value of my struct by using a linked list but the next value is null.

I know that I'm not doing this the right way, hope you can help me to solve my problem.

Input file:

new_employee Hasselhoff David PDG F-13000 1 
new_employee Anderson Pamela DDR F-31000 2 
new_employee LeeNolin Gena DDR F-94270 3
new_employee Charvet David DPR F-54000 4

The code:

char    *my_strcat(char *s1, char *s2)
{
  char  *tmp;
  int   i;
  int   j;

  i = -1;
  j = 0;
  tmp = malloc(sizeof(char) * (strlen(s1) + strlen(s2)) + 1);
  while (s1[++i])
    tmp[j++] = s1[i];
  i = -1;
  while (s2[++i])
    tmp[j++] = s2[i];
  tmp[j] = '\0';
  return (tmp);
}

char    *read_func()
{
  char *str;
  char line[256];

  str = malloc(sizeof(char) * 256);
  while (fgets(line, sizeof(line), stdin))
    str = my_strcat(str, line);
  return (str);
}

void            fill_struct(t_emp *s_emp)
{
  char          *read;

  read = read_func();
  if (strstr(read, "new_employee"))
    {
      while (s_emp->next != NULL)
        s_emp = s_emp->next;
      s_emp->next = malloc(sizeof(t_emp));
      strdup(strtok(read, " "));
      s_emp->name = strdup(strtok(NULL, " "));
      s_emp->forename = strdup(strtok(NULL, " "));
      s_emp->job = strdup(strtok(NULL, " "));
      s_emp->zipcode = strdup(strtok(NULL, " "));
      s_emp->id = atoi(strtok(NULL, " "));
      s_emp = s_emp->next;
    }
}

void    print_emp(t_emp *s_emp)
{
  while (s_emp != NULL)
    {
      printf("******************************\n");
      printf("%s %s\n", s_emp->forename, s_emp->name);
      printf("position: %s\n", s_emp->job);
      printf("city: %s\n", s_emp->zipcode);
      s_emp = s_emp->next;
    }
}

main function:

int     main()
{
  t_emp s_emp;

  if (!isatty(STDIN_FILENO))
    {
      fill_struct(&s_emp);
      print_emp(&s_emp);
    }
  else
    show_help();
}

output:

******************************
David Hasselhoff
position: PDG
city: F-13000
******************************
(null) (null)
position: (null)
city: (null)

desired output:

******************************
David Hasselhoff
position: PDG
city: F-13000
******************************
Pamela Anderson
position: DDR
city: F-31000

Thanks!

like image 593
ap705 Avatar asked Feb 24 '26 10:02

ap705


1 Answers

You need to call fill_struct in a loop; the if statement will only run once, and it will stop after the first employee. The best way to do this is simply modifying the if statement in that function to a while one. For example:

//...
while (strstr(read, "new_employee") != NULL)
//...

Leveraging the fact that strstr will return NULL if the next occurrence of "new_employee" is not found.

As the comments have pointed out, your first s_emp = s_emp->next in the while loop is unnecessary as it will always be NULL; you don't assign s_emp->next until the next line.

Furthermore, your first call to strdup is not valid as you must free the pointer returned from that call. So store it in a variable and free it later.

Aside: why is your my_strcat function so complicated? strcat is simply the equivalent of this:

char *p = str1;

while (*p) //Traverse to the end of str1 
  p++;
while (*p++ = *str2++) //Will stop with copying '\0' over
  ;

return str1;

It need not be more complicated than that.

like image 182
iRove Avatar answered Feb 27 '26 00:02

iRove