Home inserting a new node on a linked list creates a new one
Reply: 1

inserting a new node on a linked list creates a new one

jonelearn
1#
jonelearn Published in 2017-12-07 12:14:53Z

i'm trying to create a linked list, which creates fine but when i try to insert new nodes to the already non-empty list and display it, only the new inserted node exist. here's the code:

struct patient{
  char name[NAME];
  int ttime;
  int atime;
  int priority;
  struct patient *next;
};

struct wr{
  struct patient *fnode;
  struct patient *lnode;
}*wr;

void wr_insert(struct patient *node){
  if(wr->fnode == NULL){
    wr->fnode = node;
    wr->lnode = node;
  }

  else{

    wr->lnode->next = node;

    wr->lnode = node;

  }

}
void display(){
  struct patient *tmp = wr->fnode;
  while(tmp != NULL){
    printf("%s %d %d %d\n",tmp->name,tmp->ttime,tmp->atime,tmp->priority);
    tmp = tmp->next;
  }
}
    void new_patient(char *line){
      int i,check;
      listsem = sem_open(lISTSEM, 0);
      char *token;
      token = strtok(line," ");
      check = atoi(token);
      if(check == 0){
        struct patient *node = malloc(sizeof(struct patient));
        strcpy(node->name,token);
        node->ttime = atoi(strtok(NULL," "));
        node->atime = atoi(strtok(NULL," "));
        node->priority = atoi(strtok(NULL," "));
        node->next = NULL;
        sem_wait(listsem);
        wr_insert(node);
        sem_post(listsem);
      }
      else{
        struct patient *group = malloc(sizeof(struct patient));
        sprintf(group->name,"20171201-%d",groupnum);
        group->ttime = atoi(strtok(NULL," "));
        group->atime = atoi(strtok(NULL," "));
        group->priority = atoi(strtok(NULL," "));
        group->next = NULL;
        groupnum++;
        sem_wait(listsem);
        wr_insert(group);
        sem_post(listsem);
        for(i=1;i<check;i++){
          struct patient *node = malloc(sizeof(struct patient));
          sprintf(node->name,"20171201-%d",groupnum);
          node->ttime = group->ttime;
          node->atime = group->atime;
          node->priority = group->priority;
          node->next = NULL;
          groupnum++;
          sem_wait(listsem);
          wr_insert(node);
          sem_post(listsem);
        }
        display();
      }
      get_patient();
    }

So for example, if i input through the pipe "3 10 20 30" it rightfully displays the 3 nodes i've created. However if i do it again, when it should print 6 nodes, it only prints 3 again, but the new ones.

user3629249
2#
user3629249 Reply to 2017-12-08 06:23:52Z

this struct definition/instance:

struct wr
{
    struct patient *fnode;
    struct patient *lnode;
} *wr;

is not correct, because the root pointers are fine, but the the instance of wr needs to be an instance of the struct, not a pointer to an instance of the struct.

Suggest:

struct wr
{
    struct patient *fnode;
    struct patient *lnode;
} wr;

then the rest of the code needs to treat wr as an instance of the struct and not a pointer to the struct So this:

void wr_insert(struct patient *node)
{
     if(wr->fnode == NULL)
     {
         wr->fnode = node;
         wr->lnode = node;
     }

     else
     {
         wr->lnode->next = node;
         wr->lnode = node;
     }

should be:

void wr_insert(struct patient *node)
{
     if(wr.fnode == NULL)
     {
         wr.fnode = node;
         wr.lnode = node;
     }

     else
     {
         wr.lnode->next = node;
         wr.lnode = node;
     }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307507 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO