-
Notifications
You must be signed in to change notification settings - Fork 0
/
vartable.c
55 lines (48 loc) · 1.08 KB
/
vartable.c
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
#include "vartable.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct Node_s
{
struct Node_s* next;
char* name;
double value;
} Node;
// linked list head
static Node* head = NULL;
void setvar(const char* name, double value)
{
// search for existing variable
Node** node_ptr = &head;
while (*node_ptr != NULL)
{
if (strcmp(name, (*node_ptr)->name) == 0)
{
(*node_ptr)->value = value;
return;
}
node_ptr = &(*node_ptr)->next;
}
// variable does not exist already, so add a new one
Node* tail = malloc(sizeof(Node));
tail->next = NULL;
tail->value = value;
tail->name = malloc(strlen(name) + 1);
strcpy(tail->name, name);
*node_ptr = tail;
}
bool getvar(const char* name, double* value)
{
// search for existing variable
Node* node = head;
while (node != NULL)
{
if (strcmp(name, node->name) == 0)
{
*value = node->value;
return true;
}
node = node->next;
}
return false;
}