-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.c
More file actions
72 lines (63 loc) · 1.68 KB
/
table.c
File metadata and controls
72 lines (63 loc) · 1.68 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* table.c implements a <string,int> key/value data structure
*
* @author Dr. Fenwick
* @version Fall 2014
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "table.h"
void initTable(tableType *Xtable)
{
Xtable->numItemsInUse = 0;
}
/* When storing, it is necessary to check if the variable is already
present in the table.
If it is, then rewrite the value as opposed to creating a completely
new entry.
*/
void store(tableType *Xtable, char *key, int val)
{
int i;
for(i = 0; i < Xtable->numItemsInUse; i++){
//First pass through to see if value is already in the table.
if(strcmp(key,Xtable->entry[i].key) == 0)
{
//If it is, overwrite the value and return
/* WRITE THIS STATEMENT */
Xtable->entry[i].value = val;
return;
}
}
// If reach this point then the value is not already present, add it.
/* WRITE CODE HERE */
Xtable->entry[i].value = val;
strcpy(Xtable->entry[i].key, key);
Xtable->numItemsInUse++;
}
int retrieve(tableType *Xtable, char *key){
//Search for entry. If found return value. If not, error occurred.
/* WRITE CODE HERE */
int i;
for(i = 0; i < Xtable->numItemsInUse; i++)
{
if(strcmp(key, Xtable->entry[i].key) == 0)
{
return Xtable->entry[i].value;
}
}
// Not found, report error.
printf("ERROR, non-existant key %s in table.\n", key);
exit(1);
return 0;
}
//for debugging
void printTable(tableType * Xtable)
{
int i;
for(i = 0; i < Xtable->numItemsInUse; i++)
{
printf("%s = %d\n", Xtable->entry[i].key, Xtable->entry[i].value);
}
}