-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmap.c
More file actions
134 lines (112 loc) · 3.19 KB
/
hashmap.c
File metadata and controls
134 lines (112 loc) · 3.19 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* This program will ask the user to enter a list of Names, then the names will be entered into an array/linked list data structure by using a hash function.
* -Insertion, search and lookup should be of constant time best case scenario
*
* objective of hash function
* - minimize collisions
* - uniform distribution of hash values (data should be spread accross table)
* - easy to calculate
* - collisions resolved with open or closed addressing
*
* Hashing is used in database indexing,password authentication etc.
*
**/
#define hashSize 10
int size = 0;
struct NameStruct *hashArray[hashSize];
struct NameStruct
{
int key;
char name[20];
int age;
};
int hashFunction(char *name)
{
int key = 0;
int i = 0;
while (i <= strlen(name))
{
key = key + name[i];
i++;
}
//printf("check 1: %d\n", key);
//printf("check 2: %d\n\n", (key % hashSize));
return (key % hashSize);
}
void insertName(char *name)
{
struct NameStruct *Name = (struct NameStruct *)malloc(sizeof(struct NameStruct));
//hashes key
int index = hashFunction(name);
//populates struct object
Name->key = index;
strcpy(Name->name, name);
//entering hash into hashtable
while (hashArray[index] != NULL)
{
//traverses through array to find a spot for Name
index++;
index %= hashSize;
}
hashArray[index] = Name;
//printf("%s\n", Name->name);
printf("index is: %d\n", index);
}
struct NameStruct *searchName(char *name)
{
int index = hashFunction(name);
if (strcmp(hashArray[index]->name, name) == 0)
{
return hashArray[index];
}
printf("Name does not exist in map.\n");
return NULL;
}
void display()
{
for (int i = 0; i < hashSize; i++)
{
if (hashArray[i] != NULL)
printf(" (%d,%s)\n", hashArray[i]->key, hashArray[i]->name);
}
printf("\n");
}
int main()
{
char name[20];
int choice = 1;
int iterator = 0;
printf("\n----------------------------------\n");
printf("\n\nWelcome to the Name dictionary\n\nHow many names would you like to enter in the dictionary?\n");
scanf("%d", &size);
while (choice)
{
if (iterator == size)
{
printf("Sorry, you cannot add any more names.\n");
break;
}
printf("Please enter a Name to be added to the list.");
scanf("%s", name);
insertName(name);
iterator++;
printf("\nWould you like to enter another name?\nEnter 1 to enter another name, or enter 0 to search list.\n");
scanf("%d", &choice);
}
printf("\n----------------------------------\n");
display();
int key = 0;
printf("\n----------------------------------\n");
printf("Enter the key associated with the name you would like to retrieve:\n");
//print table
scanf("%d", &key);
struct NameStruct *retrieval = searchName(name);
printf("The name retrieved is: %s\n", retrieval->name);
printf("The key retrieved is %d\n", retrieval->key);
printf("program successful!\n");
printf("\n----------------------------------\n");
return 0;
}