c - 在链表上插入一个新节点会创建一个新节点

标签 c linux pointers unix linked-list

我正在尝试创建一个链接列表,它创建良好但是当我尝试将新节点插入已经非空的列表并显示它时,只有新插入的节点存在。这是代码:

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();
    }

例如,如果我通过管道 "3 10 20 30" 输入,它会正确显示我创建的 3 个节点。但是,如果我再做一次,当它应该打印 6 个节点时,它只会再次打印 3 个,但会打印新的。

最佳答案

这个结构定义/实例:

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

不正确,因为根指针很好,但是 wr 的实例需要是结构的实例,而不是指向结构实例的指针。

建议:

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

然后其余代码需要将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 wr_insert(struct patient *node)
{
     if(wr.fnode == NULL)
     {
         wr.fnode = node;
         wr.lnode = node;
     }

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

关于c - 在链表上插入一个新节点会创建一个新节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47694937/

相关文章:

linux - 获取自 Linux 上的 Epoch、Bash 以来的当前时间(以秒为单位)

Python 创建一个包含打乱元素并按对象引用的子列表

c - 返回值3221225725,可能缓冲区溢出?

c - 在 c 中,sizeof()..[-1] 是什么意思

c - 作为位表示的空字符串输入

linux - 如何在 windows 10 linux 子系统上卸载 terraform 并安装更新版本的 terraform?

c - 有符号整数的未定义行为和 Apple 安全编码指南

python - 从 Python 脚本运行时找不到 Nmap

c - 即使使用 -gdwarf-2,GCC 4.8 也会在编译单元 header 中插入版本 4

C 中的 CreateProcess 关闭控制台太快