diff options
| author | mo khan <mo.khan@gmail.com> | 2020-07-04 13:10:46 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-07-04 13:10:46 -0600 |
| commit | 05f5d5496957939bee3cb360bea5b96da98b4a00 (patch) | |
| tree | a4d5bbb2cabfae7544ae9960650508a428fad93b /src/01 | |
| parent | 24700ade3ae9ff643b0c092ff617fb065d9f19f1 (diff) | |
Split stack_test.c
Diffstat (limited to 'src/01')
| -rw-r--r-- | src/01/01b/Makefile | 37 | ||||
| -rw-r--r-- | src/01/01b/README.md | 2 | ||||
| -rw-r--r-- | src/01/01b/main.c | 7 | ||||
| -rw-r--r-- | src/01/01b/stack.c | 99 | ||||
| -rw-r--r-- | src/01/01b/stack.h | 22 | ||||
| -rw-r--r-- | src/01/01b/stack_test.c | 127 |
6 files changed, 173 insertions, 121 deletions
diff --git a/src/01/01b/Makefile b/src/01/01b/Makefile new file mode 100644 index 0000000..008c82c --- /dev/null +++ b/src/01/01b/Makefile @@ -0,0 +1,37 @@ +#!/usr/bin/make -f +SHELL=/bin/sh + +CC=clang +TEST_LIBS = -lcgreen + +BUILDDIR := build +OBJS := $(addprefix $(BUILDDIR)/,stack.o) +TEST_OBJS := $(addprefix $(BUILDDIR)/,stack_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/01b/README.md b/src/01/01b/README.md index ceab17a..a6e39a8 100644 --- a/src/01/01b/README.md +++ b/src/01/01b/README.md @@ -7,7 +7,7 @@ Student ID: 3431709 Implement the stack methods `push(x)` and `pop()` using two queues. -Analyze the running time of the push(x) and pop() operations based on this implementation. +Analyze the running time of the `push(x)` and `pop()` operations based on this implementation. 2. Description of the Code: 3. Errors and Warnings: diff --git a/src/01/01b/main.c b/src/01/01b/main.c new file mode 100644 index 0000000..170ad11 --- /dev/null +++ b/src/01/01b/main.c @@ -0,0 +1,7 @@ +#include <stdio.h> +#include "stack.h" + +int main(int argc, char *argv[]) +{ + return 0; +} diff --git a/src/01/01b/stack.c b/src/01/01b/stack.c new file mode 100644 index 0000000..beffd64 --- /dev/null +++ b/src/01/01b/stack.c @@ -0,0 +1,99 @@ +#include "stack.h" +#include <stdio.h> +#include <stdlib.h> + +static Queue *newQueue(){ + Queue *queue = malloc(sizeof(Queue)); + queue->size = 0; + queue->head = NULL; + return queue; +} + +Stack *initialize() { + Stack *stack = malloc(sizeof(Stack)); + stack->q1 = newQueue(); + return stack; +} + +Node *newNode(int data) { + Node *node = malloc(sizeof(Node)); + node->data = data; + node->next = NULL; + return node; +} + +void enqueue(Queue *q, int data) { + Node *node = newNode(data); + q->size++; + + if (q->head == NULL) { + q->head = node; + } else { + Node *tail = q->head; + + while (tail->next != NULL) + tail = tail->next; + tail->next = node; + } +} + +int dequeue(Queue *q) { + if (q->head == NULL) return -1; + + Node *node = q->head; + int data = node->data; + + q->head = node->next; + free(node); + return data; +} + +// The running time is linear time. +void push(Stack *stack, int data) { + enqueue(stack->q1, data); +} + +// The running time is linear time. +int pop(Stack *stack) { + int count = stack->q1->size - 1; + Queue *tmp = newQueue(); + + for (int i = 0; i < count; i++) + enqueue(tmp, dequeue(stack->q1)); + + int data = dequeue(stack->q1); + free(stack->q1); + stack->q1 = tmp; + return data; +} + +int size(Stack *stack) { + return stack->q1->size; +} + +void destroy(Stack *stack) { + int count = size(stack); + + for (int i = 0; i < count; i++) + pop(stack); + + free(stack->q1); + free(stack); +} + +static void inspect(Queue *q) { + Node *tmp = q->head; + + if (q->size == 0) { + printf("[]\n"); + return; + } + + printf("["); + for (int i = 0; i < q->size; i++) { + printf("%d,", tmp->data); + tmp = tmp->next; + } + printf("\b]\n"); +} + diff --git a/src/01/01b/stack.h b/src/01/01b/stack.h new file mode 100644 index 0000000..7ccc173 --- /dev/null +++ b/src/01/01b/stack.h @@ -0,0 +1,22 @@ +struct node { + int data; + struct node *next; +}; + +typedef struct node Node; + +typedef struct { + Node *head; + int size; +} Queue; + +typedef struct { + Queue *q1; +} Stack; + + +Stack *initialize(); +void push(Stack *stack, int data); +int pop(Stack *stack); +int size(Stack *stack); +void destroy(Stack *stack); diff --git a/src/01/01b/stack_test.c b/src/01/01b/stack_test.c index b34c191..457f656 100644 --- a/src/01/01b/stack_test.c +++ b/src/01/01b/stack_test.c @@ -1,124 +1,5 @@ #include <cgreen/cgreen.h> - -/* -Implement the stack methods using two queues - -* `push(x)` -* `pop()` - -Analyze the running time of the `push(x)` and `pop()` operations based on this implementation. -*/ - -struct node { - int data; - struct node *next; -}; - -typedef struct node Node; - -typedef struct { - Node *head; - int size; -} Queue; - -typedef struct { - Queue *q1; -} Stack; - -static Queue *newQueue(){ - Queue *queue = malloc(sizeof(Queue)); - queue->size = 0; - queue->head = NULL; - return queue; -} - -static Stack *initialize() { - Stack *stack = malloc(sizeof(Stack)); - stack->q1 = newQueue(); - return stack; -} - -Node *newNode(int data) { - Node *node = malloc(sizeof(Node)); - node->data = data; - node->next = NULL; - return node; -} - -void enqueue(Queue *q, int data) { - Node *node = newNode(data); - q->size++; - - if (q->head == NULL) { - q->head = node; - } else { - Node *tail = q->head; - - while (tail->next != NULL) - tail = tail->next; - tail->next = node; - } -} - -int dequeue(Queue *q) { - if (q->head == NULL) return -1; - - Node *node = q->head; - int data = node->data; - - q->head = node->next; - free(node); - return data; -} - -// The running time is linear time. -static void push(Stack *stack, int data) { - enqueue(stack->q1, data); -} - -// The running time is linear time. -static int pop(Stack *stack) { - int count = stack->q1->size - 1; - Queue *tmp = newQueue(); - - for (int i = 0; i < count; i++) - enqueue(tmp, dequeue(stack->q1)); - - int data = dequeue(stack->q1); - free(stack->q1); - stack->q1 = tmp; - return data; -} - -int size(Stack *stack) { - return stack->q1->size; -} - -static void destroy(Stack *stack) { - int count = size(stack); - - for (int i = 0; i < count; i++) - pop(stack); - - free(stack->q1); - free(stack); -} - -static void inspect(Queue *q) { - Node *tmp = q->head; - - if (q->size == 0) { - printf("[]\n"); - return; - } - - printf("["); - for (int i = 0; i < q->size; i++) { - printf("%d,", tmp->data); - tmp = tmp->next; - } - printf("\b]\n"); -} +#include "stack.h" Describe(Stack); BeforeEach(Stack){ } @@ -166,3 +47,9 @@ TestSuite *stack_tests() { return suite; } + +int main(int argc, char **argv) { + TestSuite *suite = create_test_suite(); + add_suite(suite, stack_tests()); + return run_test_suite(suite, create_text_reporter()); +} |
