-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin.c
More file actions
61 lines (53 loc) · 1008 Bytes
/
coin.c
File metadata and controls
61 lines (53 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "coin.h"
#include "log.h"
coin_t* coin_new(transaction_t* transaction, int index, int amount) {
coin_t* coin;
coin = (coin_t*)calloc(1, sizeof(coin_t));
if (coin == NULL) {
log_printf(LOG_ERROR, "Unable to allocate new coin\n");
return NULL;
}
coin->transaction = transaction;
coin->index = index;
coin->amount = amount;
coin->next = NULL;
return coin;
}
void coin_free(coin_t* coin) {
coin->transaction = NULL;
free(coin);
return;
}
coin_t* coin_add_coins(coin_t* prev, coin_t* next) {
coin_t* head;
if (prev == NULL) {
return next;
}
if (next == NULL) {
return prev;
}
head = prev;
while (prev->next != NULL) {
prev = prev->next;
}
prev->next = next;
return head;
}
void coin_free_coins(coin_t* head) {
coin_t* tmp;
while (head != NULL) {
tmp = head->next;
coin_free(head);
head = tmp;
}
return;
}
int coin_sum_coins(coin_t* coin) {
int value;
value = 0;
while (coin != NULL) {
value += coin->amount;
coin = coin->next;
}
return value;
}