summaryrefslogtreecommitdiffstats
path: root/src/dfs.c
diff options
context:
space:
mode:
authorAlexandre Jesus <adbjesus@gmail.com>2016-10-05 18:42:30 +0100
committerAlexandre Jesus <adbjesus@gmail.com>2016-10-05 18:42:30 +0100
commit73010dc96357a9715b87421534bebc19ecd5863d (patch)
tree3dc42a2467ba8d2ff78ac54f2c3e2377de438b5c /src/dfs.c
parentddb004a2cc4ee7906f2167493da8d43b652ecdc4 (diff)
downloadlibuknapsack-73010dc96357a9715b87421534bebc19ecd5863d.tar.gz
libuknapsack-73010dc96357a9715b87421534bebc19ecd5863d.zip
Fix issues that would lead to seg fault on the online method
Diffstat (limited to 'src/dfs.c')
-rw-r--r--src/dfs.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/src/dfs.c b/src/dfs.c
index b0bda53..12f4ee2 100644
--- a/src/dfs.c
+++ b/src/dfs.c
@@ -92,11 +92,12 @@ struct online_return dfs_online(struct data * data, struct front_item * front, s
front_last->next = new_front_item(tree->values.p, tree->values.w, front_last, NULL);
front_last = front_last->next;
+ /* Adjust prev and next */
+ if(tree->prev != NULL) tree->prev->next = tree->next;
+ if(tree->next != NULL) tree->next->prev = tree->prev;
+
struct tree_item * old = tree;
tree = tree->next;
- if(tree != NULL) {
- tree->prev = old->prev;
- }
free(old);
change++;
@@ -125,6 +126,10 @@ struct online_return dfs_online(struct data * data, struct front_item * front, s
/* Update left next */
left->next = right;
+ /* Adjust prev and next */
+ if(tree->prev != NULL) tree->prev->next = left;
+ if(tree->next != NULL) tree->next->prev = right;
+
/* Update tree to left node and free old node */
struct tree_item * old = tree;
tree = left;