我在C链接列表中遇到问题(第一个节点为空)

时间:2020-10-12 18:39:16

标签: c linked-list

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。但是,事实并非如此。我不知道哪一部分有错误...

0 个答案:

没有答案