From 41fb3a2c92604a7964f32c55f614702a2e6e3860 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 17 Aug 2011 16:10:36 +0000 Subject: [PATCH] made it easier to change the stack test size, added linked-list implementation by Eric --- stack/Makefile | 2 +- stack/stack-test.c | 11 ++--- stack/stack.reference.eric.c | 81 ++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 stack/stack.reference.eric.c diff --git a/stack/Makefile b/stack/Makefile index 310469f..d865488 100755 --- a/stack/Makefile +++ b/stack/Makefile @@ -2,6 +2,6 @@ all: @rm -rf build; mkdir -p build cd build; cmake ../ cd build; make - ./build/stack-test + valgrind ./build/stack-test .PHONY: all diff --git a/stack/stack-test.c b/stack/stack-test.c index 6125f0a..a2ac0d7 100755 --- a/stack/stack-test.c +++ b/stack/stack-test.c @@ -10,6 +10,7 @@ void fail(char* msg) { } int main(int argc, char* argv[]) { + int max_len = 100; char* a = "abc"; char* d = "def"; char* g = "ghijklmnopqrstuvwxyz"; @@ -58,7 +59,7 @@ int main(int argc, char* argv[]) { // Test that stack can hold any number of elements // len = 1; - while (len < 99) { + while (len < (max_len - 1)) { len += 1; if (len != s_push(s, (void*) d)) { @@ -78,11 +79,11 @@ int main(int argc, char* argv[]) { // // Test edge case of last element inserted and removed // - if (100 != s_push(s, (void*) g)) { + if (max_len != s_push(s, (void*) g)) { fail("stack length is not 100"); } - if (100 != s_length(s)) { + if (max_len != s_length(s)) { fail("stack is not length 100 after adding 100 items"); } @@ -94,7 +95,7 @@ int main(int argc, char* argv[]) { fail("stack item 1 doesn't match peek"); } - if (99 != s_length(s)) { + if ((max_len - 1) != s_length(s)) { fail("stack is not length 99 after removing 1 item"); } @@ -102,7 +103,7 @@ int main(int argc, char* argv[]) { // // Test that the stack can be emptied as it was filled // - len = 99; + len = max_len - 1; while (len > 1) { len -= 1; diff --git a/stack/stack.reference.eric.c b/stack/stack.reference.eric.c new file mode 100644 index 0000000..de2f9df --- /dev/null +++ b/stack/stack.reference.eric.c @@ -0,0 +1,81 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ +* * +* Name: Eric Jacobs * +* What: Stack Implementation * +* Date: Aug 16, 2011 * +* File: stack.c * +* Version:0.1 * +* * +\* * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include +#include +#include "include/stack.h" + +// the implementation of the stack +struct Stack { + struct Node* top; + int length; +}; + +// Node for the linked list which will make the Stack. +struct Node { + struct Node* prev; + void* v; +}; + +// Initialize a Node. +struct Node* node_create(struct Node* prev, void* content); + +struct Node* node_create(struct Node* prev, void* v) { + struct Node* n = (struct Node*)malloc(sizeof(struct Node)); + n->prev = prev; + n->v = v; + return n; +} + +struct Stack* s_create() { + struct Stack* s = (struct Stack*)malloc(sizeof(struct Stack)); + + s->top = NULL; + s->length = 0; + return s; +} + +int s_length(struct Stack* s) { + return s->length; +} + +int s_push(struct Stack* s, void* v) { + struct Node* new_node = node_create(s->top, v); + s->top = new_node; + return ++s->length; +} + +void* s_pop(struct Stack* s) { + if(s->top == NULL) { + return NULL; + } + struct Node* node_tbd = s->top; + void* v = node_tbd->v; + s->top = node_tbd->prev; + free(node_tbd); + node_tbd = NULL; + s->length--; + return v; +} + +void* s_peek(struct Stack* s) { + if(s->top == NULL) { + return NULL; + } + return s->top->v; +} + +void s_destroy(struct Stack* s) { + while(s->length > 0) { + s_pop(s); + } + free(s); + s = NULL; +}