我的代码中不断出现错误,但无法找出问题所在。 该代码应该找到包含 findValue 的第一个节点,并将该节点从当前位置移动到列表的头部。
以下是我收到的错误消息 -
list.c:56:15: error: invalid operands to binary expression
('int (*)()' and 'int')
count << node->value << " ";
功能:
void findAndMove(listNode **listPtr, int findValue) {
if (*listPtr == NULL || (*listPtr)->next == NULL)
return;
listNode *secLast = NULL;
listNode *last = *listPtr;
while (last->next != NULL)
{
if(last->value == findValue)
{
break;
}
secLast = last;
last = last->next;
}
secLast->next = last->next;
last->next = *listPtr;
*listPtr = last;
}
void push(listNode** head_ref, int new_data)
{
listNode* new_node = new_listNode();
new_node->value = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(listNode *node)
{
while(node != NULL)
{
count << node->value << " ";
node = node->next;
}
}
最佳答案
void printList(listNode *node)
{
while(node != NULL)
{
count << node->value << " ";
node = node->next;
}
}
这行带有 count << node->value << " ";
看起来您正在尝试使用 C++ 标准命名空间中的 cout 。在 C 中,您需要使用 printf,如下所示:
void printList(listNode *node)
{
while(node != NULL)
{
//count << node->value << " ";
printf("%d \n", node->value); //IDK what the print should be because you didnt post the node struct, but you need to use a function from the printf family if your printing
node = node->next;
}
}
编辑当我第一次读到你的问题时,我以为你正在做一些位移,直到我读到函数名称来尝试确定你想要做什么。我假设带有“”的移位将移位 32 位,然后在某个 int 计数上移位节点->值,该整数永远不会被分配,因此它将被优化。
关于c - 在单链表中查找并移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55111737/