Skip to content

Commit 3f97959

Browse files
committed
hash finalizado, começando binary tree
1 parent 44b3a76 commit 3f97959

File tree

2 files changed

+148
-102
lines changed

2 files changed

+148
-102
lines changed

binaryTree.c

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <math.h>
5+
6+
typedef struct Node
7+
{
8+
int data;
9+
struct _Node* left;
10+
struct _Node* right;
11+
}Node;
12+
typedef struct _binaryTree
13+
{
14+
int height;
15+
Node* root;
16+
}binaryTree;
17+
18+
binaryTree* iniT()
19+
{
20+
binaryTree* newTree = (binaryTree*) malloc(sizeof(binaryTree));
21+
newTree->height = 0;
22+
newTree->root = NULL;
23+
return newTree;
24+
}
25+
void left(int value)
26+
{
27+
28+
}
29+
void right(int value)
30+
{
31+
32+
}
33+
void add(int value, binaryTree* rootz)
34+
{
35+
if (rootz->height == 0)
36+
{
37+
addRoot(value,rootz);
38+
}
39+
40+
rootz->height++;
41+
}
42+
int main()
43+
{
44+
int n;
45+
binaryTree* bT = iniT();
46+
while (scanf("%d",&n) != EOF)
47+
{
48+
printf("----\nAdicionando %d\n",n);
49+
add(n,bT);
50+
printTree();
51+
52+
}
53+
return 0;
54+
}

hash.c

+94-102
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,109 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
3-
#include <string.h>
4-
//https://thehuxley.com/problem/242?quizId=6236
5-
#define DEBUG if(0)
6-
typedef struct _node
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
//https://thehuxley.com/problem/242?quizId=6236
5+
#define DEBUG if(0)
6+
typedef struct _node
7+
{
8+
int data;
9+
struct _node* next;
10+
}node;
11+
typedef struct _list
12+
{
13+
int size;
14+
node* tail;
15+
node* head;
16+
}list;
17+
typedef struct table
18+
{
19+
list* tables[100];
20+
}table;
21+
int hashFunction(int value, int base)
22+
{
23+
return value%base;
24+
}
25+
list* initList()
26+
{
27+
list* new_list = (list*) malloc (sizeof(list));
28+
new_list->head = NULL;
29+
new_list->tail = NULL;
30+
new_list->size = 0;
31+
return new_list;
32+
}
33+
table* initTable()
34+
{
35+
table* newTable = (table*) malloc(sizeof(table));
36+
for (int i = 0; i < 100; i++)
737
{
8-
int data;
9-
struct _node* next;
10-
}node;
11-
typedef struct _list
12-
{
13-
int size;
14-
node* tail;
15-
node* head;
16-
}list;
17-
typedef struct table
18-
{
19-
list* tables[100];
20-
}table;
21-
22-
int hashFunction(int value, int base)
23-
{
24-
return value%base;
25-
}
26-
list* initList()
27-
{
28-
list* new_list = (list*) malloc (sizeof(list));
29-
new_list->head = NULL;
30-
new_list->tail = NULL;
31-
new_list->size = 0;
32-
return new_list;
38+
newTable->tables[i] = initList();
3339
}
34-
table* initTable()
40+
return newTable;
41+
}
42+
void addListTail(list* lista, int value)
43+
{
44+
node* new_node = (node*) malloc(sizeof(node));
45+
new_node->data = value;
46+
new_node->next = NULL;
47+
if (lista->size == 0)
3548
{
36-
table* newTable = (table*) malloc(sizeof(table));
37-
for (int i = 0; i < 100; i++)
38-
{
39-
newTable->tables[i] = initList();
40-
}
41-
return newTable;
42-
}
43-
void addListTail(list* lista, int value)
44-
{
45-
node* new_node = (node*) malloc(sizeof(node));
46-
new_node->data = value;
47-
new_node->next = NULL;
48-
if (lista->size == 0)
49-
{
50-
lista->head = new_node;
51-
lista->tail = new_node;
52-
lista->size++;
53-
return;
54-
}
55-
lista->tail->next = new_node;
49+
lista->head = new_node;
5650
lista->tail = new_node;
5751
lista->size++;
52+
return;
5853
}
59-
60-
void addHashed(table* ht, int value, int key)
54+
lista->tail->next = new_node;
55+
lista->tail = new_node;
56+
lista->size++;
57+
}
58+
void addHashed(table* ht, int value, int key)
59+
{
60+
addListTail(ht->tables[key],value);
61+
}
62+
void get(int baseNum, int keysNum, table* ht)
63+
{
64+
int value, key;
65+
for (int i = 0; i < keysNum; i++)
6166
{
62-
addListTail(ht->tables[key],value);
67+
scanf("%d",&value);
68+
key = hashFunction(value,baseNum);
69+
addHashed(ht,value,key);
6370
}
64-
65-
void get(int baseNum, int keysNum, table* ht)
71+
}
72+
void printao(list* list, int key)
73+
{
74+
printf("%d -> ", key);
75+
node* tmp = list->head;
76+
while (tmp != NULL)
6677
{
67-
int value, key;
68-
for (int i = 0; i < keysNum; i++)
69-
{
70-
scanf("%d",&value);
71-
key = hashFunction(value,baseNum);
72-
addHashed(ht,value,key);
73-
}
74-
78+
printf("%d -> ", tmp->data);
79+
tmp = tmp->next;
7580
}
76-
void printao(list* list, int key)
81+
printf("\\");
82+
printf("\n");
83+
}
84+
void print(table* ht, int keysNum)
85+
{
86+
for (int i = 0; i < keysNum; i++)
7787
{
78-
printf("%d -> ", key);
79-
node* tmp = list->head;
80-
while (tmp != NULL)
81-
{
82-
printf("%d -> ", tmp->data);
83-
tmp = tmp->next;
84-
}
85-
printf("\\");
86-
printf("\n");
87-
88+
printao(ht->tables[i],i);
8889
}
89-
void print(table* ht, int keysNum)
90+
printf("\n");
91+
92+
}
93+
int main()
94+
{
95+
int testNumber;
96+
scanf("%d",&testNumber);
97+
for (int i = 0; i < testNumber; i++)
9098
{
91-
for (int i = 0; i < keysNum; i++)
92-
{
93-
printao(ht->tables[i],i);
94-
}
95-
printf("\n");
96-
99+
DEBUG printf("----------- Caso de teste #%d ----------\n",i);
100+
int baseNumber, keysNumber;
101+
scanf("%d",&baseNumber);
102+
scanf("%d",&keysNumber);
103+
DEBUG printf("Base = [%d], Chaves [%d]\n",baseNumber,keysNumber);
104+
table* ht = initTable();
105+
get(baseNumber,keysNumber,ht);
106+
print(ht,baseNumber);
97107
}
98-
int main()
99-
{
100-
int testNumber;
101108

102-
scanf("%d",&testNumber);
103-
for (int i = 0; i < testNumber; i++)
104-
{
105-
DEBUG printf("----------- Caso de teste #%d ----------\n",i);
106-
int baseNumber, keysNumber;
107-
scanf("%d",&baseNumber);
108-
scanf("%d",&keysNumber);
109-
DEBUG printf("Base = [%d], Chaves [%d]\n",baseNumber,keysNumber);
110-
111-
112-
table* ht = initTable();
113-
get(baseNumber,keysNumber,ht);
114-
print(ht,baseNumber);
115-
}
116-
117-
}
109+
}

0 commit comments

Comments
 (0)