summaryrefslogtreecommitdiff
path: root/src/01/02a
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/02a
parente27441426cd127c1f9969da1fcd2fde0ab415586 (diff)
run make fmt
Diffstat (limited to 'src/01/02a')
-rw-r--r--src/01/02a/main.c13
-rw-r--r--src/01/02a/singly_linked_list.c24
-rw-r--r--src/01/02a/singly_linked_list_test.c11
3 files changed, 26 insertions, 22 deletions
diff --git a/src/01/02a/main.c b/src/01/02a/main.c
index f566031..95d8c2e 100644
--- a/src/01/02a/main.c
+++ b/src/01/02a/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 2a ===\n");
Node *head = initialize(next());
@@ -17,9 +14,9 @@ int main(int argc, char *argv[])
printf("\n\t");
inspect(head);
- for (int i = 0; i < 10; i+=2){
- swap(&head, i, i+1);
- printf("swap: %d,%d\n\t", i, i+1);
+ for (int i = 0; i < 10; i += 2) {
+ swap(&head, i, i + 1);
+ printf("swap: %d,%d\n\t", i, i + 1);
inspect(head);
}
diff --git a/src/01/02a/singly_linked_list.c b/src/01/02a/singly_linked_list.c
index 2271965..bdf77da 100644
--- a/src/01/02a/singly_linked_list.c
+++ b/src/01/02a/singly_linked_list.c
@@ -25,7 +25,7 @@ Node *add(Node *head, int data) {
Node *tail;
Node *tmp = head;
- while(tmp) {
+ while (tmp) {
if (!tmp->next)
break;
tmp = tmp->next;
@@ -43,9 +43,10 @@ Node *add(Node *head, int data) {
* @return The node at the specific index
*/
Node *get(Node *self, int index) {
- if (!self || index < 0) return NULL;
+ if (!self || index < 0)
+ return NULL;
- while(index > 0 && self){
+ while (index > 0 && self) {
self = self->next;
index--;
}
@@ -60,7 +61,8 @@ Node *get(Node *self, 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;
}
@@ -75,9 +77,12 @@ static int size(Node *head) {
void swap(Node **head, int x, int y) {
int count = size(*head);
- if (x == y) return;
- if (x >= count) return;
- if (y >= count) return;
+ if (x == y)
+ return;
+ if (x >= count)
+ return;
+ if (y >= count)
+ return;
Node *xp = get(*head, x - 1);
Node *yp = get(*head, y - 1);
@@ -106,10 +111,11 @@ void swap(Node **head, int x, int y) {
* @param self The head of the linked list
*/
void inspect(Node *self) {
- if (!self) return;
+ if (!self)
+ return;
printf("[");
- while(self) {
+ while (self) {
printf(" %d ", self->data);
self = self->next;
}
diff --git a/src/01/02a/singly_linked_list_test.c b/src/01/02a/singly_linked_list_test.c
index 63cbd6a..5fc90e3 100644
--- a/src/01/02a/singly_linked_list_test.c
+++ b/src/01/02a/singly_linked_list_test.c
@@ -2,8 +2,8 @@
#include <cgreen/cgreen.h>
Describe(SinglyLinkedList);
-BeforeEach(SinglyLinkedList){ }
-AfterEach(SinglyLinkedList){ }
+BeforeEach(SinglyLinkedList) {}
+AfterEach(SinglyLinkedList) {}
Ensure(SinglyLinkedList, when_getting_head) {
Node *head = initialize(100);
@@ -54,7 +54,6 @@ Ensure(SinglyLinkedList, when_getting_index_out_of_range) {
free(head);
}
-
Ensure(SinglyLinkedList, when_swapping_head) {
Node *head = initialize(100);
@@ -177,14 +176,16 @@ TestSuite *swap_singly_linked_list_tests() {
add_test_with_context(suite, SinglyLinkedList, when_getting_tail);
add_test_with_context(suite, SinglyLinkedList, when_getting_from_empty_list);
add_test_with_context(suite, SinglyLinkedList, when_getting_negative_index);
- add_test_with_context(suite, SinglyLinkedList, when_getting_index_out_of_range);
+ add_test_with_context(suite, SinglyLinkedList,
+ when_getting_index_out_of_range);
add_test_with_context(suite, SinglyLinkedList, when_swapping_head);
add_test_with_context(suite, SinglyLinkedList, when_swapping_y_head);
add_test_with_context(suite, SinglyLinkedList, when_swapping_mid);
add_test_with_context(suite, SinglyLinkedList, when_swapping_y_mid);
add_test_with_context(suite, SinglyLinkedList, when_swapping_tail);
- add_test_with_context(suite, SinglyLinkedList, when_swapping_index_out_of_range);
+ add_test_with_context(suite, SinglyLinkedList,
+ when_swapping_index_out_of_range);
add_test_with_context(suite, SinglyLinkedList, when_swapping_self);
return suite;