-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
89 lines (80 loc) · 1.73 KB
/
main.c
File metadata and controls
89 lines (80 loc) · 1.73 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
#include "stdio.h"
#include "stdlib.h"
typedef struct node{
int val;
struct node* next;
}Node;
int gethash (int n){
int hash = n % 13;
return hash;
}
int add_node (Node *table, Node *n){
int hash = gethash(n->val);
Node* tmp = &table[hash];
while (tmp->next != NULL){
if (tmp->val == n->val){
printf ("ERROR: The value is already in the hash table\n");
printf ("value in table %d, val to enter %d\n", tmp->val, n->val);
return 0;
}
tmp = tmp->next;
}
tmp->next = n;
return 1;
}
void print_table (Node *table){
Node* tmp = malloc (sizeof (Node));
int i = 0;
for (i = 0; i < 13; i++){
printf ("Table[i].val = ");
tmp = &table[i];
while (tmp->next != NULL){
printf ("%d ---> ", tmp->val);
tmp = tmp->next;
}
printf ("%d\n", tmp->val);
}
}
void find_value (Node table){
}
int main(){
int i = 0;
int val = 0;
int option = 0;
Node *hash = (Node*)malloc (sizeof (Node)*13);
if (hash[1].next == NULL){
printf ("this?\n");
}
//Node *n = malloc (sizeof(Node));
while (1){
printf ("1 ........ Add value to hash table\n2 ........ Find value in hash table\n3 ........ Print table\n");
scanf("%d", &option);
if (option == 1){
printf ("Please enter a value to add: ");
scanf ("%d", &val);
Node *n = malloc (sizeof (Node));
n->val = val;
n->next = NULL;
add_node(hash, n);
//free(n);
}
else if (option == 2){
//TODO do the find function
}
else if (option == 3){
print_table (hash);
}
else{
printf ("Not a valid option \n");
}
}
/*n->val = 14;
n->next = NULL;
add_node (hash, n);
printf ("%d, %d, %d \n", hash[1].val, hash[1].next->val, hash[1].next->next->val);
if (hash[5].next == NULL){
printf ("ha\n");
}*/
printf ("Hello World! %d\n", i);
return 0;
}