如何计算并返回指向max元素的指针的值?C++
How do I calculate and return the value of pointer to max element? C++
我在这个问题上遇到了极大的困难。这是我编码的,但它不起作用。这些是说明书。待办事项:在此处插入代码进行计算并返回指向最大元素的指针值(如果平局,则为第一个)
Node * pointerToMax(LinkedList *list) {
assert(list!=NULL);
assert(list->head != NULL);
Node *max = list->head;
for (list->head=list->head; list!=NULL; list->head=list->head->next){
if (list->head->next > max){
max = list->head->next;
}
}
return max;
}
让我们假设节点是由其数据成员data
进行比较的(或者由数据成员value
或其他数据成员进行比较,您没有显示节点必须如何进行比较)。然后代码可以看起来像
Node * pointerToMax( LinkedList *list )
{
//assert(list!=NULL);
//assert(list->head != NULL);
if ( list == NULL || list->head == NULL ) return NULL;
Node *max = list->head;
for ( Node *node = list->head->next; node !=NULL; node = node->next )
{
if ( max->data < node->data ) max = node;
}
return max;
}
如果Node类定义了用于比较节点的operator <
,那么您应该替换语句
if ( max->data < node->data ) max = node;
对于
if ( *max < *node ) max = node;
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 使用不带参数的函数访问结构元素
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- C++如何通过用户输入删除列表元素
- lower_bound()返回最后一个元素
- 基于多个条件处理地图中的所有元素
- 调整大小后指向元素值的指针unordered_map有效?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 使用函数"remove"删除重复元素
- 具有最大子序列大小的序列,每个元素都相同
- 从自定义类获取对象向量中的 max 元素
- 如何在C++中递归地找到max元素的索引?
- 删除 max 并添加新元素 c++ std::make_heap 时进行单个'Heapify'调用
- 如何在标准库中更改堆C++ max 元素
- 如何计算并返回指向max元素的指针的值?C++
- C++在数组中查找max(按行)和min(按列)元素
- Max和Min用相同的元素堆
- 如何在 C++ 中查找向量的第 m 和第 n 个元素之间的 max 元素