我需要用启动指针启动来计算列表中的奇数
struct elem {
int key;
elem *next;
}
*start = NULL;
// List Function
void list() {
if (start)
{
cout << "\nList";
elem *p = start;
while (p)
{
cout << p->key << "\t";
p = p->next;
}
}
else
{
cout << "\nEmpty list";
}
}
//添加
void add(int n) {
if (start==NULL || start ->key > n)
{
add_b(n);
}
else
{
elem *p = start;
while (p->key <= n && p->next)
{
p = p->next;
}
add_e(n);
}
}
//驱动程序功能
int main() {
int d;
do
{
cin >> d;
if (d)
{
add(d), list();
}
} while (d);
system("pause");
return 0;
}
我不知道我需要在哪里开始我的周期来计算奇数。
请有人给我演示或类似的东西,因为我真的不明白该怎么做,这将是非常有帮助的
如果我已经正确理解你需要为列表中的奇数编号列表写一个递归函数。
递归函数可以通过以下方式查看
unsigned int count_odds( struct elem *start )
{
return start == NULL ? 0 : ( start->key & 1 ) + count_odds( start->next );
}
被称为
unsigned int n = count_odds( start );
而不是函数返回类型unsigned int
你也可以使用更好的类型size_t
。
用于计算奇数值的伪代码
int count_odd(list)
{
int count = 0;
for (current_node = start_of_list until end_of_list)
{
if (current_node->value is odd)
++count;
}
return count;
}