61 lines
967 B
C
61 lines
967 B
C
// Laboratorio 8 - Esercizio 2 - ST.c
|
|
// Matteo Schiff - s295565
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "ST.h"
|
|
#include "Item.h"
|
|
|
|
struct st {
|
|
Item *v;
|
|
int N;
|
|
int maxN;
|
|
};
|
|
|
|
ST STinit(int N) {
|
|
ST st = (ST) malloc(sizeof(*st));
|
|
|
|
st->v = (Item *) malloc(N * sizeof(Item));
|
|
st->N = 0;
|
|
st->maxN = N;
|
|
|
|
return st;
|
|
}
|
|
|
|
void STfree(ST st) {
|
|
free(st->v);
|
|
free(st);
|
|
}
|
|
|
|
int STinsert(ST st, Item item) {
|
|
if (st->N == st->maxN) {
|
|
printf("Errore, spazio esaurito nella ST\n");
|
|
return -1;
|
|
}
|
|
|
|
st->v[st->N] = item;
|
|
st->N++;
|
|
return st->N -1;
|
|
}
|
|
|
|
Item STsearchByIndex(ST st, int index) {
|
|
if (index >= st->N)
|
|
return ITEMnull();
|
|
|
|
return st->v[index];
|
|
}
|
|
|
|
int STsearch(ST st, Item item) {
|
|
int i;
|
|
for (i = 0; i < st->N; i++) {
|
|
if (strcmp(item.name, st->v[i].name) == 0)
|
|
return i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int STcount(ST st) {
|
|
return st->N;
|
|
} |