feat: Initial commit
This commit is contained in:
61
Laboratorio 9/Esercizio 1/ST.c
Normal file
61
Laboratorio 9/Esercizio 1/ST.c
Normal file
@@ -0,0 +1,61 @@
|
||||
// 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;
|
||||
}
|
||||
Reference in New Issue
Block a user