The third section contains functions to add, edit, and remove transactions from the transactions linked list, as follows:
struct Node *findNodeById(int id, struct Node *withinNode) {
struct Node *node = withinNode;
while (node != NULL) {
if (node->id == id) return node;
node = node->next;
}
return NULL;
}
void addTransaction(int id, int categoryId, float rawAmount,
float cookedAmount) {
appendNode(&transactionsHead, id, categoryId, rawAmount, cookedAmount);
}
void editTransaction(int id, int categoryId, float rawAmount,
float cookedAmount) {
struct Node *foundNode = findNodeById(id, transactionsHead);
if (foundNode != NULL) {
foundNode->categoryId = categoryId;
foundNode->rawAmount = rawAmount;
foundNode->cookedAmount = cookedAmount;
}
}
void removeTransaction(int id) {
struct Node *foundNode = findNodeById(id, transactionsHead);
if (foundNode != NULL) deleteNode(&transactionsHead, foundNode);
}
The appendNode() and deleteNode() functions we reviewed in the previous section aren't intended to be called from the JavaScript code. Instead, calls to addTransaction(), editTransaction(), and removeTransaction() are used to update the local linked list. The addTransaction() function calls the appendNode() function to add the data passed in as arguments to a new node in the local linked list. The removeTransaction() calls the deleteNode() function to delete the corresponding transaction node. The findNodeById() function is used to determine which node needs to be updated or deleted within the linked list based on the specified ID.