c - 计算C中列表中的不同元素
问题描述
我想计算一个列表的所有不同数字。例如,如果我的列表由 {1,2,1,1,3,4} 组成,我需要返回 4。这是我编写的代码。
struct Node
{
int data;
struct Node *next;
};
typedef struct Node Node_t;
int count_distinct(Node_t * start)
{
Node_t *ptr1, *ptr2;
ptr1 = start;
int counter=0;
/* pick one element*/
while (ptr1 != NULL && ptr1->next != NULL)
{
ptr2 = ptr1;
/* compare picked element with the rest */
while (ptr2->next != NULL)
{
/* If duplicate */
if (ptr1->data == ptr2->next->data)
{
break;
}
else
ptr2 = ptr2->next;
counter++;
}
ptr1 = ptr1->next;
}
return counter;
}
解决方案
推荐阅读
- python - 使用 pyqtgraph 和线程的实时绘图
- php - Laravel 无法路由到 /admin
- php - 在 laravel 中使用多个翻译文件时出现 htmlspecialchars 错误
- html - 如何让一个 div 出现在另一个 div 内而不是在其下方?
- laravel-5 - Laravel 5.8 显示 404 Not Found 错误
- c++ - 运行时检查失败 #0。不知如何使用CDECL
- c# - 信用卡到期检查 Linq C#
- python - 正则表达式在 Python 中替换或添加子字符串
- python-3.x - 如何减少请求数量并仅使用一个?
- python - 删除python中的过多列表