You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言50-80随机数奇偶分类存入链表去重降序问题排查

代码核心问题

  • 循环条件错误:原代码循环条件用了位运算符&而非逻辑运算符,且逻辑不对:只要奇偶任意一个链表没填满10个元素就需要继续生成随机数,应该用||逻辑或判断。
  • 重复校验逻辑失效:仅校验了当前遍历到的节点值和新值是否相等,没有遍历完整链表,且带头结点的头节点本身不存有效数值,这个校验本身就没有意义。
  • 计数逻辑错误:无论新生成的数是否重复、是否成功插入链表,都会直接给对应计数器+1,导致计数器虚高,最终链表实际元素不足10个就提前退出循环。
  • 插入逻辑漏洞:检测到重复值释放节点后,仍执行了链表插入的赋值操作,会产生野指针同时破坏链表结构。

修正后完整代码

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>

typedef struct list numbers;
typedef struct list* Pos;

int PrintRandom();
Pos CreateNew();
int PrintList(Pos);
// 新增:校验对应链表中是否已存在目标值
int IsExist(Pos head, int num) {
    Pos p = head->next;
    while (p != NULL) {
        if (p->number == num)
            return 1;
        p = p->next;
    }
    return 0;
}

struct list {
    int number;
    Pos next;
};

int PrintList(Pos P) {
    if (P == NULL)
        printf("Empty list.\n");
    while (P != NULL) {
        printf("%d\n", P->number);
        P = P->next;
    }
    return 0;
}

Pos CreateNew() {
    Pos q = NULL;
    q = (Pos)malloc(sizeof(struct list));
    if (q == NULL) return NULL;
    q->next = NULL;
    return q;
}

int PrintRandom() {
    int x = 0, max = 80, min = 50;
    x = (rand() % (max - min + 1) + min);
    printf("Random number is : %d\n", x);
    
    return x;
}

int main() {
    srand(time(0));
    Pos even, odd, q = NULL;
    even = malloc(sizeof(numbers));
    odd = malloc(sizeof(numbers));
    even->next = odd->next = NULL;
    int count_even = 0, count_odd = 0, j;
    Pos head_p = even, head_n = odd;

    // 修改循环条件:只要有一个链表没满就继续执行
    while (count_even < 10 || count_odd < 10) {
        j = PrintRandom();
        if (j % 2 == 0) {
            // 偶数链表已经满了就直接跳过本次
            if (count_even >= 10)
                continue;
            // 先校验是否重复,重复就跳过
            if (IsExist(head_p, j))
                continue;
            q = CreateNew();
            if (q == NULL) return -1;
            q->number = j;
            // 找到降序插入的位置
            while (even->next != NULL && even->next->number > q->number) {
                even = even->next;
            }
            q->next = even->next;
            even->next = q;
            even = head_p;
            // 只有插入成功才给计数器+1
            count_even++;
        }
        else {
            // 奇数链表已经满了就直接跳过本次
            if (count_odd >= 10)
                continue;
            // 先校验是否重复,重复就跳过
            if (IsExist(head_n, j))
                continue;
            q = CreateNew();
            if (q == NULL) return -1;
            q->number = j;
            // 找到降序插入的位置
            while (odd->next != NULL && odd->next->number > q->number) {
                odd = odd->next;
            }
            q->next = odd->next;
            odd->next = q;
            odd = head_n;
            // 只有插入成功才给计数器+1
            count_odd++;
        }
    }

    printf("Even numbers in list are:\n");
    PrintList(head_p->next);
    printf("Odd numbers in list are:\n");
    PrintList(head_n->next);

    // 内存释放,避免泄漏
    Pos temp;
    while (head_p != NULL) {
        temp = head_p;
        head_p = head_p->next;
        free(temp);
    }
    while (head_n != NULL) {
        temp = head_n;
        head_n = head_n->next;
        free(temp);
    }

    return 0;
}

内容的提问来源于stack exchange,提问作者duje.je

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 00:00:03