Ejemplo n.º 1
0
void interactive()
{
	HashTable* ht = makeTable(22);
	for (unsigned i = 0; i < 27; ++i) {
		ht = insertTable(ht, namen[i], rand() % 100);
	}
	printHashTable(ht);
	while (1) {
		unsigned i;
		printf( "Insert (2) or search (1) or delete (0)? " );
		scanf(  "%u", &i );
		if (i == 2) {
			printf( "Name: " );
			char* name = getline();
			unsigned telNr;
			printf( "Telephone-Nr.: " );
			scanf("%u", &telNr);
			ht = insertTable(ht, name, telNr);
			printHashTable(ht);
		} else if (i == 1) {
			printf( "Name: " );
			char* name = getline();
			unsigned* telNr = searchTable(ht, name);
			printf( "Telephone-Nr.: %u\n", *telNr );
		} else {
			printf( "Name: " );
			char* name = getline();
			deleteTable(ht, name);
			printHashTable(ht);
		}
	}
}
Ejemplo n.º 2
0
bool hasUniqueCharacters(char * str) {
    HashTable  * table = newHashMap(NULL, 0);
    char * ptr = &str[0];
    while (ptr != NULL && *ptr != '\0') {
        char temp[2] = {*ptr,'\0'};
        if(hashTableGet(table, temp) == HashTableNotFound){
            hashTableSet(table, temp, 1);
            printHashTable(table);
            ptr++;
        }else{
            printHashTable(table);
            printf("string does not have unique characters, found at least two %c\n", *ptr);
            ptr = NULL;
        }
    }
    
    return false;
}
Ejemplo n.º 3
0
int main() {
    int capacity = 11, i;
    HashTablePtr t = newHashTable(capacity, hashInt, compare);
    HashNodePtr node;
    putIntoHashTable(t, 1, 1);
    putIntoHashTable(t, 2, 2);
    putIntoHashTable(t, 3, 3);
    putIntoHashTable(t, 4, 4);
    putIntoHashTable(t, 5, 5);
    printHashTable(t);
    for(i = 0; i <= 5; i++) {
        HashNodePtr node = getFromHashTable(t, i);
        if(node) {
            printf("key %d values %d\n", node->key, node->value);
        } else {
            printf("cann't find key %d\n", i);
        }
    }
    for(i = 11; i <= 16; i++) {
        HashNodePtr node = getFromHashTable(t, i);
        if(node) {
            printf("key %d values %d\n", node->key, node->value);
        } else {
            printf("cann't find key %d\n", i);
        }
    }
    removeFromHashTable(t, 2);
    removeFromHashTable(t, 3);
    printHashTable(t);
    putIntoHashTable(t, 2, 2);
    putIntoHashTable(t, 13, 13);
    putIntoHashTable(t, 24, 24);
    printHashTable(t);
    removeFromHashTable(t, 2);
    printHashTable(t);
    node = getFromHashTable(t, 13);
    printf("key %d values %d\n", node->key, node->value);
    node = getFromHashTable(t, 24);
    printf("key %d values %d\n", node->key, node->value);
    putIntoHashTable(t, 35, 35);
    printHashTable(t);
    freeHashTable(t);
    return 0;
}
Ejemplo n.º 4
0
void typeCheckerAndPrint(tree tr)
{
    currMax = 1;
    scope = 1;
    dummy = 0;
    table = createHashTable(); 
    strcpy(mapV[1],"_main");  
    param = &dummy;
    Populate(tr,0); //Start from this function. 
    printHashTable(table);
}
Ejemplo n.º 5
0
int main(int argc, const char * argv[]) {
    HashTableNode nodes[2] = {{"Pantera",234}, {"Pantera2",223}};
    HashTable * hashTable = newHashMap(nodes, 2);
    hashTableSet(hashTable, "key2", 666);
    hashTableSet(hashTable, "key3", 666);
    printHashTable(hashTable);
    int key2 = hashTableGet(hashTable, "key2");
    printf("\nkey2 %i\n", key2);
    
    return 0;
}
Ejemplo n.º 6
0
int main(void)
{

	HashTable *T = createHashTable (10);
	Node* newNode1 = createNode (1);
	Node* newNode2 = createNode (2);
	Node* newNode3 = createNode (3);
	Node* newNode4 = createNode (22);
	Node* newNode5 = createNode (23);
	Node* newNode6 = createNode (21);
	Node* newNode7 = createNode (32);
	Node* newNode8 = createNode (999);



	put (T,newNode1);
	put (T,newNode2);
	put (T,newNode3);
	put (T,newNode4);
	put (T,newNode5);
	put (T,newNode6);
	put (T,newNode7);
	put (T,newNode8);


	//get (T, 999);
	get (T, 11);
	printHashTable (T);

	int el = 11;
	search (T, el);
	if (search (T, el)) printf("\nElemento %d encontrado na HT", el);
	else printf("\nElemento %d não ncontrado na HT", el);
	
	el = 3;
	if (search (T, el)) printf("\nElemento %d encontrado na HT", el);
	else printf("\nElemento %d não ncontrado na HT", el);



	destroyNode (newNode1);
	destroyNode (newNode2);
	destroyNode (newNode3);
	destroyNode (newNode4);
	destroyNode (newNode5);
	destroyNode (newNode6);
	destroyNode (newNode7);
	destroyNode (newNode8);

	return 0;
}
Ejemplo n.º 7
0
int main()
{
    printf("Type numbers of values to insert: ");
    int nr_of_values;
    scanf("%d", &nr_of_values);
    srand(time(NULL));
    createHashTable();
    int max = 0;

    printf("Would you like random string length ? (Y/N)\n");
    char key;
    do
    {
        key = getch();
    }
    while ((toupper(key) != 'Y') && (toupper(key) != 'N'));
    int length = -1;
    if (toupper(key) == 'N')
    {
        printf("Type the length of the strings: ");
        scanf("%d", &length);
    }

    for (int i=0; i<nr_of_values; i++)
    {
        char* string = randomString(length);
        int val = insertValue(string);
        if (max < val)
        {
            max = val;
        }
    }
    printf("The max number of collisions: %d\n", max);
    printf("Save the hash table file ? (Y/N)\n");
    do
    {
        key = getch();
    }
    while ((toupper(key) != 'Y') && (toupper(key) != 'N'));
    if (toupper(key) == 'Y')
    {
        char* buffer = (char*) calloc(1024, sizeof(char));
        printf("Type the name of the file (without extension): ");
        scanf("%s", buffer);
        strcat(buffer, ".txt");
        printHashTable(buffer);
    }
    return 0;
}
Ejemplo n.º 8
0
void testcase()
{
	clearHashTable();

	int numOperations;
	scanf("%d\n", &numOperations);
	while(numOperations-->0)
	{
		scanf("%s\n", lineBuffer);
		if(lineBuffer[0] == 'A')
			addEntry(lineBuffer+4);
		else
			removeEntry(lineBuffer+4);
	}

	printHashTable();
}
Ejemplo n.º 9
0
int main(void)
{
  Error error;
  HashTable *table;
  Data *data = NULL;
  error.error = 0;
  strcpy((char *)(error.str),"\0");

  table = initializeHashTable(2,&error);

  setDataInTableByString(table,"abc",10,&error);
  puts("=========");
  setDataInTableByString(table,"aaaaaaaaaaaaaa",5,&error);
  puts("=========");
  setDataInTableByString(table,"abd",1,&error);
  puts("=========");
  setDataInTableByString(table,"abc",7,&error);
  puts("=========");
  printHashTable(table);puts("====");
  puts("=========");
  data = getDataFromTableByString(table,"abc");
  puts("=========");
  puts("====");
  if (NULL != data) {
    printf("Data(%p):Key='%s',val=(%"PRIu64")\n",data,data->key,data->value);
  } else {
    printf("Not fuond!\n");
  }
  deleteDataFromTableByString(table,"abc");
  puts("=========");
  setDataInTableByString(table,"abc",7,&error);
  puts("=========");
  setDataInTableByString(table,"olololololololololololol",1000000,&error);
  puts("=========");
  setDataInTableByString(table,"AhTiZEbanyTiNahui",70000000000,&error);
  puts("=========");
  data = getDataFromTableByString(table,"AhTiZEbanyTiNahui");
  puts("=========");
  puts("====");
  if (NULL != data) {
    printf("Data(%p):Key='%s',val=(%"PRIu64")\n",data,data->key,data->value);
  } else {
    printf("Not fuond!\n");
  }
  return 0;
}
Ejemplo n.º 10
0
int main() {
	HashTablePTR table = NULL;
	for(;;) {
		char cmd[80];
		printf("Command: ");
		int retval = scanf("%79s", cmd);
		if (retval <= 0) break;
		if (strcmp(cmd, "create") == 0) {
			printf("Size: ");
			int size;
			scanf("%d", &size);
			/* note: this is a signed int because unsigned int
			 * conversion in scanf is drunk and return 0xffffffff
			 * for -1.
			 */
			if (size < 0) {
				printf("Size cannot be negative, clamping to 0.\n");
				size = 0;
			}
			if (table != NULL) DestroyHashTable(&table);
			CreateHashTable(&table, (unsigned int) size);
			
			if (table == NULL) {
				printf("create returned NULL\n");
			} else {
				printf("create returned non-NULL\n");
			}
		} else if (strcmp(cmd, "destroy") == 0) {
			freeHashTableContents(table);
			DestroyHashTable(&table);
			if (table != NULL) {
				printf("FAIL: destroy did not set NULL");
			}
		} else if (strcmp(cmd, "print") == 0) {
			printHashTable(table);
		} else if (strcmp(cmd, "set") == 0) {
			printf("Key: ");
			char key[81];
			scanf(" %80s", key);
			char* value = malloc(81 * sizeof(char));
			scanf(" %80s", value);
			char* existingData = NULL;
			int success = InsertEntry(table, key, value, (void**) &existingData);
			switch(success){
				case GET_NONE:
					printf("Inserted into blank space\n");
					break;
				case GET_COLLIDE:
					printf("Inserted after resolving hash collision\n");
					break;
				case GET_EXISTS:
					printf("Inserted after removing existing data: %s\n", existingData);
					free(existingData);
					break;
				default:
					printf("InsertEntry failed\n");
					free(value);
					break;
			}
			if (success >= 0) {
				HashTableInfo info;
				int status2 = GetHashTableInfo(table, &info);
				if (status2 == 0) {
					if (info.dynamicBehaviour && info.useFactor > info.expandUseFactor) {
						printf("Insert FAIL: use factor %f"
							" greater than expand use factor. %f\n",
							info.useFactor, info.expandUseFactor);
					}
				}
			}
		} else if (strcmp(cmd, "read") == 0) {
			printf("Key: ");
			char key[81];
			scanf(" %80s", key);
			char* value;
			int success = FindEntry(table, key, (void**) &value);
			if (success == 0) {
				printf("%s\n", value);
			} else {
				printf("readPosition returned failure\n");
			}
		} else if (strcmp(cmd, "delete") == 0) {
			printf("Key: ");
			char key[81];
			scanf(" %80s", key);
			char* value;
			int success = DeleteEntry(table, key, (void**) &value);
			if (success == 0) {
				printf("Deleted (was %s)\n", value);
				free(value);
				HashTableInfo info;
				int status2 = GetHashTableInfo(table, &info);
				if (status2 == 0) {
					if (info.dynamicBehaviour &&
						info.useFactor < info.contractUseFactor) {
						printf("Delete FAIL: use factor %f"
							" less than contract use factor. %f\n",
							info.useFactor, info.contractUseFactor);
					}
				}
			} else {
				printf("DeleteValue returned failure\n");
			}
		} else if (strcmp(cmd, "load") == 0) {
			float myfloat;
			int status = GetLoadFactor(table, &myfloat);
			if (status == 0) {
				printf("Load factor: %f\n", myfloat);
			} else {
				printf("Load factor returned failure\n");
			}
		} else if (strcmp(cmd, "sentinel") == 0) {
			sentinelTest();
		} else if (strcmp(cmd, "info") == 0) {
			HashTableInfo info;
			int status = GetHashTableInfo(table, &info);
			if (status == 0) {
				printf(
					"Bucket count: %u\n"
					"Load factor: %f\n"
					"Use factor: %f\n"
					"Largest bucket size: %u\n"
					"Dynamic behaviour: %d\n"
					"Expand use factor: %f\n"
					"Contract use factor: %f\n",
					info.bucketCount, info.loadFactor, info.useFactor, info.largestBucketSize,
					info.dynamicBehaviour, info.expandUseFactor, info.contractUseFactor);
			} else {
				printf("GetHashTableInfo returned failure\n");
			}
		} else if (strcmp(cmd, "setresize") == 0) {
			int resize;
			float expand, contract;
			printf("Resize: (0/1) ");
			scanf("%d", &resize);
			printf("Expand: ");
			scanf("%f", &expand);
			printf("Contract: ");
			scanf("%f", &contract);
			int status = SetResizeBehaviour(table, resize, expand, contract);
			if (status != 0) {
				printf("Set resize returned failure\n");
			}
		} else if (strcmp(cmd, "quit") == 0) {
			break;
		} else {
			printf("Invalid command\n");
		}
	}
	if (table != NULL) {
		freeHashTableContents(table);
		DestroyHashTable(&table);
	}
	return 0;
}