gpt4 book ai didi

c - 链表字段添加

转载 作者:行者123 更新时间:2023-11-30 16:09:56 25 4
gpt4 key购买 nike

我正在尝试创建我的 findUpdate 方法,该方法使用 accountNumber = accountbalance = amount 更新我的链接列表。如果帐号不存在,则会创建一个新节点。

但是,我不确定如何将节点添加到链表中。另外,我应该像这样在这里初始化前置变量 null 吗?

包括

struct ACCOUNT {

int accountNumber;
float balance;
struct ACCOUNT *next;
};


struct ACCOUNT *head = NULL;
void findUpdate(int account, float amount) {
struct ACCOUNT* temp = (struct ACCOUNT*) malloc(sizeof(struct ACCOUNT));
temp->accountNumber = account;
temp->balance = amount;
}

非常感谢您的帮助。

最佳答案

我无法发表评论,所以我将发布作为答案。

您应该使用 NULL 进行初始化。要添加,取决于您要在哪里添加新节点。在列表的开头、中间还是末尾?

有一个非常好的教程here这解释了每一种情况。

关于c - 链表字段添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58986490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com