summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-06-22 17:11:49 -0600
committermo khan <mo.khan@gmail.com>2020-06-22 17:11:49 -0600
commitd71a8aa3a8dda7d53f01d2dc6a558bed4289154c (patch)
tree00a9dfe67d3a6011fd5c4b5a8940b95934428b61
parent0f0ab99a5eb26fb5f107fe0ad32f0569a2123cdb (diff)
Assign other next/prev
-rw-r--r--assignments/01/swap_doubly_linked_list_test.c13
1 files changed, 6 insertions, 7 deletions
diff --git a/assignments/01/swap_doubly_linked_list_test.c b/assignments/01/swap_doubly_linked_list_test.c
index 8c3490d..3575954 100644
--- a/assignments/01/swap_doubly_linked_list_test.c
+++ b/assignments/01/swap_doubly_linked_list_test.c
@@ -80,11 +80,17 @@ static int size(Node *head) {
static void assign_next(Node *self, Node *other) {
if (self)
self->next = other;
+
+ if (other)
+ other->prev = self;
}
static void assign_prev(Node *self, Node *other) {
if (self)
self->prev = other;
+
+ if (other)
+ other->next = self;
}
static void swap(Node *x, Node *y) {
@@ -97,20 +103,13 @@ static void swap(Node *x, Node *y) {
// if adjacent
if (x->next == y && y->prev == x) {
assign_next(x, yn);
- assign_prev(x->next, x);
assign_prev(x, y);
- assign_next(y, x);
assign_prev(y, xp);
- assign_next(y->prev, y);
} else {
assign_prev(x, yp);
- assign_next(x->prev, x);
assign_next(x, yn);
- assign_prev(x->next, x);
assign_prev(y, xp);
- assign_next(y->prev, y);
assign_next(y, xn);
- assign_prev(y->next, y);
}
}