如何获取链表中具有特定值的节点?

How to get a node with specific value in a linked list?

我想得到一个带有bno(分支号)的节点。

struct bank {
    struct branch *branches; // to hold the branches of the bank
    struct operation_type *optypes; // to hold the transaction types offered by the bank
};
struct branch *getProperBranch(struct bank *banka, int entity) {
    while (banka->branches != NULL) {
        if (banka->branches->bno == entity) {
            return banka->branches;
        }
        banka->branches = banka->branches->nextb;
    }
}

当我这样做时,我可以找到它,但 bank->branches 地址已更改。我失去了它原来的价值。我怎样才能在不失去位置的情况下做到这一点? 我需要访问 bank->branches->customers 并在适当的位置添加新客户。 提前致谢。

使用节点类型的临时变量。不要忘记事件中的 return 值 entity 未找到。

struct branch *getProperBranch(struct bank *banka, int entity) {
    for (struct branch *bp = banka->branches; bp; bp = bp->nextb)
        if (bp->bno == entity) 
            return bp;                                                  
                               
    return NULL;
}