본문 바로가기

데이터구조/소스코드

Hasing 예제코드

반응형
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SZ 15

struct info
{
	char *name;
	unsigned short age;
	char *city;
	struct info *next;
	struct info *city_next;
};

struct info* hashTable[SZ];
struct info* cityHashTable[SZ];

//
// determine index by applying hasing function to _name
//
int hashingByName(char *_name)
{
	int sum = 0;
	int length = strlen(_name);
	for (int i = 0; i < length; i++)
	{
		sum += _name[i];
	}
	return (sum % SZ);
}

int hashingByCity(char *_city)
{
	return(_city[0] % SZ);
}

void showData(int _idx, char* _name)
{
	struct info *temp = hashTable[_idx];

	while (1)
	{
		if (strcmp(_name, temp->name) != 0)
		{
			temp = temp->next;
		}
		else
		{
			break;
		}
	}

	printf("%s %d %s\n", temp->name,
		temp->age, temp->city);
}

void searchByCity(char *_city)
{
	int idx = hashingByCity(_city);
	struct info *temp = cityHashTable[idx];
	printf("\n\n Who lives in %s\n", _city);
	while (temp != 0)
	{
		printf("%s %d %s\n", temp->city, temp->age, temp->name);
		temp = temp->city_next;
	}
}

int main(void)
{
	int n;
	scanf("%d", &n);

	for (int i = 0; i < n; i++)
	{
		char _name[100];
		char _city[100];
		int _age;
		
		scanf("%s %d %s", _name, &_age, _city);

		struct info *cur = 0;
		cur = (struct info *)malloc(sizeof(struct info));
		cur->next = 0;
		cur->city_next = 0;
		cur->age = (unsigned short)_age;
		cur->name = (char *)malloc(strlen(_name) + 1);
		strcpy(cur->name, _name);
		cur->city = (char *)malloc(strlen(_city) + 1);
		strcpy(cur->city, _city);

		// hasing by Name
		int idx = hashingByName(_name);
		if (hashTable[idx] == 0)
		{
			hashTable[idx] = cur;
		}
		else
		{
			printf("Collision!!!!!!!\n");
			printf("%s\n", _name);
			struct info *temp = hashTable[idx];
			while (temp->next != 0)
			{
				temp = temp->next;
			}
			temp->next = cur;
		}

		idx = hashingByCity(_city);
		if (cityHashTable[idx] == 0)
		{
			cityHashTable[idx] = cur;
		}
		else
		{
			printf("City Collision!!!!!!!\n");
			printf("%s\n", _city);
			struct info *temp = cityHashTable[idx];
			while (temp->city_next != 0)
			{
				temp = temp->city_next;
			}
			temp->city_next = cur;
		}

		//printf("%s %d %s\n", _name, _age, _city);
	}
	printf("Data input ended!!!\n");

	//showData(hashingByName("alice"), "alice");
	searchByCity("california");

	return 0;
}
반응형

'데이터구조 > 소스코드' 카테고리의 다른 글

Quick sort 소스코드  (0) 2016.05.26
Priority Queue, Min Heap  (0) 2016.05.16
BST를 이용한 영어/한글 단어관리 프로그램 소스  (0) 2016.05.09
OJ 1161  (0) 2016.04.25
OJ 1160  (0) 2016.04.25