diff options
Diffstat (limited to 'src/01')
| -rw-r--r-- | src/01/02a/Makefile | 37 | ||||
| -rw-r--r-- | src/01/02a/main.c | 6 | ||||
| -rw-r--r-- | src/01/02a/singly_linked_list.c | 78 | ||||
| -rw-r--r-- | src/01/02a/singly_linked_list.h | 11 | ||||
| -rw-r--r-- | src/01/02a/singly_linked_list_test.c (renamed from src/01/02a/swap_singly_linked_list_test.c) | 95 |
5 files changed, 139 insertions, 88 deletions
diff --git a/src/01/02a/Makefile b/src/01/02a/Makefile new file mode 100644 index 0000000..82cdb5d --- /dev/null +++ b/src/01/02a/Makefile @@ -0,0 +1,37 @@ +#!/usr/bin/make -f +SHELL=/bin/sh + +CC=clang +TEST_LIBS = -lcgreen + +BUILDDIR := build +OBJS := $(addprefix $(BUILDDIR)/,singly_linked_list.o) +TEST_OBJS := $(addprefix $(BUILDDIR)/,singly_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/02a/main.c b/src/01/02a/main.c new file mode 100644 index 0000000..d2dd2af --- /dev/null +++ b/src/01/02a/main.c @@ -0,0 +1,6 @@ +#include <stdio.h> + +int main(int argc, char *argv[]) +{ + return 0; +} diff --git a/src/01/02a/singly_linked_list.c b/src/01/02a/singly_linked_list.c new file mode 100644 index 0000000..c4f3bb0 --- /dev/null +++ b/src/01/02a/singly_linked_list.c @@ -0,0 +1,78 @@ +#include "singly_linked_list.h" +#include <stdio.h> +#include <stdlib.h> + +static void inspect(Node *node) { + if (!node) return; + + printf("*******\n"); + while (node) { + printf("\t%d\n", node->data); + node = node->next; + } + printf("*******\n"); +} + +Node *initialize(int data) { + Node *node = malloc(sizeof(Node)); + node->data = data; + node->next = 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); + 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; +} + +void swap(Node **head, int x, int y) { + int count = size(*head); + + if (x == y) return; + if (x >= count) return; + if (y >= count) return; + + Node *xp = get(*head, x - 1); + Node *yp = get(*head, y - 1); + Node *xc = get(*head, x); + Node *yc = get(*head, y); + + if (x == 0) + *head = yc; + else + xp->next = yc; + + if (y == 0) + *head = xc; + else + yp->next = xc; + + Node *tmp = yc->next; + yc->next = xc->next; + xc->next = tmp; +} diff --git a/src/01/02a/singly_linked_list.h b/src/01/02a/singly_linked_list.h new file mode 100644 index 0000000..e240ce4 --- /dev/null +++ b/src/01/02a/singly_linked_list.h @@ -0,0 +1,11 @@ +struct node { + int data; + struct node *next; +}; + +typedef struct node Node; + +Node *initialize(int data); +Node *get(Node *from, int index); +Node *add(Node *head, int data); +void swap(Node **head, int x, int y); diff --git a/src/01/02a/swap_singly_linked_list_test.c b/src/01/02a/singly_linked_list_test.c index aedd015..63cbd6a 100644 --- a/src/01/02a/swap_singly_linked_list_test.c +++ b/src/01/02a/singly_linked_list_test.c @@ -1,97 +1,10 @@ +#include "singly_linked_list.h" #include <cgreen/cgreen.h> -/* -Swap two adjacent elements in a list by adjusting -only the links (and not the data) using a: -* singly-linked list -* doubly-linked list -*/ Describe(SinglyLinkedList); BeforeEach(SinglyLinkedList){ } AfterEach(SinglyLinkedList){ } -struct node { - int data; - struct node *next; -}; - -typedef struct node Node; - -static void inspect(Node *node) { - if (!node) return; - - printf("*******\n"); - while (node) { - printf("\t%d\n", node->data); - node = node->next; - } - printf("*******\n"); -} - -Node *initialize(int data) { - Node *node = malloc(sizeof(Node)); - node->data = data; - node->next = 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); - 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; -} - -void swap(Node **head, int x, int y) { - int count = size(*head); - - if (x == y) return; - if (x >= count) return; - if (y >= count) return; - - Node *xp = get(*head, x - 1); - Node *yp = get(*head, y - 1); - Node *xc = get(*head, x); - Node *yc = get(*head, y); - - if (x == 0) - *head = yc; - else - xp->next = yc; - - if (y == 0) - *head = xc; - else - yp->next = xc; - - Node *tmp = yc->next; - yc->next = xc->next; - xc->next = tmp; -} - Ensure(SinglyLinkedList, when_getting_head) { Node *head = initialize(100); assert_that(get(head, 0), is_equal_to(head)); @@ -276,3 +189,9 @@ TestSuite *swap_singly_linked_list_tests() { return suite; } + +int main(int argc, char **argv) { + TestSuite *suite = create_test_suite(); + add_suite(suite, swap_singly_linked_list_tests()); + return run_test_suite(suite, create_text_reporter()); +} |
