【发布时间】:2010-02-21 06:07:49
【问题描述】:
int findLargest (ListNode *p)
// --------------------------------------------------------------------------
// Preconditions: list head pointer is passed as a parameter.
// Postconditions: returns the largest value in the linked list.
// --------------------------------------------------------------------------
{
if (p->item != NULL)
{
int largest = p->item;
if (largest > p->next->item)
...
}
...
}
是否可以编写这个递归函数,只传递一个指针作为参数?如果不添加更多参数,我无法弄清楚如何做到这一点。有任何想法吗?我只使用顺序搜索。没什么特别的。
这是需要的类 List 的一部分:
struct ListNode
{
ListItemType item; // A data item on the list.
ListNode *next; // Pointer to next node
}; // end ListNode
ListNode *head; // Pointer to linked list of items.
我主要担心问题的可行性。可以只用指针作为参数吗?
【问题讨论】:
-
请给出List的定义...
标签: c++ recursion linked-list