大门|大门CS 2010 |问题36

下面的C函数将简单链表作为输入参数。它通过将最后一个元素移动到列表的前面来修改列表,并返回修改后的列表。代码的某些部分留空。

null

typedef struct node
{
int value;
struct node *next;
}Node;
Node *move_to_front(Node *head)
{
Node *p, *q;
if ((head == NULL: || (head->next == NULL))
return head;
q = NULL; p = head;
while (p-> next !=NULL)
{
q = p;
p = p->next;
}
_______________________________
return head;
}


选择正确的替代来替换空白行。 (A) q=零;p->next=头部;头=p; (B) q->next=NULL;头=p;p->next=头部; (C) 头=p;p->next=q;q->next=NULL; (D) q->next=NULL;p->next=头部;头=p; 答复: (D) 说明: 当while循环结束时,q包含倒数第二个节点的地址,p包含倒数第二个节点的地址。所以我们需要在while循环之后做以下事情。 i) 将q的next设置为NULL(q->next=NULL)。 ii)将p的下一个设置为头部(p->next=头部)。 iii)将头部设为p(头部=p) 必须在步骤(iii)之前执行步骤(ii)。如果我们先改变头部,那么我们就失去了原始链表中头部节点的轨迹。 看见 https://www.geeksforgeeks.org/move-last-element-to-front-of-a-given-linked-list/ 更多细节。 这个问题的小测验

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享