Вы находитесь на странице: 1из 15

/* ALGORITMI DI ORDINAMENTO */

void bubble_sort(BASE v[], int n); void isort (BASE v[], int n); void ssort (BASE v[], int n); void heapSort (BASE v[], int array_size); void siftDown (BASE v[], int root, int bottom); void mergesort (BASE v[], int l, int r); void merge (BASE v[], int l, int m, int r); void qsort (BASE v[], int n); void quick (BASE v[], int n); void quick (BASE v[], int lb, int ub); int partition (BASE v[], int lb, int ub);

void bubble_sort (BASE v[], int n){ int i, j, flag; for(i = n -1; i > 0; i--){ flag = 0; for(j = 0; j < i; j++) if(basecmp(v[j], v[j+1]) == 1){ flag = 1; swap(v, j, j+1);

}

}

}

if (flag == 0) break;

void isort (BASE v[], int n){ int i, j; BASE key; for(i = 1; i < n; i++){ key = v[i]; for(j = i; j > 0; j--){ if(basecmp(v[j-1], key) == 1) v[j] = v[j-1];

}

}

else

}

v[j] = key;

break;

void ssort (BASE v[], int n){ int i, j, k; for(i = 0; i < n -1; i++){ k = i; for(j = i+1; j < n; j++) if(basecmp(v[j], v[k]) == -1) k = j;

}

}

swap(v, i, k);

void heapSort(BASE v[], int array_size){ int i; for(i = (array_size/2) - 1; i >= 0; i--) siftDown (v, i, array_size-1); for(i = array_size - 1; i >= 1; i--){ swap(v, 0, i); siftDown(v, 0, i-1);

}

}

void siftDown (BASE v[], int root, int bottom){ int done, maxChild; done = 0; while ((root * 2 < = bottom) && (!done)) { if(root * 2 == bottom) maxChild = root * 2; else if (basecmp(v[root * 2], v[root * 2 + 1]) == 1) maxChild = root * 2;

else

maxChild = root * 2 + 1; if(basecmp(v[root], v[maxChild]) == -1){ swap (v, root, maxChild); root = maxChild;

}

else

}

}

done = 1;

void mergesort (BASE v[], int l, int r){ int m; if(l < r){

m = (l + r)/ 2; mergesort(v, l, m); mergesort(v, m+1, r); merge(v, l, m, r);

}

}

void merge (BASE v[], int l, int m, int r){ BASE * u; int i = l, j = m+1, k = 0; u = (BASE *) malloc ((r-l+1)*sizeof(BASE)); while (i <= m && j <= r) { if(basecmp(v[i], v[j]) == -1){ u[k] = v[i]; k++; i++;

}

else{

u[k] = v[j]; k++; j++;

}

}

while (i <= m) u[k++] = v[i++]; while (j <= r) u[k++] = v[j++]; for(k = l; k <= r; k++) v[k] = u[k-l];

free(u);

}

void qsort (BASE v[], int n){ quick(v, 0, n-1);

}

void quick (BASE v[], int lb, int ub){ int pivot_index; if(lb >= ub) return; pivot_index = partition (v, lb, ub); quick(v, lb, pivot_index-1); quick(v, pivot_index + 1, ub);

}

int partition (BASE v[], int lb, int ub){ int up, down; BASE pivot; pivot = v[lb]; down = lb; up = ub; while (down < up) { while(basecmp(v[down], pivot) < = 0 && down < ub) down++; while(basecmp(v[up], pivot) == 1 && down < ub) up--; if(down < up) swap (v, down, up);

}

swap(v, lb, up); return up;

}

//LISTE

typedef int ITEM;

typedef struct s1 { ITEM data; struct s1 * next; struct s1 * prev; }ListNode;

void insert (ListNode ** sptr, ITEM value); ITEM delete (ListNode ** sptr, ITEM value); void printList (ListNode * currentPtr);

void insert (ListNode ** sptr, ITEM value){

ListNode * newPtr, * previousPtr, * currentPtr;

newPtr = malloc(sizeof(ListNode)); if(newPtr != NULL){ newPtr->data = value; newPtr->next = NULL;

previousPtr = NULL; currentPtr = *sptr;

while(currentPtr != NULL && (basecmp(value, currentPtr->data)) > 0){ previousPtr = currentPtr; currentPtr = currentPtr->next;

}

if(previousPtr == NULL){ newPtr->next = *sptr; *sptr = newPtr;

}

else{

previousPtr->next = newPtr; newPtr->next = currentPtr;

}

else

}

printf("no memory available");

}

ITEM delete (ListNode ** sptr, ITEM value){

ListNode * tempPtr, * previousPtr, * currentPtr;

if(basecmp(value, (*sptr)->data)) == 0){ tempPtr = *sptr; *sptr = (*sptr)->next; free(tempPtr); return value;

}

else{

previousPtr = *sptr; currentPtr = (*sptr)->next; while(currentPtr != NULL && basecmp(value, currentPtr->data)) != 0){ previousPtr = currentPtr; currentPtr = currentPtr->next;

}

if(currentPtr != NULL){ tempPtr = currentPtr; previousPtr->next = currentPtr->next; free(tempPtr); return value;

}

}

return '\0'

}

void printList(ListNode * currentPtr){

if(currentPtr == NULL) printf("the list is empty\n");

else{

 

printf("the list is:\n"); while(currentPtr != NULL){ printItem(currentPtr->data); currentPtr = currentPtr->next;

}

}

}

/* PILE - LIFO */

struct stacknode{ int data; struct stacknode * nextPtr;

};

typedef struct stacknode StackNode; typedef struct stacknode * StackNodePtr;

void push (StackNodePtr * topPtr, int info); int pop(StackNodePtr * topPtr); void printStack(StackNodePtr currentPtr);

void push (StackNodePtr * topPtr, int info){ StackNodePtr newPtr; newPtr = malloc(sizeof(StackNode)); /* inserisci il nodo in cima alla pila */ if(newPtr != NULL){ newPtr->data = info; newPtr->nextPtr = *topPtr; *topPtr = newPtr;

}

else

printf("%d non inserito. no memory available", info);

}

int pop(StackNodePtr * topPtr){ StackNodePtr tempPtr; int popValue; tempPtr = *topPtr; popValue = (*topPtr)->data; *topPtr = (topPtr)->nexPtr; free(tempPtr); return popValue;

}

void printStack(StackNodePtr currentPtr){ if(currentPtr == NULL) printf("the stack is empty");

else{

printf("the stack is:\n"); /* finchè non raggiungo la fine della pila */ while(currentPtr != NULL){ printf("%d --> "currentPtr->data); currentPtr = currentPtr->nextPtr;

}

printf("NULL\n\n");

}

}

/* CODE - FIFO */

struct queueNode { char data; struct queueNode *nextPtr;

};

typedef struct queueNode QueueNode; typedef QueueNode * QueueNodePtr;

void printQueue (QueueNodePtr currentPtr); char dequeue (QueueNodePtr * headPtr, QueueNodePtr * tailPtr); void enqueue (QueueNodePtr * headPtr, QueueNodePtr * tailPtr, char value); int isEmpty(QueueNodePtr headPtr);

void enqueue (QueueNodePtr * headPtr, QueueNodePtr * tailPtr, char value){ QueueNodePtr newPtr; newPtr = malloc(sizeof(QueueNode)); if(newPtr != NULL){ newPtr->data = value; newPtr->nextPtr = NULL; /* se la coda è vuota, inserisce il nodo in testa */ if(isEmpty (*headPtr)){ *headPtr = newPtr;

}

else{

(*tailPtr)->nextPtr = newPtr;

}

*tailPtr = newPtr;

}

else

printf("%c non inserted. no memory available.\n", value);

}

char dequeue (QueueNodePtr * headPtr, QueueNodePtr *tailPtr){ char value; QueueNodePtr tempPtr; value = (*headPtr)->data; tempPtr = *headPtr; *headPtr = (* headPtr) -> nextPtr;

/* se la coda non è vuota */ if(* headPtr == NULL){ *tailPtr = NULL;

}

free(tempPtr);

return value;

}

int isEmpty(QueueNodePtr headPtr){ return (headPtr == NULL);

}

void printQueue (QueueNodePtr currentPtr){ /* se la coda non è vuota */ if(currentPtr == NULL) printf("Queue is empty");

else{

 

printf("the queue is:\n"); /* finchè non raggiungo la fine della coda */ while(currentPtr != NULL){ printf("%c --> ", currentPtr->data); currentPtr = currentPtr->nextPtr;

}

printf("NULL\n\n");

}

}

/* ALBERI */

typedef int TTREEITEM; /* spesso è una struct */ typedef struct s1 TTREEELEM; typedef struct s2 TTREE;

typedef struct s1 { TTREEITEM info; /* spesso puntatore a struct */ TTREEELEM * left; TTREEELEM * right;

};

typedef struct s2 { int n_elem; TTREEELEM * tree;

};

void TreeCreate (TTREE * t); TTREEELEM * Create (TTREEITEM i); TTREEELEM * LocateNode (TTREE * t, TTREEITEM i); int TreePutItem (TTREE * t, TTREEITEM i); int TreePutRecur (TTREE * t, TTREEITEM i); int putRecur (TTREEELEM * p, TTREEELEM i); int IsLeaf(TTREEELEM * p); int HasOneSubTree(TTREEELEM * p); TTREEELEM * LocateInOrderSucc(TTREEELEM * p, TTREEELEM ** pfather); TTREEELEM * LocateNode(TTREE * t, TTREEITEM i, TTREEELEM **pf); int TreeGetNode (TTREE * t, TTREEITEM i); TTREEELEM * LocNodeRec (TTREEITEM i, TTREEELEM * node); void printItem (TTREEELEM * p); void TreePrintInOrder (TTREE * t); void TreePrintPreOrder (TTREE * t);

void TreePrintPostOrder (TTREE * t); TTREEELEM * LocateFather (TTREE * t, TTREEELEM * p); TTREEELEM * TreeLocateMax (TTREEELEM * t); TTREEELEM * TreeLocateMin (TTREEELEM * t); TTREEELEM * TreeLocateSucc (TTREEITEM i, TTREE * t); TTREEELEM * TreeLocatePrec (TTREEITEM i, TTREE * t);

void TreeCreate (TTREE * t){ t->n_elem = 0; t->tree = t;

}

TTREEELEM * Create (TTREEITEM i){ TTREEELEM * p;

p = malloc(sizeof(TTREEELEM)); p->info = i; p->right = p->left = NULL;

return p;

}

TTREEELEM * LocateNode (TTREE * t, TTREEITEM i){ TTREEELEM *p, *q;

p = q = t->tree;

while (q != NULL && i != p->info) {

p =q;

if(basecmp(i, p->info) < 0)

}

else

}

return p;

q

q

= p->left;

= p->right;

int TreePutItem (TTREE * t, TTREEITEM i){ TTREEELEM * p, * z; if(t->n_elem == 0){

z = Create(i);

t->n_elem++;

t->tree = z; return 0;

}

p = LocateNode(t, i); if(p->info == i) return 1;

else{

z = Create(i);

t->n_elem++;

if(basecmp(p->info, i) > 0) p->left = z;

else

}

return 0;

}

p->right = z;

int TreePutRecur (TTREE * t, TTREEITEM * i){ int a; TTREEELEM * p, * z; if(t->n_elem == 0){ z = Create(i); t->n_elem++; t->tree = z; return 0;

}

z

= Create(i);

a = putRecur(t->tree, z); if(a == 1 || a == -1) free(z);

else

}

return a;

t->n_elem+;

int putRecur (TTREEELEM * p, TTREEELEM i){ int a; if(p == NULL) return -1;

a = basecmp(i->info, p->info); if(a < 0){

}

if(p->left == NULL){ p->left = i; return 0;

}

else

putRecur (p->left, i);

}

if(a == 0)

if(a > 0){

return 1;

if(p->right == NULL){ p->right = i; return 0;

}

else

putRecur(p->right, i);

}

TTREEELEM * LocateInOrderSucc(TTREEELEM * p, TTREEELEM ** pfather){ TTREEELEM * father, *

succ;

 

father = p; succ = p->right; while (succ->left != NULL) { father = succ; succ = succ->left;

}

*pfather = father; return succ;

}

TTREEELEM * LocateNode(TTREE * t, TTREEITEM i, TTREEELEM **pf){

TTREEELEM * p, * q, * father;

p = father = q = t->tree;

while (q != NULL && i != p->info) { father = p; p = q; if(basecmp(i, p->info) < 0)

}

else

q

q

= p->left;

= p->right;

if(p->info == i){ *pf = father; return p;

}

else {

}

}

*pf = NULL; return NULL;

int IsLeaf(TTREEELEM * p){ return (p->right == NULL && p->left == NULL);

}

int HasOneSubTree(TTREEELEM * p){ return ((p->right == NULL) ^ (p->left == NULL));

}

int TreeGetNode (TTREE * t, TTREEITEM i){

TTREEELEM * p, * pfather, * rp, * rpfather;

p = LocateNode(t, i, &pfather); if(p == NULL) return 0; if(IsLeaf(p)) rp = NULL;

else {

if(HasOneSubTree(p)){ if(p->left != NULL) rp = p->left;

else

rp = p->right;

}

else {

 

rp = LocateInOrderSucc(p, &rpfather) if(rpfather != p){ rpfather->left = rp->right; rp->right = p->right;

}

rp->left = p->left;

}

if(p == t->tree) t->tree = rp; else if(p == pfather->left) pfather->left = rp;

else

}

t->n_elem--;

free(p);

return1;

}

pfather->right = rp;

TTREEELEM * LocNodeRec (TTREEITEM i, TTREEELEM * node){ int a;

a = basecmp(i, node->info); if(a == 0)

return node; else if(a < 0) return LocNodeRec (i, node->left); else if(a > 0) return LocNodeRec (i, node->right);

}

void printItem (TTREEELEM * p){ /* se info è una struct la funzione

printf("%d", p->info);

}

void TreePrintInOrder (TTREE * t){ TTREEELEM * p;

p = t->tree;

if(p != NULL){ TreePrintInOrder(p->left); printItem(p); TreePrintInOrder(p->right);

}

}

è diversa, bisogna sapere qual è l'informazione da stampare */

void TreePrintPreOrder (TTREE * t){ TTREEELEM * p;

p = t->tree;

if(p != NULL){ printItem(p); TreePrintPreOrder(p->left); TreePrintPreOrder(p->right);

}

}

void TreePrintPostOrder (TTREE * t){ TTREEELEM * p;

p = t->tree;

if(p != NULL){ TreePrintPostOrder(p->left); TreePrintPostOrder(p->right); printItem(p);

}

}

TTREEELEM * LocateFather (TTREE * t, TTREEELEM * p){ TTREEELEM * q, * fp; int i; if(p == t->tree) return NULL;

q = t->tree; while ((i = basecmp(p->info, q->info)) != 0) { fp = q; if (i > 0)

}

}

return fp;

q = q->right;

if(i < 0)

q = q->left;

TTREEELEM * TreeLocateMax (TTREEELEM * t){ TTREEELEM * p;

p = t;

while (p->left != NULL)

}

return ;

p = p->left;

TTREEELEM * TreeLocateMin (TTREEELEM * t){ TTREEELEM * p;

p = t;

while (p->right != NULL)

}

p = p->right;

return p;

TTREEELEM * TreeLocateSucc (TTREEITEM i, TTREE * t){ TTREEELEM * p, * fp;

p = LocateNode(t, i, &fp);

if(p == NULL) return NULL; if(p == TreeLocateMax(t->tree)) return NULL; if(p->right != NULL) return (TreeLocateMin(p->right));

while (p != NULL && (p == fp->right)) {

p = fp;

fp = LocateFather(t, fp);

}

return fp; }

TTREEELEM * TreeLocatePrec (TTREEITEM i, TTREE * t){ TTREEELEM * p, * fp;

p = LocateNode(t, i, &fp);

if(p == NULL) return NULL; if(p == TreeLocateMin(t->tree)) return NULL; if(p->left != NULL) return (TreeLocateMax(p->left)); while (p != NULL && (p == fp->left)) { p = fp; fp = LocateFather(t, fp);

}

return fp;

}

/* Hash Table */

#define FREE_SLOT -1 #define DELETED_SLOT -2 #define OCCUPATED_SLOT 0

typedef int TKEY; typedef int THASHITEM; typedef struct s0 THASHELEM; typedef struct s1 THASH;

typedef struct s0 { TKEY key; THASHITEM info; }; //THASHELEM

typedef struct s1 { int size; THASHELEM * hash; };//THASH

int hash(THASH * h, TKEY k); int rehash(THASH * h, int j); void HashCreate (THASH * a, int size); void HashFree (THASH * a); int HashPut (THASH * h, THASHITEM i, TKEY k); THASHITEM HashGet (THASH * h, TKEY k, int *trovato); THASHITEM HashRead (THASH * h, TKEY k, int *trovato);

int hash(THASH * h, TKEY k){ return (k % h->size);

}

int rehash(THASH * h, int j){ return ((j+1) % h->size);

}

void HashCreate (THASH * a, int size){ THASHELEM * p; int i; a->size = size;

p = calloc(size, sizeof(THASHELEM)); a->hash = p; for(i = 0; i < size; i++) a->hash[i].key = FREE_SLOT;

}

void HashFree (THASH * a){ if(!a->size) return; free(a->hash); a->size = 0;

}

int HashPut (THASH * h, THASHITEM i, TKEY k){ int j, f;

f = j = hash(h, k);

while (h->hash[j].key != k && h->hash[j].key != FREE_SLOT && h->hash[j].key != DELETED_SLOT) {

j = rehash(h, j); if(j == f) return FALSE;

}

if(h->hash[j].key == k) return FALSE; h->hash[j].key = k; h->hash[j].info = i; return TRUE;

}

THASHITEM HashGet (THASH * h, TKEY k, int *trovato){ THASHITEM empty; int j, i;

i = j = hash(h, k);

while (h->hash[j].key != k || h->hash[j].key == DELETED_SLOT) {

j = rehash(h, j); if(i == j) break;

}

if(h->hash[j].key == k){ *trovato = TRUE; h->hash[j].key = DELETED_SLOT; return h->hash[j].info;

}

else {

}

}

*trovato = FALSE; return empty;

THASHITEM HashRead (THASH * h, TKEY k, int *trovato){ THASHITEM empty; int j, i;

i = j = hash(h, k);

while (h->hash[j].key != k || h->hash[j].key == DELETED_SLOT) {

j = rehash(h, j); if(i == j) break;

}

if(h->hash[j].key == k){ *trovato = TRUE; return h->hash[j].info;

}

else {

}

}

*trovato = FALSE; return empty;

#define FREE_SLOT NULL #define EMPTY_ITEM 0

typedef int TKEY; typedef int THASHITEM; typedef struct s0 THASHELEM; typedef struct s1 THASH;

typedef struct s0 { TKEY key; THASHITEM info; struct s0 * next;

};

typedef struct s1 { int size; THASHELEM ** hash;

};

void HashCreate(THASH * a, int size); void deleteList (THASHELEM * ptr); void HashFreeChaining(THASH * a); int hashChaining (THASH * h, TKEY k); THASHELEM * ItemCreate (TKEY k, THASHITEM i); int HashPut (THASH * h, THASHITEM i, TKEY k); THASHITEM HashRead (THASH * h, TKEY k, int * trovato); long int createKey(char parola[]);

void HashCreate(THASH * a, int size){ THASHELEM ** p; int i; a->size = size;

p = calloc (size, sizeof(THASHELEM*)); a->hash = p; for(i = 0; i < size; i++) a->hash[i] = FREE_SLOT;

}

void deleteList (THASHELEM * ptr){ if(ptr == NULL) return; deleteList(ptr->next); free(ptr);

}

void HashFreeChaining(THASH * a){ int i; for (i = 0; i < a->size; i++) deleteList(a->hash[i]);

}

int hashChaining (THASH * h, TKEY k){ return (k % h->size);

}

THASHELEM * ItemCreate (TKEY k, THASHITEM i){ THASHELEM * p;

p = malloc(sizeof(THASHELEM)); p->key = k; p->info = i; p->next = NULL; return p;

}

int HashPut (THASH * h, THASHITEM i, TKEY k){ int idx = hash (h, k); THASHELEM * tmp = h->hash[idx]; THASHELEM * chain = tmp; while (chain != NULL) { if(basecmp(chain->key) == 0) return FALSE; chain = chain->next;

}

h->hash[idx] = ItemCreate(k, i); h->hash[idx]->next = tmp; return TRUE;

}

THASHITEM HashRead (THASH * h, TKEY k, int * trovato){ int idx = hash(h, k); THASHELEM * tmp = h->hash[idx]; THASHITEM empty = EMPTY_ITEM; while (tmp != NULL) { if(basecmp(tmp->key, k) == 0){ *trovato = TRUE; return tmp->info;

}

}

tmp = tmp->next;

}

*trovato = FALSE; return empty;

THASHITEM HashGetChaining (THASH * h, TKEY k, int * trovato){ int idx = hash(h, k); THASHELEM * tmp, * prev, empty; THASHITEM info; tmp = h->hash[idx]; prev = tmp; empty = EMPTY_ITEM; while (tmp != NULL) { if(tmp->key == k){ info = tmp->info; *trovato = TRUE; if(tmp == h->hash[idx]) h->hash[idx] = tmp->next;

else

prev->next = tmp->next; free(tmp); return info;

}

prev = tmp; tmp = tmp->next;

}

* trovato = FALSE; return empty;

}

long int createKey(char parola[]){ int i; long int somma = 1; for(i = 0; i < strlen(parola); i++) somma += (parola[i] - 47)*(i+1); return(somma);

}