61 lines
1.0 KiB
C
61 lines
1.0 KiB
C
// Laboratorio 9 - Esercizio 1 - ST.c
|
|
// Matteo Schiff - s295565
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "ST.h"
|
|
|
|
struct st {
|
|
char **table;
|
|
int N;
|
|
int maxN;
|
|
};
|
|
|
|
ST STinit(int N) {
|
|
ST st = malloc(sizeof(*st));
|
|
|
|
st->table = malloc(N * sizeof(char *));
|
|
for (int i = 0; i < N; i++) {
|
|
st->table[i] = (char *)malloc(MAX_LEN+1*sizeof(char));
|
|
}
|
|
st->N = 0;
|
|
st->maxN = N;
|
|
|
|
return st;
|
|
}
|
|
|
|
void STfree(ST st) {
|
|
free(st->table);
|
|
free(st);
|
|
}
|
|
|
|
int STinsert(ST st, char * label) {
|
|
if (st->N == st->maxN) {
|
|
printf("Errore, spazio esaurito nella ST\n");
|
|
return -1;
|
|
}
|
|
|
|
strncpy(st->table[st->N++], label, MAX_LEN+1);
|
|
return st->N-1;
|
|
}
|
|
|
|
char * STsearchByIndex(ST st, int index) {
|
|
if (index >= st->N)
|
|
return NULL;
|
|
|
|
return st->table[index];
|
|
}
|
|
|
|
int STsearch(ST st, char * label) {
|
|
for (int i = 0; i < st->N; i++) {
|
|
if (strcmp(label, st->table[i]) == 0)
|
|
return i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int STcount(ST st) {
|
|
return st->N;
|
|
} |