/
CoinTree.cpp
141 lines (122 loc) · 3.44 KB
/
CoinTree.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// Created by yolanda on 2/3/2019.
//
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include "CoinTree.h"
#include "CoinNode.h"
CoinTree::CoinTree(char *coinId, char *initialOwner, int value) {
this->coinId = (char*)malloc(strlen(coinId) + 1);
strcpy(this->coinId, coinId);
this->initialOwner = (char*)malloc(strlen(initialOwner) + 1);
strcpy(this->initialOwner, initialOwner);
/* Initialize the coin id with NULL */
this->root = new CoinNode(initialOwner, value, coinId);
this->next = NULL;
}
CoinNode* CoinTree::getRoot(char *coinId) {
CoinTree *current = this;
/* Traverse the list to find the last node */
while( current != NULL ) {
if(strcmp(current->coinId, coinId) == 0) {
break;
}
current = current->next;
}
return current->root;
}
void CoinTree::setRoot(char *owner, int value, char *coinId){
this->root = new CoinNode(owner, value, coinId);
}
/* Find if this coinId exists */
/* If found return 1 else return 0 */
int CoinTree::find(char *coinId) {
int found = 0;
/* Find CoinId */
CoinTree *current = this;
while( current != NULL ) {
if(strcmp(current->coinId, coinId) == 0) {
found = 1;
break;
}
current = current->next;
}
return found;
}
CoinNode* CoinTree::insert(char *coinId, char *initialOwner, int value) {
CoinTree *current = this;
/* Traverse the list to find the last node */
while( current->next != NULL ) {
current = current->next;
}
/* If not found go to next node */
current->next = new CoinTree(coinId, initialOwner, value);
return current->next->root;
}
void CoinTree::print() {
/* Print the current list */
CoinTree *current = this;
while ( current != NULL ) {
printf("Coin Id: %s ", current->coinId);
current = current->next;
}
printf("\n");
}
void CoinTree::printNodes(char *coinId) {
CoinTree *current = this;
while (current != NULL ) {
if(strcmp(current->coinId, coinId) == 0) {
current->root->printNodes(current->root);
break;
}
current = current->next;
}
}
void CoinTree::traceCoin(char *coinId) {
CoinTree *current = this;
while (current != NULL ) {
if(strcmp(current->coinId, coinId) == 0) {
current->root->traceCoin(current->root);
break;
}
current = current->next;
}
}
/* Find the number of transactions */
int CoinTree::findNumberOfTransactions(char *coinId){
int transactions;
CoinTree *current = this;
while (current != NULL ) {
if(strcmp(current->coinId, coinId) == 0) {
transactions = current->root->findNumberOfTransactions(current->root);
break;
}
current = current->next;
}
return transactions;
}
int CoinTree::findUnspent(char *coinId) {
int unspent = 0;
CoinTree *current = this;
while (current != NULL ) {
if(strcmp(current->coinId, coinId) == 0) {
unspent = current->root->findUnspent();
break;
}
current = current->next;
}
return unspent;
}
CoinTree* CoinTree::getNext() {
return this->next;
}
/* Add New Transaction */
// TODO: Delete the current List of CoinTrees
CoinTree::~CoinTree() {
free(this->coinId);
free(this->initialOwner);
/* Remove the Trees */
CoinNode *current = this->root;
current->deleteTree(current);
}