C语言 如何在链表中获得具有特定值的节点?



我想获得一个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并在适当的位置添加新客户。提前谢谢。

使用节点类型的临时变量。如果没有找到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;
} 

相关内容

  • 没有找到相关文章

最新更新