반응형
struct bucket* hashTable = NULL;
struct bucket* hashTable1 = NULL;
struct bucket* hashTable2 = NULL;
int SIZE = 11;
int SIZE1 = 101;
int SIZE2 = 1001;


struct Node {
	int key;
	int value;
	struct Node* next;
};

struct bucket {

	struct Node* head;
	int count;
};

struct Node* createNode(int key, int value) {

	struct Node* newNode;

	newNode = new Node;

	newNode->key = key;
	newNode->value = value;

	newNode->next = NULL;

	return newNode;
}

int hashFunction(int key, int SIZE) {

	return key % SIZE;
}

int hashBuild = 0;
int hashBuild1 = 0;
int hashBuild2 = 0;
void insert(struct bucket* hashTable, int key, int value, int SIZE) {

	int hashIndex = hashFunction(key, SIZE);

	struct Node* newNode = createNode(key, value);

	if (hashTable[hashIndex].count == 0) {
		hashTable[hashIndex].head = newNode;
		hashTable[hashIndex].count = 1;
		hashBuild++;

	}

	else {
		newNode->next = hashTable[hashIndex].head;
		hashTable[hashIndex].head = newNode;
		hashTable[hashIndex].count++;
		hashBuild++;

	}

	return;
}
int nodeCompare = 0;
void search(struct bucket* hashTable, int key, int SIZE) {

	int hashIndex = hashFunction(key, SIZE);
	struct Node* node = hashTable[hashIndex].head;

	if (node == NULL) {

		nodeCompare++;
		return;
	}

	while (node != NULL) {
		if (node->key == key) {
			nodeCompare++;
			//std::cout << "key found key = " << node->key << " value = " << node->value;
			break;
		}
		nodeCompare++;
		node = node->next;
	}
	//std::cout << "\nno key found";
	return;
}

void display() {
	struct Node* horse;
	int i = 0;

	for (i = 0; i < SIZE; i++) {

		horse = hashTable[i].head;

		std::cout << "Bucket[" << i << "] : ";

		while (horse != NULL) {
			printf("(key:%d, val:%d) -> ", horse->key, horse->value);
			horse = horse->next;

		}
		printf("\n");
	}
	printf("\n-------end of display--------");
	return;
}
반응형

+ Recent posts