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

C语言中Ship间指定Planet的Delivery链表转移逻辑错误排查

星际配送系统Delivery转移问题排查与修正

核心问题梳理

你遇到的问题本质是链表节点的删除与转移逻辑存在漏洞,导致转移计数和实际链表内容不一致,具体表现为节点未正确从原Ship移除、未正确添加到目标Ship,或计数统计与实际操作不同步。

常见错误点排查

  1. 头节点删除处理遗漏
    很多时候只处理了链表中间节点的删除,没考虑Ship的Delivery链表头就是目标节点的情况,导致原Ship的头指针未更新,残留无效节点。
  2. 链表指针未正确断开
    转移节点时没有将节点的next置为NULL,导致原Ship和目标Ship的链表出现交叉引用,后续遍历或统计时出现异常。
  3. 待配送量统计错误
    统计指定Planet的Delivery数量时逻辑出错,比如漏加某个节点的数量,导致转移方向判断错误(本该A转B却搞成B转A)。
  4. 计数与实际转移不同步
    转移计数只靠变量累加,没有和实际转移的节点数量做校验,或者转移过程中计数逻辑遗漏部分节点。

修正方案(基于C语言示例)

1. 先实现准确的待配送量统计函数

确保转移方向判断的依据正确:

#include <string.h>

typedef struct Delivery {
    char planet[50];
    int quantity;
    struct Delivery* next;
} Delivery;

typedef struct Ship {
    char name[50];
    Delivery* deliveries;
} Ship;

// 统计指定星球的待配送总量
int countDeliveryForPlanet(Ship* ship, const char* targetPlanet) {
    int total = 0;
    Delivery* curr = ship->deliveries;
    while (curr) {
        if (strcmp(curr->planet, targetPlanet) == 0) {
            total += curr->quantity;
        }
        curr = curr->next;
    }
    return total;
}

2. 修正节点转移函数

解决头节点删除、指针断开、链表拼接的问题:

// 将firstShip中指定星球的所有Delivery转移到secondShip
void moveFromFirstToSecond(Ship* firstShip, Ship* secondShip, const char* targetPlanet) {
    Delivery** currPtr = &firstShip->deliveries; // 用二级指针处理头节点删除
    Delivery* targetTail = NULL; // 缓存目标链表尾部,避免重复遍历

    while (*currPtr) {
        Delivery* temp = *currPtr;
        if (strcmp(temp->planet, targetPlanet) == 0) {
            // 从firstShip中移除当前节点
            *currPtr = temp->next;

            // 将节点添加到secondShip的链表尾部
            if (!secondShip->deliveries) {
                secondShip->deliveries = temp;
                targetTail = temp;
            } else {
                targetTail->next = temp;
                targetTail = temp;
            }
            temp->next = NULL; // 强制断开原链表连接,避免交叉引用
        } else {
            currPtr = &(*currPtr)->next;
        }
    }
}

3. 主调用逻辑

严格按照规则执行转移,并返回总待配送数:

int transferDeliveryBetweenShips(Ship* ship1, Ship* ship2, const char* targetPlanet) {
    int cnt1 = countDeliveryForPlanet(ship1, targetPlanet);
    int cnt2 = countDeliveryForPlanet(ship2, targetPlanet);

    // 按规则确定转移方向
    if (cnt1 < cnt2) {
        moveFromFirstToSecond(ship1, ship2, targetPlanet);
    } else if (cnt2 < cnt1) {
        moveFromFirstToSecond(ship2, ship1, targetPlanet);
    } else {
        // 数量相同,Ship2转Ship1
        moveFromFirstToSecond(ship2, ship1, targetPlanet);
    }

    // 返回转移后的总待配送数
    return countDeliveryForPlanet(ship1, targetPlanet) + countDeliveryForPlanet(ship2, targetPlanet);
}

测试验证要点

  • 测试头节点转移场景:比如Ship1的第一个Delivery就是目标星球,验证Ship1的头指针是否更新为空,Ship2是否正确接收节点。
  • 测试中间节点转移场景:目标节点在链表中间,验证原Ship的链表是否跳过该节点,目标Ship的链表是否正确拼接。
  • 测试数量相同场景:确保Ship2的对应Delivery全部转移到Ship1。
  • 验证转移后的计数:对比统计函数的返回值和实际链表节点的数量总和,确保一致。

内容的提问来源于stack exchange,提问作者user23691974

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 15:02:39