Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DHT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
emsouza
DHT
Commits
935a194f
Commit
935a194f
authored
8 months ago
by
Eduardo Souza
Browse files
Options
Downloads
Patches
Plain Diff
[FIX] Inserção
parent
76543468
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
a.out
+0
-0
0 additions, 0 deletions
a.out
t2.c
+24
-28
24 additions, 28 deletions
t2.c
with
24 additions
and
28 deletions
a.out
+
0
−
0
View file @
935a194f
No preview for this file type
This diff is collapsed.
Click to expand it.
t2.c
+
24
−
28
View file @
935a194f
...
@@ -21,25 +21,24 @@ typedef struct DHT {
...
@@ -21,25 +21,24 @@ typedef struct DHT {
int
max_id
;
int
max_id
;
}
DHT
;
}
DHT
;
void
redistribute_keys
(
Node
*
new_node
)
{
void
redistribute_keys
(
Node
*
dht
,
Node
*
new_node
)
{
Node
*
current
=
new_node
;
Node
*
start
=
dht
;
do
{
do
{
Node
*
next_node
=
current
->
next
;
Node
*
current
=
new_node
;
// Transferir chaves que são menores ou iguais ao id do próximo nó
printf
(
"Start:%d Next:%d
\n
"
,
start
->
id
,
current
->
id
);
for
(
int
i
=
0
;
i
<
current
->
key_count
;
i
++
)
{
printf
(
"Start->key_count:%d
\n
"
,
start
->
key_count
);
if
((
current
->
keys
[
i
]
>
current
->
id
&&
current
->
keys
[
i
]
<=
next_node
->
id
)
||
//O novo nodo deve ter as chaves EXISTENTES que são maiores que o antecessor e menores ou iguais a ele
(
current
->
id
>
next_node
->
id
&&
(
current
->
keys
[
i
]
>
current
->
id
||
current
->
keys
[
i
]
<=
next_node
->
id
)))
{
for
(
int
i
=
0
;
i
<
start
->
key_count
;
i
++
)
{
next_node
->
keys
[
next_node
->
key_count
++
]
=
current
->
keys
[
i
];
printf
(
"Start->keys[%d]:%d
\n
"
,
i
,
start
->
keys
[
i
]);
// Remover a chave do nó atual
if
(
start
->
keys
[
i
]
>
start
->
id
&&
start
->
keys
[
i
]
<=
current
->
id
)
{
for
(
int
j
=
i
;
j
<
current
->
key_count
-
1
;
j
++
)
{
printf
(
"SAIA CHAVE SAIAAAA"
);
current
->
keys
[
j
]
=
current
->
keys
[
j
+
1
];
current
->
keys
[
current
->
key_count
++
]
=
start
->
keys
[
i
];
}
start
->
keys
[
i
]
=
0
;
current
->
key_count
--
;
start
->
key_count
--
;
i
--
;
// Reavaliar a posição atual
}
}
}
}
current
=
next_node
;
start
=
start
->
next
;
}
while
(
curren
t
!=
new_node
);
}
while
(
star
t
!=
new_node
);
}
}
// Função para criar um novo nó
// Função para criar um novo nó
...
@@ -77,7 +76,7 @@ Node *insert_node(Node *head, int id, int max_id) {
...
@@ -77,7 +76,7 @@ Node *insert_node(Node *head, int id, int max_id) {
new_node
->
next
=
head
;
new_node
->
next
=
head
;
new_node
->
prev
=
last
;
new_node
->
prev
=
last
;
head
->
prev
=
new_node
;
head
->
prev
=
new_node
;
redistribute_keys
(
new_node
);
redistribute_keys
(
head
,
new_node
);
return
new_node
;
// Novo nó se torna a nova cabeça
return
new_node
;
// Novo nó se torna a nova cabeça
}
}
...
@@ -91,8 +90,7 @@ Node *insert_node(Node *head, int id, int max_id) {
...
@@ -91,8 +90,7 @@ Node *insert_node(Node *head, int id, int max_id) {
new_node
->
next
=
current
;
new_node
->
next
=
current
;
new_node
->
prev
=
prev
;
new_node
->
prev
=
prev
;
current
->
prev
=
new_node
;
current
->
prev
=
new_node
;
redistribute_keys
(
head
,
new_node
);
redistribute_keys
(
new_node
);
return
head
;
return
head
;
}
}
...
@@ -270,16 +268,14 @@ void insert_key(Node* head, int node_id, int key) {
...
@@ -270,16 +268,14 @@ void insert_key(Node* head, int node_id, int key) {
while
(
current
->
id
!=
node_id
)
{
while
(
current
->
id
!=
node_id
)
{
current
=
current
->
next
;
current
=
current
->
next
;
}
}
do
{
if
((
current
->
id
<
current
->
next
->
id
&&
key
>
current
->
id
&&
key
<=
current
->
next
->
id
)
||
//A partir no nosso nó, procuramos o próximo nó que cabe a chave
(
current
->
id
>
current
->
next
->
id
&&
(
key
>
current
->
id
||
key
<=
current
->
next
->
id
)))
{
while
(
current
->
id
<
key
&&
current
!=
head
){
current
->
next
->
keys
[
current
->
next
->
key_count
++
]
=
key
;
printf
(
"Chave %d inserida no nó %d
\n
"
,
key
,
current
->
id
);
return
;
}
current
=
current
->
next
;
current
=
current
->
next
;
}
while
(
current
!=
head
);
}
current
->
keys
[
current
->
key_count
++
]
=
key
;
printf
(
"Chave %d inserida no nó %d
\n
"
,
key
,
current
->
id
);
}
}
void
imprime_chaves
(
Node
*
head
)
{
void
imprime_chaves
(
Node
*
head
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment