首页 > 解决方案 > 我在 C 的链表中有问题(第一个节点为空)

问题描述

struct process {
  int id;
  struct process * prev;
  struct process * next;
}

struct process * ready = NULL;
struct process * running;

int main() {
  struct process * curr = NULL;
  curr = ready;
  while (curr != NULL) {
    curr = curr - > next;
  }
  struct process * tmp = (struct process * ) malloc(sizeof(struct process));
  //running->id=1  !!!
  tmp = running;
  printf("%d", tmp - > id); //it prints 1
  tmp - > next = NULL;
  curr = tmp;
  printf("%d", curr - > id); //it prints 1
}

我制作了这样的链表。在 while 循环之前,*ready 为空。我认为 curr 指向准备就绪,我将运行的 id 复制到 curr 的第一个节点的 id。所以我认为 ready 的 id 将是 1。但是,它没有。我不知道哪个部分有错误...

标签: clinked-list

解决方案


推荐阅读