Home > Back-end >  Why is my linked list when being traversed skipping the first node?
Why is my linked list when being traversed skipping the first node?

Time:03-01

I am learning C and I am currently learning about dynamic memory allocation and linked lists and I ran into a problem. I found this post and followed the answer. However my program is still skipping my first node when being printed out.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct record
{
  int id;
  char name[257];
  float age;
  struct record *next;
};

int main(void)
{

  int i = 1;
  struct record *firstrecord = (struct record *)malloc(sizeof(struct record));
  firstrecord->id = 1000;
  char firstname[] = "John Doe";
  memcpy(firstrecord->name, firstname,strlen(firstname)   1);
  firstrecord->age = 24.5;
  firstrecord->next = NULL;

  struct record *mostrecentlycreated = firstrecord;

  int iyes = 1;
  char cyes;
  int yes = 1;
  while (yes == iyes)
  {
    
      // build next node
      mostrecentlycreated->next = (struct record *)malloc(sizeof(struct record));
      // change data in new record
      int newid;
      char newname[257];
      char temp;
      float newage;
      char cnewage[257];
    
      printf("Enter the name: ");
      fgets(newname,257,stdin);
    
      newid = 1000   i;

      printf("Enter the person's age: ");
      fgets(cnewage,257,stdin);
      newage = atof(cnewage);
    
      mostrecentlycreated->id = newid;
      memcpy(mostrecentlycreated->name,newname,strlen(newname)   1);
      mostrecentlycreated->age = newage;
      mostrecentlycreated = mostrecentlycreated->next;
      mostrecentlycreated->next = NULL;


      i  ;
      // decide whether or not to keep going
      printf("Do you wish to continue? 1\\0 ");
      
      fgets(&cyes,1,stdin);
      iyes = atoi(&cyes);
    
    
      scanf("%c",&temp);
    
  }
  struct record *traverse = firstrecord;
  while (traverse != NULL)
  {
    printf("Employee id is %i; Employee Name is %s; Employee Age is %f\n",traverse->id,traverse->name,traverse->age);
    traverse = traverse->next;
  }

  free(firstrecord);
  free(mostrecentlycreated);
  return 0;
}

And this is my output:

Enter the name: john doe
Enter the person's age: 23
Do you wish to continue? 1\0 0
Employee id is 1001; Employee Name is john doe
; Employee Age is 23.000000
Employee id is 0; Employee Name is ; Employee Age is 0.000000

Why does it not print out the first person?

CodePudding user response:

After you create your first node, your loop is creating a new node on each iteration, but it is assigning the user's input to the previous node in the list, not the current node being added to the list. So, on the 1st iteration, you are overwriting the 1st node's data, and then appending a 2nd empty node. Then on the 2nd iteration, you are overwriting the 2nd node's data, and appending a blank 3rd node. And so on.

Try this instead:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct record
{
  int id;
  char name[257];
  float age;
  struct record *next;
};

int main(void)
{
  int i = 1;

  struct record *firstrecord = (struct record *)malloc(sizeof(struct record));
  firstrecord->id = 1000;
  strncpy(firstrecord->name, "John Doe", 257);
  firstrecord->age = 24.5;
  firstrecord->next = NULL;

  struct record *mostrecentlycreated = firstrecord;

  const int cyes = 1;
  int iyes = 1;

  while (iyes == cyes)
  {    
      // build next node
      struct record *newrecord = (struct record *)malloc(sizeof(struct record));

      // change data in new record    
      newrecord->id = 1000   i;

      printf("Enter the name: ");
      fgets(newrecord->name, 257, stdin);
    
      printf("Enter the person's age: ");
      scanf("%f", &(newrecord->age));
    
      newrecord->next = NULL;

      mostrecentlycreated = newrecord;

        i;

      // decide whether or not to keep going
      printf("Do you wish to continue? 1\\0 ");
      scanf("%d", &iyes);
  }

  struct record *traverse = firstrecord;
  while (traverse != NULL)
  {
    printf("Employee id is %i; Employee Name is %s; Employee Age is %f\n", traverse->id, traverse->name, traverse->age);
    traverse = traverse->next;
  }

  traverse = firstrecord;
  while (traverse != NULL)
  {
    struct record *nextrecord = traverse->next;
    free(traverse);
    traverse = nextrecord;
  }

  return 0;
}
  • Related