c - 在单链表中查找并移动

标签 c

我的代码中不断出现错误,但无法找出问题所在。 该代码应该找到包含 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/

相关文章:

c - Linux 上是否有映射到 IP 协议(protocol)字段的标准枚举?

c - 使用ftok()创建 key 以及如何使用 key ?

c++ - C中的静态和C++中的静态之间的区别??

c - 全局结构的内存分配

c - 用正确的整数类型替换数组访问变量

C write()函数发送2个字节的十六进制

c - 等待 fork 的 child ,直到它启动另一个程序

c - 微 Controller 编程中ISR函数中 volatile 关键字的使用

c++ - nm 获取整个存档的 undefined symbol ,而不是单独的目标文件

`fork: retry: No child processes`的原因