Sei sulla pagina 1di 19

ARRAY dinamici

Tarray array_create(int initial_length)


{
Tarray array;
array.item = (Tinfo*)malloc(sizeof (Tinfo)*initial_length);
assert((initial_length == 0) || (array.item != NULL));
array.length = initial_length;
array.size = initial_length;
return array;
}

void array_destroy(Tarray *array)


{
free(array -> item);
array -> item = NULL; //in maniera precauzionale
array -> length = 0;
array -> size = 0;
}

void array_resize(Tarray *array, int new_length)


{
int new_size;
if((new_length > array -> size) || (new_length) < array -> size/SHRINK)
{
new_size = new_length*GROW;
array -> item = (Tinfo*)realloc(array -> item, new_size*sizeof(Tinfo));
assert((new_size == 0) || (array -> item != NULL));
array -> size = new_size;
}
array -> length = new_size;
}

struct Sinfo{ char nome[M];


int age; };
typedef struct Sinfo Tinfo;

struct Sarray{ Tinfo *item;


int length;
int size; };
typedef struct Sarray Tarray;
STACK statico
Tstack stack_create()
{
Tstack stack; struct Sinfo{ char nome[CAPACITY];
stack.elem_number = 0; int age; };
return stack; typedef struct Sinfo Tinfo;
} struct Sstack{ Tinfo array[CAPACITY];
int elem_number; };
typedef struct Sstack Tstack;
void stack_destroy(Tstack *stack)
{ stack -> elem_number = 0; }

void stack_push(Tstack *stack, Tinfo x)


{
stack -> array[stack -> elem_number] = x;
stack -> elem_number++;
}

Tinfo stack_pop(Tstack *stack)


{
Tinfo x;
x = stack -> array[stack -> elem_number-1];
stack -> elem_number--;
return x;
}

Tinfo stack_top(Tstack *stack)


{
Tinfo x;
x = stack -> array[stack -> elem_number-1];
return x;
}

bool stack_is_empty(Tstack *stack)


{ return stack -> elem_number == 0; }

bool stack_is_full(Tstack *stack)


{ return stack -> elem_number == CAPACITY; }
STACK dinamico
Tstack stack_create()
{
Tstack stack; struct Sinfo{ char nome[CAPACITY];
stack.array = array_create(0); int age; };
return stack; typedef struct Sinfo Tinfo;
}
struct Sarray{ Tinfo *item;
int length;
int size; };
void stack_destroy(Tstack *stack)
typedef struct Sarray Tarray;
{ array_destroy(&stack -> array); }
struct Sstack{ Tarray array; };
typedef struct Sstack Tstack;
void stack_push(Tstack *stack, Tinfo x)
{
int n;
n = stack -> array.length;
array_resize(&stack -> array, n+1);
stack -> array.item[n] = x;
}

Tinfo stack_pop(Tstack *stack)


{
Tinfo x;
int n;
n = stack -> array.length;
x = stack -> array.item[n-1];
array_resize(&stack -> array, n-1);
return x;
}

Tinfo stack_top(Tstack *stack)


{
Tinfo x;
int n;
n = stack -> array.length;
x = stack -> array.item[n-1];
return x;
}

bool stack_is_empty(Tstack *stack)


{ return stack -> array.length == 0; }
QUEUE statica
Tqueue queue_create(int capacity)
{ Tqueue queue;
queue.dim = 0; struct Sinfo{ char nome [M];
queue.front = 0; int age; };
queue.back = 0; typedef struct Sinfo Tinfo;
struct Squeue{ Tinfo *array;
queue.capacity = capacity; //dall'interfaccia
queue.array = (Tinfo*)malloc(sizeof(Tinfo)*capacity); int front;
assert(queue.array != NULL); int back;
return queue; } int dim;
int capacity; };
typedef struct Squeue Tqueue;

void queue_destroy(Tqueue *queue)


{ queue -> dim = 0;
queue -> capacity = 0;
free(queue -> array); //libera la memoria e forza a puntare a NULL - non sempre avviene l'ultima istruzione pertanto si fa:
queue -> array = NULL; //in modo che punti a NULL e non che punti a qualcosa che non esiste. }

void queue_push(Tqueue *queue, Tinfo x)


{ queue -> array[queue -> back] = x;
queue -> back = (queue -> back+1) % (queue -> capacity);
queue -> dim++; }

Tinfo queue_remove(Tqueue *queue)


{ Tinfo x = queue -> array[queue -> front];
queue -> front = (queue -> front+1)%(queue -> capacity);
queue -> front--;
return x; }

Tinfo queue_front(Tqueue *queue)


{ Tinfo x = queue -> array[queue -> front];
return x; }

bool queue_is_empty(Tqueue *queue)


{ return queue -> dim = 0; }

bool queue_is_full(Tqueue *queue)


{ return queue -> dim = queue -> capacity; }
QUEUE dinamica
Tqueue queue_create(int initial_length)
{ Tqueue queue;
struct Sinfo{ char nome[M];
queue.dim = 0;
int age; };
queue.front = 0; typedef struct Sinfo Tinfo;
queue.back = 0; struct Sarray{ Tinfo *item;
queue.array = array_create(initial_length); int length; //dim. logica
return queue; } int size; }; //dim. Fisica
typedef struct Sarray Tarray;
struct Squeue{ Tarray array;
int front; //primo elem.
void queue_destroy(Tqueue *queue) int back; //ultimo elem.
{ queue -> dim = 0; int dim; }; //elementi
array_destroy(&queue -> array); } effettivamente presenti nella coda
typedef struct Squeue Tqueue;

void queue_add(Tqueue *queue, Tinfo x)


{ if(queue -> dim == queue -> array.length)
{ int i, j, old_length = queue -> array.length;
array_resize(&queue -> array, old_length*2+1);
if(queue -> dim > 0 && queue -> front > queue -> back)
{ j = queue -> array.length-1;
for(i = old_length-1; i >= queue -> front; i--)
queue -> array.item[j--] = queue -> array.item[i];
queue -> front = j+1; }
}
queue -> array.item[queue -> back] = x;
queue -> back = (queue -> back+1) % queue -> array.length;
queue -> dim++;
}

Tinfo queue_remove(Tqueue *queue)


{ Tinfo x = queue -> array.item[queue -> front];
queue -> front = (queue -> front+1) % queue -> array.length;
queue -> dim--;
return x; }

Tinfo queue_front(Tqueue *queue)


{ return queue -> array.item[queue -> front]; }

bool queue_is_empty(Tqueue *queue)


{ return queue -> dim == 0; }
LISTE
iterative
Tlist list_create()
{ return NULL; }
struct Sinfo{ char nome[M];
int age; };
Tlist list_destroy(Tlist list) typedef struct Sinfo Tinfo;
{ struct Snode{ Tinfo info;
struct Snode *link; };
Tnode *curr, *succ;
typedef struct Snode Tnode;
curr = list; typedef Snode* Tlist;
while(curr != NULL) {
succ = curr -> link;
node_destroy(curr);
curr = succ; }
return NULL;
}

void list_print(Tlist list)


{
Tnode *curr;
curr = list;
while(curr != NULL) {
print_info(curr -> info);
curr = curr -> link; }
}

Tnode* node_create(Tinfo x)
{
Tnode *nuovo;
nuovo = (Tnode*)malloc(sizeof(Tnode));
if(nuovo == NULL)
return NULL;
nuovo -> info = x;
nuovo -> link = NULL;
return nuovo;
}

void node_destroy(Tnode *node)


{ free(node); }
Tlist list_insert(Tlist list, Tinfo x)
{
Tnode *prec, *curr, *nuovo;
prec = NULL;
curr = list;
while((curr != NULL) && (greater(x, curr -> info))) {
prec = curr;
curr = curr -> link; }
nuovo = node_create(x);
assert(nuovo != NULL);
if(prec == NULL)
{
nuovo -> link = list;
return nuovo;
} else {
nuovo -> link = curr;
prec -> link = nuovo;
return list;
}
}

Tlist list_delete(Tlist list, Tinfo x)


{
Tnode *prec, *curr, *alias;
prec = NULL;
curr = list;
while((curr != NULL) && (greater(x, curr -> info))) {
prec = curr;
curr = curr -> link; }
if((curr != NULL) && (equal(x, curr -> info)))
{
if(prec == NULL)
list = curr -> link;
else {
alias = curr -> link;
prec -> link = alias;
}
node_destroy(curr);
}
return list;
}
Tnode* list_search(Tlist list, Tinfo x)
{
Tnode *curr;
curr = list;
while((curr != NULL) && (greater(x, curr -> info)))
curr = curr -> link;
if((curr != NULL) && (equal(x, curr -> info)))
return curr;
else
return NULL;
}

int list_count_nodes(Tlist list)


{
Tnode *curr;
int k = 0;
curr = list;
while(curr != NULL) {
curr = curr -> link;
k++; }
return k;
}

bool list_is_empty(Tlist list)


{ return list == NULL; }

recursive
void list_print_Recursive(Tlist list)
{
if(list != NULL) {
print_info(list -> info);
list_print_recursive(list -> link); }
}
Tlist list_insert_Recursive(Tlist list, Tinfo x)
{
if (list == NULL || greater(list -> info, x))
{
Tnode *nuovo;
nuovo = node_create(x);
assert(nuovo != NULL);
nuovo -> link = list;
return nuovo;
} else {
Tlist L2 = list_insert_Recursive(list -> link, x);
list -> link = L2;
return list;
}
}

Tnode *list_search_Recursive(Tlist list, Tinfo x)


{
if(list == NULL || greater(list -> info, x))
return NULL;
else {
if(equal(list -> info, x))
return list;
else
return list_search_Recursive(list -> link, x);
}

Tlist list_delete_Recursive(Tlist list, Tinfo x)


{
if (list == NULL || greater(list -> info, x))
return NULL;
else {
if(equal(list -> info, x)) { //cancellazione in testa
Tnode *alias = list -> link;
node_destroy(list);
return alias;
} else {
Tlist L2;
L2 = list_delete_Recursive(list -> link, x);
list -> link = L2;
return list;
}
ALBERI BINARI
ricorsivi
Ttree tree_create()
{ return NULL; }

Ttree tree_destroy(Ttree tree)


{ if(tree == NULL) //CASO BASE: albero vuoto o con un solo elemento
return NULL;
else if((tree -> left == NULL) && (tree -> right == NULL))
{ free(tree);
return NULL;
} else { struct Sinfo{ char nome[M];
tree -> left = tree_destroy(tree -> left); int age; };
typedef struct Sinfo Tinfo;
tree -> right = tree_destroy(tree -> right);
struct Snode{ Tinfo info;
node_destroy(tree); struct Snode *left;
return NULL; } struct Snode *right; };
} typedef struct Snode Tnode;
typedef Tnode* Ttree;

Tnode *node_create(Tinfo x)
{ Tnode *node;
node = (Tnode*)malloc(sizeof(Tnode));
assert(node != NULL);
node -> info = x;
node -> left = NULL;
node -> right = NULL;
return node; }

void node_destroy(Tnode *node)


{ free(node); }

void tree_visit(Ttree tree)


{ if(tree != NULL)
{ tree_visit(tree -> left);
print_info(tree -> info);
tree_visit(tree -> right); }
}
Ttree tree_insert(Ttree tree, Tinfo x)
{ if(tree == NULL) //CASO BANALE: albero vuoto o finito (foglie)
{ Tnode *nuovo;
nuovo = node_create(x);
assert(nuovo != NULL);
return nuovo; }
else if(!greater(x, tree -> info)) //DIVIDE ET IMPERA: ricerca elemento
{
tree -> left = tree_insert(tree -> left, x);
return tree; }
else {
tree -> right = tree_insert(tree -> right, x);
return tree; }
}

Ttree tree_delete(Ttree tree, Tinfo x)


{ if(tree == NULL) //CASO BANALE: albero vuoto o finito (foglie)
return NULL;
else if(greater(tree -> info, x)) //DIVIDE ET IMPERA: ricerca elemento
{ tree -> left = tree_delete(tree -> left, x);
return tree;
} else if(greater(x, tree -> info)) {
tree -> right = tree_delete(tree -> right, x);
return tree;
} else { //FASE DI COMBINA: cancellazione e ricollegamento agli alberi principali
Ttree min_right;
if((tree -> right == NULL) && (tree -> left == NULL)) //cancellazione di una FOGLIA
{ node_destroy(tree);
return NULL; }
else if(tree -> right == NULL) //cancellazione di un nodo con figlio SINISTRO
{ Ttree alias;
alias = tree -> left;
node_destroy(tree);
return alias;
} else if(tree -> left == NULL) { //cancellazione di un nodo con figlio DESTR0
Ttree alias;
alias = tree -> right;
node_destroy(tree);
return alias; }
//cancellazione di un nodo con ENTRAMBI I FIGLI
min_right = tree_min(tree -> right); //ricerca del minimo nel sotto albero destro
tree -> info = min_right -> info; //copia del minimo nel campo info del nodo da cancellare
tree -> right = tree_delete(tree -> right, min_right -> info); //eliminazione dato da cui è stato copiato il minimo
return tree; } (fine “else”)
}

Tnode* node_search(Ttree tree, Tinfo x)


{ if((tree == NULL) || (equal(x, tree -> info)))
return tree;
else if(greater(x, tree -> info))
return node_search(tree -> right, x);
else
return node_search(tree -> left, x); }

Tnode* tree_min(Ttree tree)


{ Ttree min;
if(tree == NULL)
return tree;
else if(tree -> left)
return tree;
else {
min = tree_min(tree -> left);
return min; }
}

Tnode* tree_max(Ttree tree)


{ Ttree max;
if(tree == NULL)
return tree;
else if(tree -> right)
return tree;
else {
max = tree_max(tree -> right);
return max; }
}

bool tree_is_empty(Ttree tree)


{ return tree == NULL; }

int conta_nodi(Ttree tree)


{ int counter_l, counter_r;
if(tree == NULL)
return 0;
counter_l = conta_nodi(tree->left);
counter_r = conta_nodi(tree->right);
return 1 + counter_l + counter_r; }

int conta_foglie(Ttree tree)


{ int counter_l, counter_r;
if(tree == NULL)
return 0;
else if((tree->left == NULL) && (tree->right == NULL))
return 1;
else {
counter_l = conta_foglie(tree->left);
counter_r = conta_foglie(tree->right);
return counter_l + counter_r; }
}

int tree_high(Ttree tree)


{ assert(tree != NULL);
if((tree->left == NULL) && (tree->right == NULL))
return 0;
else if(tree->left == NULL)
return 1 + tree_high(tree->right);
else if(tree->right == NULL)
return 1 + tree_high(tree->left);
else {
int high_l = tree_high(tree->left);
int high_r = tree_high(tree->right);
if(high_l > high_r)
return 1 + high_l;
else
return 1 + high_r; }
}
TABELLE HASH
indirizzamento chiuso
THashTable *hashtable_create (int buckets)
{ int i;
THashTable *ht = (THashTable *)malloc(sizeof(THashTable));
assert(ht != NULL);
assert(buckets > 0);
ht -> bucket_number = buckets;
ht -> bucket = (TList *)malloc(sizeof(TList)*buckets);
assert(ht -> bucket != NULL); typedef int TKey;
for(i = 0; i < buckets; i++) typedef int TValue;
ht -> bucket[i] = list_create();
return ht; } struct SInfo{ TKey key;
TValue value; };
typedef struct SInfo TInfo;
void hashtable_destroy(THashTable *ht)
struct SHashTable{ int bucket_number;
{ int i;
TList *bucket; };
for(i = 0; i < ht -> bucket_number; i++) typedef struct SHashTable THashTable;
ht -> bucket[i] = list_destroy(ht -> bucket[i]);
free(ht -> bucket);
ht -> bucket_number = 0;
free(ht); }

TValue *hashtable_search(THashTable *ht, TKey key)


{ unsigned h = hash(key) % ht -> bucket_number;
TInfo info;
info.key = key;
TNode *node = list_search_unordered(ht -> bucket[h], info);
if (node == NULL)
return NULL;
else
return &node -> info.value; }

void hashtable_insert(THashTable *ht, TKey key, TValue value)


{ TInfo info;
info.key = key;
info.value = value;
unsigned h = hash(key) % ht -> bucket_number;
TNode *node = list_search_unordered(ht -> bucket[h], info);
if (node == NULL)
ht -> bucket[h] = list_insert_at_index(ht -> bucket[h], 0, info);
else
node -> info = value; }

void hashtable_delete(THashTable *ht, TKey key)


{ TInfo info;
info.key = key;
unsigned h = hash(key) % ht -> bucket_number;
ht -> bucket[h] = list_delete(ht -> bucket[h]); }

void hashtable_print(THashTable *ht)


{ int i;
TNode *node;
for(i = 0; i < ht -> bucket_number; i++)
list_print(ht -> bucket[i]); }

indirizzamento aperto
THashTable *hashtable_create (int buckets)
{ int i;
THashTable *ht = (THashTable *)malloc(sizeof(THashTable));
assert(ht != NULL);
assert(buckets > 0);
ht -> bucket_number = buckets;
ht -> bucket = (TBucket *)malloc(sizeof(Tbucket)*buckets);
assert(ht -> bucket != NULL);
for(i = 0; i < buckets; i++) typedef int TKey;
ht -> bucket[i].used = false; typedef int TValue;
ht -> used_buckets = 0;
return ht; } struct SInfo{ TKey key;
TValue value; };
typedef struct SInfo TInfo;

void hashtable_destroy(THashTable *ht) struct SBucket{ TInfo info;


{ free(ht -> bucket); bool used; };
ht -> bucket_number = 0; typedef struct SBucket TBucket;
ht -> use_buckets = 0;
free(ht); } struct SHashTable{ int bucket_number;
TBucket *bucket;
int used_buckets; };
TValue *hashtable_search(THashTable *ht, TKey key)
typedef struct SHashTable THashTable;
{ unsigned h = hash(key) % ht -> bucket_number;
TInfo info;
info.key = key;
while(ht -> bucket[h].used && !equal(info, ht -> bucket[h].info))
h = (h + 1) % ht -> bucket_number;
if (ht -> bucket[h].used)
return &ht -> bucket[h].info.value;
else
return NULL; }

void hashtable_insert(THashTable *ht, TKey key, TValue value)


{ TValue p;
p = hashtable_search(ht, key);
if(p != NULL)
{ *p = value;
return ; }
info.value = value;
if(ht -> used_buckets +1 >= MAX_LOAD*ht -> bucket_number)
{ int new_buckets = GROW_FACTOR*ht -> bucket_number;
if(new_buckets <= ht -> bucket_number)
new_buckets = ht -> bucket_number + 1;
hashtable_resize(ht, new_buckets); }
unsigned h = hash(key) % ht -> bucket_number;
while(ht -> bucket[h].used && !equal(info, ht -> bucket[h].info))
h = (h + 1) % ht -> bucket_number;
ht -> bucket[h].info.key = key;
ht -> bucket[h].info.value = value;
ht -> bucket[h]. used = true;
ht -> used_buckets++; }

void hashtable_resize(THashTable *ht, int new_buckets)


{ int i;
assert(new_buckets > ht -> bucket_number);
TBucket *p = (TBucket *)malloc(sizeof(TBucket)*new_buckets);
TBucket *old = ht -> bucket;
int old_buckets = ht -> bucket_number;
assert(p != NULL);
ht -> bucket = p;
ht -> bucket_number = new_buckets;
ht -> use_buckets = 0;
for(i = 0; i < new_buckets; i++)
p[i].used = false;
for(i = 0; i < old_buckets; i++)
hashtable_insert(ht, old[i].info.key, old[i].info.value);
free(old);}

void hashtable_print(THashTable *ht)


{ int i;
for(i = 0; i < ht -> bucket_number; i++)
if (ht -> bucket[h].used)
print_info(ht -> bucket[i].info); }

void hashtable_delete(THashTable *ht, TKey key, TValue value)


{ Tinfo info;
info.key = key;
unsigned h = hash(key) % ht -> bucket_number;
while(ht -> bucket[h].used && !equal(info, ht -> bucket[h].info))
h = (h + 1) % ht -> bucket_number;
if (ht -> bucket[h].used)
{ unsigned hole = h;
ht -> bucket[hole]. used = false;
ht -> used_buckets--;
h = (h + 1) % ht -> bucket_number; }
while(ht -> bucket[h].used)
{ unsigned h1 = hash(ht -> bucket[h].info.key) % ht -> bucket_number;
bool movable;
if(h > hole)
movable = h1 <= hole || h1 > h;
else
movable = h1 <= hole && h1 > h;
if(movable)
{ ht -> bucket[hole] = ht -> bucket[h];
hole = h;
ht -> bucket[hole].used = false; }
h = (h + 1) % ht -> bucket_number; } } }

BUBBLESORT
void bubblesort(int vett[], int dim)
{ int flag = 1; //variabile che indica se è stato effettuato un cambio di valori, è inzializzata a 1 per permettere di entrare nel ciclo
int i, k, temp;
for(i = 0; i < dim && flag == 1; i++)
{ flag = 0; //è 0 se non vengono effettuati scambi. L'algoritmo quindi termina.
for(k = 0; k < dim-i-1; k++) //per la parte dl vettore non ancora ordinata.
if(vett[k] > vett[k+1]) //se l'elemento precedente è maggiore viene scambiato.
{ temp = vett[k];
vett[k] = vett[k+1];
vett[k+1] = temp;
flag = 1; //effettuato lo scambio, il flag vale 1, e il ciclo si ripete }
} }

MERGESORT
void mergesort(int vett[], int dim, int temp[]) //l'ordinamento non avviene sul posto, ma su un vettore esterno che viene passato
{ int i, pivot = dim/2; //variabile che indica la divisione del vettore in 2 parti: 1. da 0 a PIVOT, 2. da PIVOT+1 a DIM
if(dim < 2)
return;
mergesort(vett, pivot, temp); //RICORSIONE della funzione MERGESORT sulla prima parte del vettore
mergesort(vett+pivot, dim-pivot, temp); //RICORSIONE della funzione MERGESORT sulla seconda parte del vettore
. merge(vett, dim, vett+pivot, dim-pivot, temp); //FUSIONE della 2 parti: confrontando i valori si effettua l'ordinamento
for(i = 0; i < dim; i++) //si ricopia TEMP nel vettore originario, in modo da lavorare sulla stessa area di memoria
vett[i] = temp[i]; }

void merge(int vett1[], int dim1, int vett2[], int dim2, int temp[])
{ int pos1 = 0, pos2 = 0, k = 0;
while(pos1 < dim1 && pos2 < dim2)
{ if(vett2[pos2] < vett1[pos1]) //se l'elemento del secondo vettore è minore viene aggiunto al vettore TEMP e si incrementano K e POS2
temp[k++] = vett2[pos2++];
else //se l'elemento del primo vettore è minore viene aggiunto al vettore TEMP e si incrementano K e POS1
temp[k++] = vett1[pos1++]; }
while(pos1 < dim1) //se il secondo vettore è finito e il primo no, gli elementi del primo vettore vengono messi in coda al vettore TEMP.
temp[k++] = vett1[pos1++];
while(pos2 < dim2) //se il primo vettore è finito e il secondo no, gli elementi del secondo vettore vengono messi in coda al vettore TEMP.
temp[k++] = vett2[pos2++]; }

QUICKSORT
void quicksort(int vett[], int dim)
{ int k;
if(dim < 2) //se la dimensione della riga è minore di 2
return;
k = partition(vett, dim);
quicksort(vett, k);
quicksort(vett+k+1, dim-k-1); }

int partition(int vett[], int dim)


{ int temp, i, j = 1;
for(i = 0; i < dim; i++) //abbiamo scelto l'ultimo elemento come pivot (TEMP)
if(vett[i] < vett[0])
{ temp = vett[k];
vett[k] = vett[i];
vett[i] = temp;
k++; }
temp = vett[k-1];
vett[k-1] = vett[0];
vett[0] = temp;
return k-1; }

Potrebbero piacerti anche