将头节点从第一个链表移动到第二个链表的后面

Move the head node from a first linked list to the back of a second linked list

当通过某个 if 语句时,我必须从我的第一个链表“new_queue”中删除头节点,并将其添加到我的第二个链表“ready_queue”的后面".

当我尝试这个时,头部从“new_queue”中移除,但它不会添加到“ready_queue”的后面,而是总是替换“[]中的第二个节点=21=]".

我认为这是因为行 ready_queue_prev -> next = NULL;,但如果我删除此行,整个链表将被放在“ready_queue”的后面,而不仅仅是节点。

有人知道怎么解决吗?

typedef struct ST_PCB {
  int arrival_time;
  char name[9];
  int duration;
  struct ST_PCB * next;
} T_PCB;


int main(void){
    // serial list of newly arrived tasks
    T_PCB * new_queue = NULL;
    // circular list of active tasks
    T_PCB * ready_queue = NULL;
    // extra state needed to switch tasks 
    // from new_queue to ready_queue when they're started
    T_PCB * ready_queue_prev = NULL;

    //this constructs the linked-list and sorts it by arrival time
    new_queue = read_tasks();
    new_queue = sort_tasks_on_arrival(new_queue);


    if(something happends...){
        if(ready_queue != NULL){
            ready_queue_prev = new_queue;
            new_queue = new_queue->next;      
            ready_queue -> next = ready_queue_prev;
            ready_queue_prev -> next = NULL;
        }
        else{
            ready_queue = new_queue;
            new_queue = new_queue->next;
            ready_queue->next = NULL;     
        }
    }
}

这条语句确实使节点成为列表中的第二个:

ready_queue -> next = ready_queue_prev;

ready_queue是queue的头,不是尾。你需要先找出尾巴在哪里,然后在那里做作业:

T_PCB * tail = ready_queue;
while (tail->next != NULL) {
    tail = tail->next;
}
// We found the tail. Now make the assignment to `next`:
tail->next = ready_queue_prev;
// And now continue with what you had:
ready_queue_prev->next = NULL;

When a certain if statement is passed i have to remove the head node from my first linked-list "new_queue" and add this to the back of my second linked-list "ready_queue".

要将节点添加到链表的尾部,您必须找到尾部。

首先实际上你需要检查new_queue是否不等于NULL。如果它等于 NULL 那么就没有什么可以追加的了。 if 语句(没有任何其他因为它不是必需的)可以看起来像下面的方式

    if ( new_queue != NULL)
    {
        T_PCB *tmp = new_queue;
        new_queue = new_queue->next;
        tmp->next = NULL;

        T_PCB **current = &ready_queue;
        while ( *current != NULL ) current = &( *current )->next;
        *current = tmp;     
    }