我正在尝试构建一个有序链表,而不会在新的有序列表中找到重复数据。说f_total
有这个值:
f_total: 48 42 45 50 42 48 48 43
新的链表应该是这样的:
42 43 45 48 50
代码更新:
typedef struct _sigma {
int algo;
int f;
int c;
node_p list;
struct _sigma *next;
} sigma, *sigma_p;
sigma_p sigma_tmp = NULL;
sigma_p sigma_new = NULL;
sigma_p sigma_optimal = NULL;
int f_total = 0;
f_total = exectasks(m, nod_tmp, &c_max, &cmin); // f_total will be updated after each time exectasks function return.
...
if(sigma_optimal == NULL)
{
sigma_optimal = (sigma *)malloc(sizeof(sigma));
sigma_optimal->algo = 0;
sigma_optimal->f = f_optimal;
sigma_optimal->c = cmin;
sigma_optimal->list = nod_tmp;
sigma_optimal->next = NULL;
}
else
{
sigma_new = (sigma *)malloc(sizeof(sigma));
sigma_new->algo = 0;
sigma_new->f = f_optimal;
sigma_new->c = cmin;
sigma_new->list = nod_tmp;
sigma_tmp = sigma_optimal;
while(sigma_tmp != NULL && f_optimal > sigma_tmp->f)
{
sigma_prev = sigma_tmp;
sigma_tmp = sigma_tmp->next;
}
if(sigma_tmp != NULL)
{
if(sigma_tmp->f != f_optimal)
{
if(sigma_tmp == sigma_optimal)
{
sigma_new->next = sigma_optimal;
sigma_optimal = sigma_new;
}
else if(sigma_tmp->next == NULL)
{
sigma_prev->next = sigma_new;
sigma_new->next = sigma_tmp;
}
else if(sigma_tmp == NULL)
{
sigma_tmp->next = sigma_new;
sigma_new->next = NULL;
}
}
}
}
但是在这里我有两个问题:
- 它没有正确排序列表。
- 它不会删除所有重复项。