diff options
| author | mo khan <mo.khan@gmail.com> | 2020-07-04 17:56:01 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-07-04 17:56:01 -0600 |
| commit | fd62033e4c79cf2753ba3518af709c7a4102f3bc (patch) | |
| tree | e11c4a2300dbb2f8ab17d20e50a4917e148441be /src | |
| parent | 8ef8f0e1f1e96f823a3cee027d65a34a3d515ba9 (diff) | |
Split doubly linked list into separate files
Diffstat (limited to 'src')
| -rw-r--r-- | src/01/02b/Makefile | 37 | ||||
| -rw-r--r-- | src/01/02b/doubly_linked_list.c | 112 | ||||
| -rw-r--r-- | src/01/02b/doubly_linked_list.h | 13 | ||||
| -rw-r--r-- | src/01/02b/doubly_linked_list_test.c (renamed from src/01/02b/swap_doubly_linked_list_test.c) | 124 | ||||
| -rw-r--r-- | src/01/02b/main.c | 15 |
5 files changed, 185 insertions, 116 deletions
diff --git a/src/01/02b/Makefile b/src/01/02b/Makefile new file mode 100644 index 0000000..fd91d8e --- /dev/null +++ b/src/01/02b/Makefile @@ -0,0 +1,37 @@ +#!/usr/bin/make -f +SHELL=/bin/sh + +CC=clang +TEST_LIBS = -lcgreen + +BUILDDIR := build +OBJS := $(addprefix $(BUILDDIR)/,doubly_linked_list.o) +TEST_OBJS := $(addprefix $(BUILDDIR)/,doubly_linked_list_test.o) + +$(BUILDDIR)/%.o : %.c + $(COMPILE.c) $(OUTPUT_OPTION) $< + +.PHONY: all +all: $(OBJS) $(BUILDDIR)/main.o + $(CC) $(OBJS) $(BUILDDIR)/main.o -o $(BUILDDIR)/program + +.PHONY: test +test: $(OBJS) $(TEST_OBJS) + $(CC) $(OBJS) $(TEST_OBJS) $(TEST_LIBS) -o $(BUILDDIR)/test + +$(OBJS): | $(BUILDDIR) + +$(TEST_OBJS): | $(BUILDDIR) + +$(BUILDDIR): + mkdir $(BUILDDIR) + +.PHONY: clean +clean: + rm -fr build + +run : all + ./build/program + +run_test : test + cgreen-runner -c -v $(BUILDDIR)/test diff --git a/src/01/02b/doubly_linked_list.c b/src/01/02b/doubly_linked_list.c new file mode 100644 index 0000000..88e49ee --- /dev/null +++ b/src/01/02b/doubly_linked_list.c @@ -0,0 +1,112 @@ +#include "doubly_linked_list.h" +#include <stdio.h> +#include <stdlib.h> + +Node *initialize(int data) { + Node *node = malloc(sizeof(Node)); + node->data = data; + node->next = NULL; + node->prev = NULL; + return node; +} + +Node *add(Node *head, int data) { + Node *tail; + Node *tmp = head; + + while(tmp) { + if (!tmp->next) + break; + tmp = tmp->next; + } + tail = tmp; + tail->next = initialize(data); + tail->next->prev = tail; + return tail->next; +} + +Node *get(Node *from, int index) { + if (!from || index < 0) return NULL; + + while(index > 0 && from){ + from = from->next; + index--; + } + return from; +} + +static int size(Node *head) { + int i = 0; + for (Node *tmp = head; tmp && tmp != NULL; tmp = tmp->next) i++; + return i; +} + +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; +} + +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); + + Node *xp = x->prev, *xn = x->next, *yp = y->prev, *yn = y->next; + + // if adjacent + if (x->next == y && y->prev == x) { + assign_next(x, yn); + assign_prev(x, y); + assign_prev(y, xp); + } else { + assign_prev(x, yp); + assign_next(x, yn); + assign_prev(y, xp); + assign_next(y, xn); + } +} + +Node *reverse(Node *head) { + Node *tmp = NULL; + Node *current = head; + + while (current != NULL) { + tmp = current->prev; + current->prev = current->next; + current->next = tmp; + current = current->prev; + } + return tmp ? tmp->prev : head; +} + +static void print(Node *node) { + if (node->prev && node->next) + printf("%d <- %d -> %d\n", node->prev->data, node->data, node->next->data); + else if (node->next) + printf("nil <- %d -> %d\n", node->data, node->next->data); + else + printf("%d <- %d -> nil\n", node->prev->data, node->data); +} + +static void inspect(Node *node) { + if (!node) return; + + printf("*******\n"); + while (node) { + print(node); + node = node->next; + } + printf("*******\n"); +} + diff --git a/src/01/02b/doubly_linked_list.h b/src/01/02b/doubly_linked_list.h new file mode 100644 index 0000000..63470c4 --- /dev/null +++ b/src/01/02b/doubly_linked_list.h @@ -0,0 +1,13 @@ +struct node { + int data; + struct node *next; + struct node *prev; +}; + +typedef struct node Node; + +Node *initialize(int data); +Node *add(Node *head, int data); +Node *get(Node *from, int index); +void swap(Node *x, Node *y); +Node *reverse(Node *head); diff --git a/src/01/02b/swap_doubly_linked_list_test.c b/src/01/02b/doubly_linked_list_test.c index 0520f67..ffc89cc 100644 --- a/src/01/02b/swap_doubly_linked_list_test.c +++ b/src/01/02b/doubly_linked_list_test.c @@ -1,4 +1,6 @@ #include <cgreen/cgreen.h> +#include "doubly_linked_list.h" + /* Swap two adjacent elements in a list by adjusting only the links (and not the data) using a: @@ -13,122 +15,6 @@ Describe(DoublyLinkedList); BeforeEach(DoublyLinkedList){ } AfterEach(DoublyLinkedList){ } -struct node { - int data; - struct node *next; - struct node *prev; -}; - -typedef struct node Node; - -static void print(Node *node) { - if (node->prev && node->next) - printf("%d <- %d -> %d\n", node->prev->data, node->data, node->next->data); - else if (node->next) - printf("nil <- %d -> %d\n", node->data, node->next->data); - else - printf("%d <- %d -> nil\n", node->prev->data, node->data); -} - -static void inspect(Node *node) { - if (!node) return; - - printf("*******\n"); - while (node) { - print(node); - node = node->next; - } - printf("*******\n"); -} - -static Node *initialize(int data) { - Node *node = malloc(sizeof(Node)); - node->data = data; - node->next = NULL; - node->prev = NULL; - return node; -} - -static Node *add(Node *head, int data) { - Node *tail; - Node *tmp = head; - - while(tmp) { - if (!tmp->next) - break; - tmp = tmp->next; - } - tail = tmp; - tail->next = initialize(data); - tail->next->prev = tail; - return tail->next; -} - -static Node *get(Node *from, int index) { - if (!from || index < 0) return NULL; - - while(index > 0 && from){ - from = from->next; - index--; - } - return from; -} - -static int size(Node *head) { - int i = 0; - for (Node *tmp = head; tmp && tmp != NULL; tmp = tmp->next) i++; - return i; -} - -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) { - 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; - - // if adjacent - if (x->next == y && y->prev == x) { - assign_next(x, yn); - assign_prev(x, y); - assign_prev(y, xp); - } else { - assign_prev(x, yp); - assign_next(x, yn); - assign_prev(y, xp); - assign_next(y, xn); - } -} - -static Node *reverse(Node *head) { - Node *tmp = NULL; - Node *current = head; - - while (current != NULL) { - tmp = current->prev; - current->prev = current->next; - current->next = tmp; - current = current->prev; - } - return tmp ? tmp->prev : head; -} - Ensure(DoublyLinkedList, when_getting_head) { Node *head = initialize(100); assert_that(get(head, 0), is_equal_to(head)); @@ -586,3 +472,9 @@ TestSuite *swap_doubly_linked_list_tests() { return suite; } + +int main(int argc, char **argv) { + TestSuite *suite = create_test_suite(); + add_suite(suite, swap_doubly_linked_list_tests()); + return run_test_suite(suite, create_text_reporter()); +} diff --git a/src/01/02b/main.c b/src/01/02b/main.c new file mode 100644 index 0000000..8249019 --- /dev/null +++ b/src/01/02b/main.c @@ -0,0 +1,15 @@ +#include "doubly_linked_list.h" +#include <stdio.h> +#include <stdlib.h> + +int next(void) { + return rand() % 100; +} + +int main(int argc, char *argv[]) +{ + printf("=== COMP-272 - Assignment 1 - Question 2b ===\n"); + Node *head = initialize(next()); + + return 0; +} |
