Commit c9575a5

mo khan <mo.khan@gmail.com>
2020-07-04 22:52:13
split singly_linked_list into separate files
1 parent 5d6de54
src/01/02a/main.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+int main(int argc, char *argv[])
+{
+  return 0;
+}
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
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;
+}
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);
src/01/02a/swap_singly_linked_list_test.c → 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());
+}