summaryrefslogtreecommitdiff
path: root/src/01/02b
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-07-05 14:01:59 -0600
committermo khan <mo.khan@gmail.com>2020-07-05 14:01:59 -0600
commit8bf32957fef23b85b1421c38b6416047bcba476e (patch)
tree3eece4d270f52c91a6c22e84b6eb636fd57326c1 /src/01/02b
parente27441426cd127c1f9969da1fcd2fde0ab415586 (diff)
run make fmt
Diffstat (limited to 'src/01/02b')
-rw-r--r--src/01/02b/doubly_linked_list.c23
-rw-r--r--src/01/02b/doubly_linked_list_test.c27
-rw-r--r--src/01/02b/main.c9
3 files changed, 34 insertions, 25 deletions
diff --git a/src/01/02b/doubly_linked_list.c b/src/01/02b/doubly_linked_list.c
index 4bb71b8..24e1734 100644
--- a/src/01/02b/doubly_linked_list.c
+++ b/src/01/02b/doubly_linked_list.c
@@ -14,7 +14,7 @@ Node *add(Node *head, int data) {
Node *tail;
Node *tmp = head;
- while(tmp) {
+ while (tmp) {
if (!tmp->next)
break;
tmp = tmp->next;
@@ -26,9 +26,10 @@ Node *add(Node *head, int data) {
}
Node *get(Node *from, int index) {
- if (!from || index < 0) return NULL;
+ if (!from || index < 0)
+ return NULL;
- while(index > 0 && from){
+ while (index > 0 && from) {
from = from->next;
index--;
}
@@ -37,7 +38,8 @@ Node *get(Node *from, int index) {
static int size(Node *head) {
int i = 0;
- for (Node *tmp = head; tmp && tmp != NULL; tmp = tmp->next) i++;
+ for (Node *tmp = head; tmp && tmp != NULL; tmp = tmp->next)
+ i++;
return i;
}
@@ -58,9 +60,12 @@ static void assign_prev(Node *self, Node *other) {
}
void swap(Node *x, Node *y) {
- if (x == y) return;
- if (!x || !y) return;
- if (x->prev == y && y->next == x) return swap(y, x);
+ if (x == y)
+ return;
+ if (!x || !y)
+ return;
+ if (x->prev == y && y->next == x)
+ return swap(y, x);
Node *xp = x->prev, *xn = x->next, *yp = y->prev, *yn = y->next;
@@ -100,7 +105,8 @@ static void print(Node *node) {
}
void inspect(Node *node) {
- if (!node) return;
+ if (!node)
+ return;
printf("[ ");
while (node) {
@@ -110,4 +116,3 @@ void inspect(Node *node) {
}
printf("]\n");
}
-
diff --git a/src/01/02b/doubly_linked_list_test.c b/src/01/02b/doubly_linked_list_test.c
index f5a1004..0571ef0 100644
--- a/src/01/02b/doubly_linked_list_test.c
+++ b/src/01/02b/doubly_linked_list_test.c
@@ -1,9 +1,9 @@
-#include <cgreen/cgreen.h>
#include "doubly_linked_list.h"
+#include <cgreen/cgreen.h>
Describe(DoublyLinkedList);
-BeforeEach(DoublyLinkedList){ }
-AfterEach(DoublyLinkedList){ }
+BeforeEach(DoublyLinkedList) {}
+AfterEach(DoublyLinkedList) {}
Ensure(DoublyLinkedList, when_getting_head) {
Node *head = initialize(100);
@@ -440,20 +440,27 @@ TestSuite *swap_doubly_linked_list_tests() {
add_test_with_context(suite, DoublyLinkedList, when_getting_tail);
add_test_with_context(suite, DoublyLinkedList, when_getting_from_empty_list);
add_test_with_context(suite, DoublyLinkedList, when_getting_negative_index);
- add_test_with_context(suite, DoublyLinkedList, when_getting_index_out_of_range);
+ add_test_with_context(suite, DoublyLinkedList,
+ when_getting_index_out_of_range);
add_test_with_context(suite, DoublyLinkedList, when_swapping_head_adjacent);
- add_test_with_context(suite, DoublyLinkedList, when_swapping_head_adjacent_inverted);
- add_test_with_context(suite, DoublyLinkedList, when_swapping_head_with_non_adjacent_node);
- add_test_with_context(suite, DoublyLinkedList, when_swapping_head_with_non_adjacent_node_inverted);
+ add_test_with_context(suite, DoublyLinkedList,
+ when_swapping_head_adjacent_inverted);
+ add_test_with_context(suite, DoublyLinkedList,
+ when_swapping_head_with_non_adjacent_node);
+ add_test_with_context(suite, DoublyLinkedList,
+ when_swapping_head_with_non_adjacent_node_inverted);
add_test_with_context(suite, DoublyLinkedList, when_swapping_mid);
add_test_with_context(suite, DoublyLinkedList, when_swapping_y_mid);
add_test_with_context(suite, DoublyLinkedList, when_swapping_mid_adjacent);
add_test_with_context(suite, DoublyLinkedList, when_swapping_mid_adjacent_y);
add_test_with_context(suite, DoublyLinkedList, when_swapping_tail_adjacent);
- add_test_with_context(suite, DoublyLinkedList, when_swapping_tail_adjacent_inverted);
- add_test_with_context(suite, DoublyLinkedList, when_swapping_tail_with_non_adjacent_node);
- add_test_with_context(suite, DoublyLinkedList, when_swapping_tail_with_non_adjacent_node_inverted);
+ add_test_with_context(suite, DoublyLinkedList,
+ when_swapping_tail_adjacent_inverted);
+ add_test_with_context(suite, DoublyLinkedList,
+ when_swapping_tail_with_non_adjacent_node);
+ add_test_with_context(suite, DoublyLinkedList,
+ when_swapping_tail_with_non_adjacent_node_inverted);
add_test_with_context(suite, DoublyLinkedList, when_swapping_with_NULL);
add_test_with_context(suite, DoublyLinkedList, when_swapping_self);
diff --git a/src/01/02b/main.c b/src/01/02b/main.c
index 10e18e3..cce2e1f 100644
--- a/src/01/02b/main.c
+++ b/src/01/02b/main.c
@@ -2,12 +2,9 @@
#include <stdio.h>
#include <stdlib.h>
-int next(void) {
- return rand() % 100;
-}
+int next(void) { return rand() % 100; }
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
printf("=== COMP-272 - Assignment 1 - Question 2b ===\n");
Node *head = initialize(next());
Node *new_head = NULL;
@@ -24,7 +21,7 @@ int main(int argc, char *argv[])
printf("swap: 0,1\n\t");
inspect(head);
- for (int i = 2; i < 10; i+=2) {
+ for (int i = 2; i < 10; i += 2) {
swap(get(head, i), get(head, i + 1));
printf("swap: %d,%d\n\t", i, i + 1);
inspect(head);