void show(Item a[], int l, int r)
{
  int i;

  for (i = l; i <= r; i++)
    ITEMshow(a[i]);
  printf("\n");
}
Ejemplo n.º 2
0
void print_rangeR(link h, Key lo, Key hi) {
    if (h == z) return;
    if (less(lo, key(h->item)))
        print_rangeR(hl, lo, hi);
    if ((less(lo, key(h->item)) || eq(lo, key(h->item)))
        && (less(key(h->item), hi) || eq(key(h->item), hi)))
        ITEMshow(*(h->item));
    if (less(key(h->item), hi))
        print_rangeR(hr, lo, hi);
}
int main(int argc, char *argv[]) 
{
  int N, i;
  Item item, itm;
  Key searched_key = malloc(2 * sizeof(char)),
      key = malloc(2 * sizeof(char));
  char init = 'a';

  srand(time(NULL));
  if (argc != 2)
    printf("Insufficient args. Enter N > 4!");
  else
    {
      N = atoi(argv[1]);
      if (N > 30)
	{
	  printf("Enter 4 < value < 30\n");
	  return 0;
	}
      STinit(N);
      for (i = 0; i < N; i++)
	{
	  key[0] = init + i;
	  key[1] = '\0';
	  item = ITEMrand(key);
	  ITEMshow(item);
	  if (i == S)
	    strcpy(searched_key, key);
	  STinsert(item);
	}
      printf("\n");
      printf("Searching for element with key \'%s\'! ", searched_key);
      itm = STsearch(searched_key);
      ITEMshow(itm);
      printf("Deleting item!\n");
      STdelete(itm);
      printf("Searching for element with key \'%s\'! ", searched_key);
      ITEMshow(STsearch(searched_key));
    }
  return 0;
}
Ejemplo n.º 4
0
int main (int argc, char * argv[]) {
    GRAPH graph;
    FILE * finput;
    symboltable table;
    EDGE tmpedge;
    ITEM * items;

    finput=fopen(argv[1], "r");
    int V;
    fscanf(finput, "%d", &V);

    graph=GRAPHinit(V);
    table=HASHinit(V);

    items=malloc(V*sizeof(ITEM));
    int i;
    for(i=0; i<V; i++) items[i]=NULL;

    i=0;
    char v1[10+1];
    char v2[10+1];
    while(fscanf(finput, "%s %s", v1, v2)==2) {
        if(HASHreturn(table, v1)==-1) {
            items[i]=ITEMinit(v1);
            HASHinsert(table, items[i], i);
            i++;
        }

        if(HASHreturn(table, v2)==-1) {
            items[i]=ITEMinit(v2);
            HASHinsert(table, items[i], i);
            i++;
        }
        tmpedge=EDGEadd(HASHreturn(table, v1), HASHreturn(table, v2));
        GRAPHinsertE(graph, tmpedge);
    }

    int * solution;
    int cmd, exit=0, path, v, w, j, k;
    while(!exit) {
        i=1;
        printf("\n");
        printf("%d Caclolare il camino semplice di lunghezza minima tra due vertici\n", i++);
        printf("%d Caclolare il camino semplice di lunghezza massima tra due vertici\n", i++);
        printf("%d Caclolare il numero di camini semplici tra due vertici\n", i++);
        printf("%d Calcolare le componenti fortemente connesse al grafo\n", i++);
        printf("%d Individuare un possibile sottoinsieme di archi per ottenere un grafo fortemente connesso\n", i++);
        printf("%d Esci\n--> ", i++);
        scanf("%d", &cmd);
        switch(cmd) {
        case 1:
            printf("Inserire il nome dei due nodi separati da spazio: ");
            scanf("%s %s", v1, v2);
            path=0;
            v=HASHreturn(table, v1);
            w=HASHreturn(table, v2);
            solution = optimalmin(graph, v, w, V);
            printf("Nodi attraversati:\n");
            ITEMshow(items[v], stdout);
            while(v!=w) {
                printf(" -> ");
                v=solution[v];
                ITEMshow(items[v], stdout);
                path++;
            }
            printf("\nLa lunghezza è %d\n", path);
            break;
        case 2:
            printf("Inserire il nome dei due nodi separati da spazio: ");
            scanf("%s %s", v1, v2);
            path=0;
            v=HASHreturn(table, v1);
            w=HASHreturn(table, v2);
            solution = optimalmax(graph, v, w, V);
            printf("Nodi attraversati:\n");
            ITEMshow(items[v], stdout);
            while(v!=w) {
                printf(" -> ");
                v=solution[v];
                ITEMshow(items[v], stdout);
                path++;
            }
            printf("\nLa lunghezza è %d\n", path);
            break;
        case 3:
            printf("Inserire il nome dei due nodi separati da spazio: ");
            scanf("%s %s", v1, v2);
            printf("Il numero dei cammini e' %d\n", npath(graph, HASHreturn(table, v1), HASHreturn(table, v2), V));
            break;
        case 4:
            k=0;
            for(i=0; i<V; i++)
                for(j=i+1; j<V; j++) {
                    if(GRAPHpath(graph, i, j) && GRAPHpath(graph, j, i)) {
                        k++;
                        ITEMshow(items[i], stdout);
                        printf(" e' fortemente connesso con ");
                        ITEMshow(items[j], stdout);
                        printf("\n");
                    }
                }
            printf("Ci sono %d componenti fortemente connesse\n", k);
            break;
        case 5:
            k=0;
            STACK additionaledges=STACKinit();
            for(i=0; i<V; i++) for(j=i+1; j<V; j++) {
                    int ab=GRAPHpath(graph, i, j);
                    int ba=GRAPHpath(graph, j, i);
                    if(!(ab && ba)) {
                        if(!ab) {
                            tmpedge = EDGEadd(i, j);
                            STACKpush(additionaledges, tmpedge);
                            GRAPHinsertE(graph, tmpedge);
                        }
                        if(!ba) {
                            tmpedge = EDGEadd(j, i);
                            STACKpush(additionaledges, tmpedge);
                            GRAPHinsertE(graph, tmpedge);
                        }
                    }
                }
            while(STACKcardinality(additionaledges)) {
                tmpedge=STACKpop(additionaledges);
                ITEMshow(items[tmpedge.v], stdout);
                printf(" -> ");
                ITEMshow(items[tmpedge.w], stdout);
                printf("\n");
            }
            break;
        case 6:
            exit=1;
        }
    }
    return 0;
}