将头节点从第一个链接列表移动到第二个链接列表的背面
当传递某个语句时,我必须从我的第一个链接列表“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要在链接列表的尾部添加一个节点,您必须找到尾巴。
首先,实际上您需要检查
new_queue
不等于null
。如果等于null
,则没有什么可附加的。 if语句(没有其他情况,因为不需要)可以看以下方式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 toNULL
. If it is equal toNULL
then there is nothing to append. The if statement (without any else because it is not required) can look the following way该语句的确使该节点成为列表中的第二个:
ready_queue
是队列的头部,而不是尾巴的头。您需要首先找出尾巴在哪里,然后在那里进行该分配:This statement is indeed making the node the second one in the list:
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: