home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.disi.unige.it
/
2015-02-11.ftp.disi.unige.it.tar
/
ftp.disi.unige.it
/
pub
/
.person
/
CataniaB
/
teach-act
/
esempi
/
Comp-Sep
/
table.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-03-11
|
948b
|
57 lines
#include <stdio.h>
#include <stdlib.h>
#include "table.h"
#define MAX_SIZE 100
table newTab(void)
{
table t;
int i;
t = (table) calloc(MAX_SIZE,sizeof(struct tab));
for (i=0;i<MAX_SIZE;i++)
t[i].key=t[i].val=0;
return t;
}
void insert(table t,int key, int val)
{
int i=0;
/* find the key */
while(i<MAX_SIZE && t[i].val && t[i].key!=key)
i++;
if (i==MAX_SIZE) /* error: full table */
{
printf("\nError: table is full\n");
return;
}
if (!t[i].val) /* create a new association */
t[i].key=key;
t[i].val=val; /* update the association */
}
int getVal(table t,int key)
{
int i=0;
/* find the key */
while(i<MAX_SIZE && t[i].val && t[i].key!=key)
i++;
if (!t[i].val) /* key not found */
return 0;
else return t[i].val;
}
void print(table t)
{
int i=0;
while(i<MAX_SIZE && t[i].val)
{
printf("Value at key %d is %d\n",t[i].key,t[i].val);
i++;
}
}