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

发布于 2025-01-28 13:31:40 字数 1314 浏览 3 评论 0原文

当传递某个语句时,我必须从我的第一个链接列表“ new_queue”中删除头节点,然后将其添加到我的第二个链接列表“ ready_queue”的背面。

当我尝试一下时,头部从“ new_queue”中删除,但不会将其添加到“ ready_queue”的背面,而是总是替换“ ready_queue”中的第二个节点。

我认为这是因为行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;     
        }
    }
}

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".

When I try this the head gets removed from "new_queue" but it does not get added to the back of "ready_queue", instead it always just replaces the second node in "ready_queue".

I think this is because of the line ready_queue_prev -> next = NULL;, but if I remove this line the whole linked list will be put at the back of "ready_queue" instead of just the node.

Anyone know how to solve this?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_畞蕅 2025-02-04 13:31:40

当通过某个语句通过时,我必须删除头节点
从我的第一个链接列表“ new_queue”中,将其添加到我的背面
第二个链接列表“ 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;     
    }

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".

To add a node to the tail of a linked list you have to find the tail.

First of all actually you need to check whether new_queue is not equal to NULL. If it is equal to NULL then there is nothing to append. The if statement (without any else because it is not required) can look the following way

    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;     
    }
花辞树 2025-02-04 13:31:40

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

ready_queue -> next = ready_queue_prev;

ready_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;

This statement is indeed making the node the second one in the list:

ready_queue -> next = ready_queue_prev;

ready_queue is the head of the queue, not the tail. You need to first find out where is the tail, and then do that assignment there:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文