-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataTable.c
More file actions
57 lines (53 loc) · 1.6 KB
/
dataTable.c
File metadata and controls
57 lines (53 loc) · 1.6 KB
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
#include "dataTable.h"
#include "cmdTable.h"
#include "constants.h"
#include "math.h"
#include "converter.h"
/*############STATIC VARIABLES#############*/
dataList data_list = { NULL, 0 };
/*############PRIVATE FUNCTIONS#############*/
/* Creates a data node with a dynamic allocated storage */
static dataNode* createDataNode(int data) {
dataNode* newDataNode = NULL;
if ((newDataNode = malloc(sizeof(dataNode)))) {
newDataNode->value = data;
newDataNode->next = NULL;
}
return newDataNode;
}
/*############PUBLIC FUNCTIONS#############*/
/* Adds data (char or int) to data list */
int addData(unsigned int data) {
dataNode *curr, *newNode;
data_list.length++;
if ((newNode = createDataNode(data % (int) pow(2, WORD_SIZE)))
&& cmd_list.length + data_list.length < MAX_MEMORY_SIZE) {
if (data_list.head == NULL) { /*list is empty*/
data_list.head = newNode;
} else {
/* Skip to the end of the list */
for (curr = data_list.head; curr->next != NULL; curr = curr->next)
;
curr->next = newNode;
}
return true;
}
return false;
}
/* reset table */
void resetDataTable(void) {
dataNode* curr = NULL;
data_list.length = 0;
while ((curr = data_list.head) != NULL) { /* set curr to head, stop if list empty. */
data_list.head = data_list.head->next; /* advance head to next element. */
free(curr); /* delete saved pointer. */
}
}
/* print table */
void printDataTable(int lineNumber) {
dataNode* curr = NULL;
for (curr = data_list.head; curr != NULL; curr = curr->next, lineNumber++) {
printf("%s\t%s\n", valueToBase10DecimalString(lineNumber),
base10to2Wierd(curr->value));
}
}